Updating one column in all rows in a table

You’re over-complicating the solution. In order to update every record, the approach you’re trying to take is:

  1. Select all records.
  2. Get the ID for each record.
  3. Loop through the IDs.
  4. Update each record by that ID.

The UPDATE syntax has a much easier way to do this. You don’t need to supply a WHERE clause to an UPDATE statement. Without that clause, it will by default update every record in the table:

UPDATE TableName SET `board`='value'

Also, please be aware that you have a SQL injection vulnerability in your code. By using
$_REQUEST['text'] directly in your SQL query, you allow any user to send SQL code to your query. Your code then executes whatever they send you. This could allow them to corrupt or delete your data, even gain administrative access to your server.

For starters, please stop using mysql_* functions. PHP has deprecated those and they should no longer be used. There is a mysqli_ replacement for them. In addition to that, use the mysqli_real_escape_string() function to sanitize your inputs before using them in a SQL query. And finally, use prepared statements instead of directly concatenating values into your SQL string.

Leave a Comment