Michael Eugene Yuen
Michael Eugene Yuen

Reputation: 2528

Foreach loop still execute after the last array is updated

I get an error "Error: Query was empty" after completing the task instead of the echo result. I suspect that the FOREACH still attempt to execute the query even the last array is updated. Am I right?

If yes, how can I correct this?

<?php
    $con = mysql_connect("localhost", "$username" , "$password");
    mysql_select_db($database, $con);
?>

<?php

  foreach ($_POST['id'] as $key=>$id)
  {
    $order = $_POST['order'][$key];
    mysql_query("UPDATE table SET `order`=$order WHERE `id` = $id");
  }
    if (!mysql_query($sql,$con))
     {
       die('Error: ' . mysql_error());
     }
       echo "<div class=\"result\">**1 record updated**</div>";         
?>

Many thanks

More information about the form posting these value : Create array from form and update mysql

Upvotes: 0

Views: 301

Answers (4)

Angripa
Angripa

Reputation: 157

In other condition, have you check your connection before? If it's okay try this

<?php
  foreach ($_POST['id'] as $key=>$id)
  {
    $order = $_POST['order'][$key];
    $query = "UPDATE table SET `order`=$order WHERE `id` = $id";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());

  }
    if (!$result)
     {
       die('Error: ' . mysql_error());
     }
       echo "<div class=\"result\">**1 record updated**</div>";         
?>

Upvotes: 1

jagc
jagc

Reputation: 209

The problem could be in:

 if (!mysql_query($sql,$con))

its' running mysql_query without $sql being defined.

Though i don't recommend putting a query inside a loop because it's a waste of memory, for your code sample - try putting mysql_query() inside a variable like so:

$sql = mysql_query("UPDATE table SET `order`=$order WHERE `id` = $id");

Upvotes: 2

BlitZ
BlitZ

Reputation: 12168

  1. You have excluded query execution from loop.
  2. You haven't defined $sql anywhere, so it is threated as empty string.

Use this:

<?php
foreach ($_POST['id'] as $key => $id)
{
    $order = mysql_real_escape_string($_POST['order'][$key], $con);
    $id    = mysql_real_escape_string($id, $con);

    $sql   = "UPDATE table SET `order` = $order WHERE `id` = $id";

    if(!mysql_query($sql, $con))die(mysql_error());
}      
?>

NOTE: MySQL (mysql_* functions) extension is deprecated. I suggest to use MySQLi (mysqli_* functions) or PDO instead.

NOTE: Your code is vulnurable to SQL-Injection attacks. You may read solution suggestions in this question: How can I prevent SQL injection in PHP?.

Upvotes: 1

user1864610
user1864610

Reputation:

You're not checking your errors properly, and instead, calling mysql_query twice. Do this:

<?php
foreach ($_POST['id'] as $key=>$id)
{
  $order = $_POST['order'][$key];
  $result = mysql_query("UPDATE table SET `order`=$order WHERE `id` = $id");
  if ($result === false)
  {
    die('Error: ' . mysql_error());
  }
  echo "<div class=\"result\">**1 record updated**</div>";         
}
?>

Obligatory notices:

  • Don't' use mysql - use mysqli or PDO
  • Do make sure you properly escape inputs to SQL queries. Use mysql_real_escape_string()

Upvotes: 1

Related Questions