Jump to content

update several MySQL rows


me1000

Recommended Posts

ok, So The user will be able to remove an entry from the database, however once they do it needs to update the rest of the entries...

 

for example, there is an 'ORDER' column in the DB

if there are 3 entries in the DB the order column would be filled out like so,

'1,2'3' respectivly.

 

however If I wanted to remove the row WHERE ORDER=1 the database will have the values to '2,3' instead it needs to update all the values that are greater than 1 and subtract 1 from them, so after you delete the row the db would be updated so that it said '1,2' again.

 

 

Here is the PART script I have written so far..

$inPageID       = (is_numeric($_REQUEST['id'])) ? $_REQUEST['id'] : die('invalid id');
	$query = "SELECT * FROM nav WHERE ASSO=$inPageID";

	$result = mysql_query($query);
	$blah = mysql_fetch_array($result);
	$newnum = $blah['ORDER'];


	$query = "SELECT * FROM nav WHERE `ORDER` > $newnum";
		$count = mysql_num_rows(mysql_query($query));
		//echo $count;
		//echo $query;
		//die;
	if ($count > 0) {	
		$result = mysql_query($query);
		while ($blah123 = mysql_fetch_array($result)){
			$newnum2 = $blah123['ORDER'] - 1;
			$query = "UPDATE nav SET `ORDER`='$newnum2' WHERE `ID`='".$blah123['ID']."'";
			$result = mysql_query($query);
			$message = ($result) ? "" : 'FAIL!';
			echo $message;
			//echo $query;

		}
	}

 

The error im getting is...

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [path...] on line 69

 

line 69 is

while ($blah123 = mysql_fetch_array($result)){

 

 

I believe it updates the first row after that, but then it fails when it tries to loop again.

 

Can anyone help me?

 

 

Thanks,

 

 

 

Link to comment
Share on other sites

It means your query wasn't successful.  You need to put in some debugging code  What you should do is place ...

 

or die('Query failed: ' . mysql_error());

 

... after all your queries, fetch_array's, etc., when debugging.  So your query becomes:

 

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

 

I don't know if your first query (just before while loop), or the second query (inside the loop) is what's failing.  The above tip will help.

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.