0

I need Update multiple rows in MySQL. I have one form and I need update more rows in one submit button.

My code:

<form action='#' method='POST' name='editakce'>
<input type='hidden' name='idsite[]' value='' />
<input type='text' name='one[]' value='' />
<input type='text' name='two[]' value='' />
<input type="submit" name="edit" />
</form>

And PHP script:

foreach($_POST['idsite'] as $key=>$id) {
    $sql[] = "UPDATE esko SET one = '".$_POST['one'][$key]."', two = '".$_POST['two'][$key]."' WHERE id = '".$_POST['idsite'][$key]."'";
    $mysql = MySQL_Query($sql); echo mysql_errno() . ": " . mysql_error(). "\n";
}

And script in script does not work determine 'id' to Mysql row.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Oct 14 '15 at 13:05
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 14 '15 at 13:05
  • This doesn't quite make sense. Usually one form would update one row, not multiple rows. – Jay Blanchard Oct 14 '15 at 13:06
  • Its not a good practice that run a update query in loop.Try to implement a different logic and create a one query for multiple update.You can use "when" and "case" for achieve the same – Ashish Kumar Saxena Oct 14 '15 at 13:14

0 Answers0