MySQL will not update the last cell of the last row of the table

I use PHP and MySQL.

MySQL will not update the last cell in my table. I can’t understand why.

Basically, I am doing 10 INSERT with a foreach () loop in an array. In each loop, I use the following code:

$sql = "INSERT INTO table1 (name, address, phone, date_time, process_started, process_ended) VALUES ('$name', '$address', '$phone', NOW(), 'started', '')"; $result = @mysql_query($sql, $con) or die(mysql_error()); 

This works great; all data is inserted into the table.

Then, when I return to update the "process_ended" field, it only updates the penalty for the first 9 lines. But it will not update the cell "process_ended" from the 10th row.

Here is the code to update. It does not use a loop; I just typed it in a script 10 times.

 $sql = "UPDATE table1 SET process_ended = 'ended' WHERE name = '$name' && address = '$address'"; $result = @mysql_query($sql, $con) or die(mysql_error()); 

I checked that the name and address cells match what is in the script; and they do it. I also checked three times to make sure there were no typos in my scripts.

I think this is due to MySQL, because when I switch the order of updates, it is always the last cell that is not updated. I deleted the table and redid it, and I also hit the DB restore button in cPanel, and it says that this is normal.

+4
source share
8 answers

Perhaps UPDATE objects to updating the empty string. Have you tried it with a placeholder instead of '??

+1
source

Two loops: sequential or parallel? If the loops are independent, check to see if the first completes before the second.

+1
source

This is usually how I debug it.

  • Do not fulfill your requests
  • Repeat instructions or save them to a file
  • Analyze it! In your case, I think process_ended is omitted in the last statement.
  • If not, run the last SQL statement. Get an error message and post it here!

Good luck.

0
source

I'm not quite sure, but I had similar problems with php arrays. For me, it looks like a trailing character in php, just trim() your last cell value before pasting. This solved my problems.

0
source

PHP arrays are counted from 0.

If your script starts counting from 1 somewhere, the first or last element may disappear.

0
source

This is not mySQL. billions of tables are updated every day, this error would cause a huge problem, if that were the case. change the update statement to a SELECT statement and view the results, check for duplicate rows.

 $sql = "SELECT FROM table1 WHERE name = '$name' && address = '$address'"; $result = @mysql_query($sql, $con) or die(mysql_error()); 

The problem is most likely your WHERE clause. I suppose you find that you are executing the same statement twice. therefore, the update instruction updates the same line twice.

It doesn’t matter in which order you run update requests, because each time it updates the same rows in the table, and therefore, when you specify rows, they look the same without updating (this is so). it was the last, perhaps the first or seventh.

Probably, when creating instructions, a copy and paste error occurs. I do this all the time :(

DC

0
source

With MySQL, I always find the best way to find an error like this by trying it manually. execute the $sql screen echo that you are trying to run, and then enter it through PHPMyAdmin and see what it returns with.

This will help you identify the problem.

0
source

Do not use the loop badly without using the abstract functions insertitemtotable () - bad, return to the correct design from the very beginning and you will never see this problem after your main functions are cleared.

In addition, it will help you a lot to make sure that all your data is properly shielded, etc.

There probably is a problem in your code that you type manually and copy / paste, for example, update two times by 9, or something like that.

0
source

All Articles