kenwvs Posted September 7, 2006 Share Posted September 7, 2006 [code]foreach($_POST['description'] as $key=>$description) { $query='INSERT INTO table (description,number) VALUES (\''.mysql_real_escape_string($description).'','.intval($_POST['number'][$key].')'; mysql_query($query) or die(mysql_error().$query); } [/code] is there something missing in this code.... when I try to use it, it complains about an unexpected comma, but if I remove the comma in there, it still complains about an unexpected error. With it in with the rest of my code, it causes all php code after it to not function in php....example my mysql_query are not working after this peice of code... Link to comment https://forums.phpfreaks.com/topic/19971-error-on-this-piece-of-code/ Share on other sites More sharing options...
ToonMariner Posted September 7, 2006 Share Posted September 7, 2006 in your query you have .'',', try this...[code]foreach($_POST['description'] as $key=>$description) { $query = "INSERT INTO table (description,number) VALUES ('" . mysql_real_escape_string($description) . "','" . intval($_POST['number'][$key] . "')"; mysql_query($query) or die(mysql_error().$query); }[/code] Link to comment https://forums.phpfreaks.com/topic/19971-error-on-this-piece-of-code/#findComment-87526 Share on other sites More sharing options...
btherl Posted September 7, 2006 Share Posted September 7, 2006 To avoid problems like this in future, I recommend you split such code over multiple lines. For example:[code]foreach($_POST['description'] as $key=>$description) { $query = "INSERT INTO table (description,number) VALUES ('" . mysql_real_escape_string($description) . "', " . intval($_POST['number'][$key] . " )"; mysql_query($query) or die(mysql_error().$query); }[/code]This makes it easier to see errors.Another nice convention is:[code]foreach($_POST['description'] as $key=>$description) { $description_esc = mysql_real_escape_string($description); $number = intval($_POST['number'][$key]); $query = "INSERT INTO table (description,number) VALUES (" . "'{$description_esc}', " . "{$number} )"; mysql_query($query) or die(mysql_error().$query); }[/code]This code is much easier to read. And "easy to read" means "fewer bugs" :) Link to comment https://forums.phpfreaks.com/topic/19971-error-on-this-piece-of-code/#findComment-87557 Share on other sites More sharing options...
kenwvs Posted September 7, 2006 Author Share Posted September 7, 2006 That solved this particular problem....Thank You very much. I am going to post another question regarding this..... Link to comment https://forums.phpfreaks.com/topic/19971-error-on-this-piece-of-code/#findComment-87576 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.