Sleeper Posted December 7, 2010 Share Posted December 7, 2010 ok I'm a bit stuck here were I would think something would work easily im getting an error. What I am trying to loop is.. $data="UPDATE ts12 SET djid='$_POST[TS0]' WHERE id=1"; $data="UPDATE ts12 SET djid='$_POST[TS1]' WHERE id=2"; This would go on for 84 id's. So I figured I could just loop this line so I only have to type it once. Right now I have it set up for just testing 7 ids and I have this code.... if ($edit == yes) { for ($i=0; $i < 7; $i++){ $n=($i+1); $data.="UPDATE ts12 SET djid='$_POST[TS$i]' WHERE id=$n"; } if (!mysql_query($data,$con)) die('Error: ' . mysql_error()); }else{} The error I am getting is... PHP Parse error: syntax error, unexpected T_VARIABLE, expecting ']' in dir/page on line 6 What am I doing wrong here? Link to comment https://forums.phpfreaks.com/topic/220974-looping-help/ Share on other sites More sharing options...
rondog Posted December 8, 2010 Share Posted December 8, 2010 geez, nobody believe in formatting code anymore? $data = ''; for ($i = 0; $i < 7; $i++) { $n = ($i + 1); $data .= "UPDATE ts12 SET djid='" . $_POST["TS" . $i] . "' WHERE id=" . $n; } Also, I dont think that query isn't going to work. I think you have to run the query each time in the loop. You cant just pass a big string like that. I may be wrong though, never tried that. Link to comment https://forums.phpfreaks.com/topic/220974-looping-help/#findComment-1144213 Share on other sites More sharing options...
Sleeper Posted December 8, 2010 Author Share Posted December 8, 2010 Ty, that got past the page error but now im getting a mysql error... Error: 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 'UPDATE ts12 SET djid='1' WHERE id='2'' at line 1 My Server version is 5.1.51 Link to comment https://forums.phpfreaks.com/topic/220974-looping-help/#findComment-1144220 Share on other sites More sharing options...
rondog Posted December 8, 2010 Share Posted December 8, 2010 Yes I figured it was going to error...do this for ($i = 0; $i < 7; $i++) { $n = ($i + 1); $sql = "UPDATE ts12 SET djid='" . $_POST["TS" . $i] . "' WHERE id=" . $n . "<br/>"; $query = mysql_query($sql) or die(mysql_error()); } basically move the query part up into the for loop. I got rid of the $data var and named it $sql and set it to '=' rather than '.=' since we dont need to concat the string. Link to comment https://forums.phpfreaks.com/topic/220974-looping-help/#findComment-1144223 Share on other sites More sharing options...
Sleeper Posted December 8, 2010 Author Share Posted December 8, 2010 That worked. TYVM. Link to comment https://forums.phpfreaks.com/topic/220974-looping-help/#findComment-1144226 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.