aabid Posted May 3, 2010 Share Posted May 3, 2010 hi guys, as i am new to php so i was making a simple script which will take the data from text field and send it to the database but the data is not inserting in table.i don't know why plz check. here is the coding of form <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ADD Records</title> </head> <body> <form action="addresults.php" method="post"> <table align="center" border="1"> <? if(isset($_POST['Submit'])) { echo "<tr align=\"center\"><td colspan=2>Records Added, ADD Another</td></tr>"; } ?> <tr align="center"> <td colspan="2"><strong>::ENTER DATA IN RESPECTIVE FIELDS::</strong></td> </tr> <tr align="center"> <td width="104"><strong>Roll Number</strong></td> <td width="316"><input type="text" name="rollno" /></td> </tr> <tr align="center"> <td><strong>Name</strong></td> <td><input type="text" name="uname" /></td> </tr> <tr align="center"> <td><strong>Maths</strong></td> <td><input type="text" name="maths" /></td> </tr> <tr align="center"> <td><strong>Physics</strong></td> <td><input type="text" name="physics" /></td> </tr> <tr align="center"> <td><strong>Chemistry</strong></td> <td><input type="text" name="chemistry" /></td> </tr> <tr align="center"> <td><strong>English</strong></td> <td><input type="text" name="english" /></td> </tr> <tr align="center"> <td><strong>Hindi</strong></td> <td><input type="text" name="hindi" /></td> </tr> <tr align="center"> <td colspan="2"><input type="submit" name="Submit" /></td> </tr> </table> </form> </body> </html> and here is the script which recieves the data (addresults.php) <?php $connect = mysql_connect("localhost","root","******") or die(mysql_error()); $db = mysql_select_db("test",$connect) or die(mysql_error()); $sql = "INSERT INTO results(rollno,name,maths,physics,chemistry,enlish,hindi) VALUES($_POST[rollno],\"$_POST[uname]\",$_POST[maths],$_POST[physics],$_POST[chemistry],$_POST[english],$_POST[hindi]"; $execquery = mysql_query($sql) or die(mysql_error()); echo "Records Added"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/ Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 you need to quote your $_POST returns eg $_POST['maths'] Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052324 Share on other sites More sharing options...
aabid Posted May 3, 2010 Author Share Posted May 3, 2010 oh thanks that was a quick reply however i did what you said but again the error appeared which says, Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\project\addresults.php on line 6 plz help....... Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052330 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 When you put php array variables, i.e. $_POST['...'], inside of a double-quoted string, they must be enclosed in {} so that the php parser can find where they start and end. That will fix the php error message you are currently getting. Any string data that is put into an sql query statement must be enclosed in single-quotes so that sql will treat it as a string. If you are still getting a sql error, please post it as that will result in the quickest solution without a lot of guessing. Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052335 Share on other sites More sharing options...
aabid Posted May 3, 2010 Author Share Posted May 3, 2010 hi PFMaBiSmAd thanks for your reply, i added the curly braces just as you said now my query looks like, $sql = "INSERT INTO results(rollno,name,maths,physics,chemistry,enlish,hindi) VALUES({$_POST['rollno']},\"{$_POST['uname']}\",{$_POST['maths']},{$_POST['physics']},{$_POST['chemistry']},{$_POST['english']},{$_POST['hindi']}"; the error i got after this is, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052337 Share on other sites More sharing options...
aabid Posted May 3, 2010 Author Share Posted May 3, 2010 help me guys waiting for your replies. Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052352 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 Calm down. You didn't even follow the instructions of the last 2 posters. You need to wrap those $_POST values in single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052356 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 Also, if you echo $sql, you will be able to see exactly what is in it (you are currently missing a closing ) that is part for the VALUES () term.) Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052361 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 Try: $sql = 'INSERT INTO results(rollno,name,maths,physics,chemistry,enlish,hindi) VALUES('.$_POST['rollno'].', \''.$_POST['uname'].'\', '.$_POST['maths'].', '.$_POST['physics'].', '.$_POST['chemistry'].', '.$_POST['english'].', '.$_POST['hindi'].')'; this is assuming that name is the only string field in your database. Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052368 Share on other sites More sharing options...
aabid Posted May 3, 2010 Author Share Posted May 3, 2010 thanks Muddy_Funster your solution worked like a charm,its now inserting all the things. however PFMaBiSmAd also got it right, i was missing right parethesis. thanks guys 5/5 Quote Link to comment https://forums.phpfreaks.com/topic/200548-phpmysql-error-plz-help-newbie-here/#findComment-1052405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.