Jump to content

my sql select id and then update problem.


calmchess

Recommended Posts

I need to select some auto incremented ids out of a database and then use those id in a where statement which updates the database at the id's location the below code works up to the update statements where it just dies without coughing up an error or anything......can you spot whats wrong and help me fix it?

 

 

$current = Array();
$currsel = mysql_query("select id from page0.page0 where refnum ='$newnum' AND username!='admin'")	
    or die(mysql_error());
while($currrow = mysql_fetch_array($currsel)){
 array_push($current,$currrow['id']);
}
print_r($current);

$current1 = Array();
$currsel1 = mysql_query("select id from page0.page0 where refnum ='$oldnum' AND username!='admin'")	
    or die(mysql_error());
while($currrow1 = mysql_fetch_array($currsel1)){
 array_push($current1,$currrow1['id']);
}
print_r($current1);

$upref1= mysql_query("update page0.page0 SET refnum='$newnum' where id='$current[0]' OR id='$current[1]'OR id='$current[2]'OR'id=$current[3]'")	
    or die(mysql_error());




$upref11= mysql_query("update page0.page0 SET refnum='$oldnum' where id='$current1[0]'OR id='$current1[1]'OR id='$current1[2]'OR id='$current1[3]'")	
    or die(mysql_error());

 

Thanks for the reply.......I've tried using AND ....still doesn't work.......its driving me mad because looking at the code everything should work just fine.....the id go into the array when i print it out just fine......it just the update won't update at each integer that is in the array..:(

Try this example:

 

<?php
/* WHERE ID = This Number */
$oldnum = "1";

$current = array();
$currsel = mysql_query("SELECT `id` FROM `page0` WHERE `refnum` = '$oldnum' AND `username` != 'admin'") or die(mysql_error());

if (mysql_num_rows($currsel) > 0)
{
while($row = mysql_fetch_array($currsel))
{
	$current[] = $row['id'];
}

$updateStr = array();

foreach ($current as $var)
{
	$updateStr[] = "`id` = '{$var}'";
}

echo "<pre>";
print_r($updateStr);
echo "</pre>";

$updateStr = (count($updateStr) > 1) ? implode(" OR ", $updateStr) : $updateStr[0];

echo "UPDATE `page0` SET --what you want to update-- WHERE ($updateStr) AND `username` != 'admin'";
}
else
{
echo "No Records Found.";
}

?>

I'm not exactly sure what you wanted to update, so you need to modify this so that its correct:

UPDATE `page0` SET --what you want to update-- WHERE ($updateStr) AND `username` != 'admin'

 

Then simply change the echo to a mysql_query if its right.

this didn't do quite what i wanted but thanks for tryin i'll keep trying to get my code to work it does everything i need it to do it just won't use the variables in the update statment if i chang the variables to integers then it works.....it a variable problem for some reason unknown to me.....take care and I like your php skills

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.