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... Quote Link to comment 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] Quote Link to comment 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" :) Quote Link to comment 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..... Quote Link to comment 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.