Jump to content

Looping Help


Sleeper

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.