Add data to mysql database

Hi,

I am unsure why i am getting the following error message
Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\addtest.php on line 29
basically i want to add data to one of my databases can anyone please help…

Thanks

[php]

<?php require_once "dbc.php"; $result = mysql_query("SELECT * FROM ode_testscript WHERE test_script_id=".mysql_real_escape_string($_GET['id'])); $r = mysql_fetch_array($result); mysql_select_db("ode_testscript") or die (mysql_error()); if(isset($_POST['submit'])) { $test_script_id = $_POST['test_script_id']; $test_name = $_POST['test_name']; $expect_result = $_POST['expect_result']; $actual_result = $_POST['actual_result']; $test_status = $_POST['test_status']; $jira_number = $_POST['jira_number']; $query = mysql_query("INSERT INTO ode_testscript SET test_script_id='{$_POST["test_script_id"]}', test_name='{$_POST["test_name"]}, expect_result= '{$_POST["expect_result"]}, actual_result='{$_POST["actual_result"]},test_status='{$_POST["test_status"]}, jira_number='{$_POST["jira_number]}" ]} } ?>

[/php]

Test Number

Test Step

Expected Result

Actual Result

Pass/ Fail/ Not Tested Pass Fail Not Tested

Jira Number

Hello nitesh, your insert query is wrong. please replace below code with my code. [php] // replace code $query = mysql_query("INSERT INTO ode_testscript SET test_script_id='{$_POST["test_script_id"]}', test_name='{$_POST["test_name"]}, expect_result= '{$_POST["expect_result"]}, actual_result='{$_POST["actual_result"]},test_status='{$_POST["test_status"]}, jira_number='{$_POST["jira_number]}"]}

[/php]
use below code(My code)

[php]
$insertsql = “INSERT INTO ode_testscript (test_script_id, test_name, expect_result,actual_result,test_status,jira_number) VALUES (”.$test_script_id.", ‘".$test_name."’, ‘".$expect_result."’,’".$actual_result."’,’".$test_status."’,’".$jira_number."’)";
mysql_query($insertsql);
[/php]

Note: “if any field of table ode_testscript is define as integer in database than just remove the single qoutes define around that field in my above code.”;
Example if test_script_id is define as INT than use as “.$test_script_id.” else ‘".$test_script_id."’
i hope you are getting my point.

I hope this will helpful for you.
Reply your feedback.
SR

Hi,

Thanks for your help, i put the corrected php into my file. However i came across some new errors messages, im unsure of how to fix these errors could your please help me…

Errors

Notice: Undefined index: id in C:\xampp\htdocs\addtest.php on line 4

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\addtest.php on line 5

dbc.php
[php]

<?php mysql_connect("127.0.0.1","root","pass") or die ("Unable to connect to the database"); mysql_select_db("testscriptsystem")or die ("Couldn't find database"); ?>

[/php]

addtest.php
[php]

<?php require_once "dbc.php"; Line 4:$result = mysql_query("SELECT * FROM ode_testscript WHERE test_script_id=".mysql_real_escape_string($_GET['id'])); Line 5:$r = mysql_fetch_array($result); mysql_select_db("ode_testscript") or die (mysql_error()); if(isset($_POST['submit'])) { $test_script_id = $_POST['test_script_id']; $test_name = $_POST['test_name']; $expect_result = $_POST['expect_result']; $actual_result = $_POST['actual_result']; $test_status = $_POST['test_status']; $jira_number = $_POST['jira_number']; $insertsql = "INSERT INTO ode_testscript (test_script_id, test_name, expect_result,actual_result,test_status,jira_number) VALUES (".$test_script_id.", '".$test_name."', '".$expect_result."','".$actual_result."','".$test_status."','".$jira_number."')"; mysql_query($insertsql); } ?>

Test Number

Test Step

Expected Result

Actual Result

Pass/ Fail/ Not Tested Pass Fail Not Tested

Jira Number

[/php]
Hello nitesh [php] #Do this on Line 4 $tsid = mysql_real_escape_string($_GET['id']); $sql = "SELECT * FROM ode_testscript WHERE test_script_id=".$tsid; echo $sql; exit; [/php] run the $sql directly in database. one more thing test_script_id is integer or varchar in database table ~~SR~~

Hi,

Yeah they are set to integers for both test_script_id and jira_number… i have amended these by removing the single quotes for both. When i changed line 4 i came up with another undefined index error message…
Notice: Undefined index: id in C:\xampp\htdocs\addtest.php on line 4
SELECT * FROM ode_testscript WHERE test_script_id=

[php]

<?php require_once "dbc.php"; $tsid = mysql_real_escape_string($_GET['id']); $sql = ("SELECT * FROM ode_testscript WHERE test_script_id=".$tsid); echo $sql; exit; $r = mysql_fetch_array($result); mysql_select_db("ode_testscript") or die (mysql_error()); if(isset($_POST['submit'])) { $test_script_id = $_POST['test_script_id']; $test_name = $_POST['test_name']; $expect_result = $_POST['expect_result']; $actual_result = $_POST['actual_result']; $test_status = $_POST['test_status']; $jira_number = $_POST['jira_number']; $insertsql = "INSERT INTO ode_testscript (test_script_id, test_name, expect_result,actual_result,test_status,jira_number) VALUES (".$test_script_id.", '".$test_name."', '".$expect_result."','".$actual_result."','".$test_status."',".$jira_number.")"; mysql_query($insertsql); } ?>

[/php]

Thanks for your help… :slight_smile:

hello nitesh, i think you are not getting any value from $_GET['id']. so use below modified code instead of your currect code [php] #use this code <?php require_once "dbc.php"; if(isset($_GET['id']) && !empty($_GET['id'])) { $tsid = mysql_real_escape_string($_GET['id']); $sql = "SELECT * FROM ode_testscript WHERE test_script_id=".$tsid; $result = mysql_query($sql) $r = mysql_fetch_array($result); mysql_select_db("ode_testscript") or die (mysql_error()); if(isset($_POST['submit'])) { $test_script_id = $_POST['test_script_id']; $test_name = $_POST['test_name']; $expect_result = $_POST['expect_result']; $actual_result = $_POST['actual_result']; $test_status = $_POST['test_status']; $jira_number = $_POST['jira_number']; $insertsql = "INSERT INTO ode_testscript (test_script_id, test_name, expect_result,actual_result,test_status,jira_number) VALUES (".$test_script_id.", '".$test_name."', '".$expect_result."','".$actual_result."','".$test_status."',".$jira_number.")"; mysql_query($insertsql); } } else { echo 'id is not define'; } ?> [/php] I hope this will helpful for you.. Reply your feedback ~~SR~~

Yeah, your correct… ID was not defined… how would i go about assigning an id… currently i have test_script_id on auto increment so i was thinking of changing the id to test_script_id… but it still produces id is not define

hello nitesh, if your are new to php than listen that there is no need to pass auto increment field value for insertion. remove this line from your form

Test Number

use below code for insert data into table [php] <? require_once "dbc.php"; mysql_select_db("ode_testscript") or die (mysql_error()); if(isset($_POST['submit'])) { //$test_script_id = $_POST['test_script_id']; $test_name = $_POST['test_name']; $expect_result = $_POST['expect_result']; $actual_result = $_POST['actual_result']; $test_status = $_POST['test_status']; $jira_number = $_POST['jira_number']; $insertsql = "INSERT INTO ode_testscript (test_name, expect_result,actual_result,test_status,jira_number) VALUES ('".$test_name."', '".$expect_result."','".$actual_result."','".$test_status."',".$jira_number.")"; mysql_query($insertsql); } ?> [/php] i hope this will helpful. ~~SR~~
Sponsor our Newsletter | Privacy Policy | Terms of Service