1

I want to update multiple mysql rows with one submit button. But there seems to be a problem in my php code.

    <?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'webtest');
define('DB_PASSWORD', '******');
define('DB_DATABASE', 'webtest');

$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_set_charset("utf8", $connection);
?>

    <form action='' method='post'>
    <table border='1'>
        <?php
            $result = mysql_query("SELECT * FROM users ");

                    echo "<tr>";
                        echo "<td colspan='3'>CLASS 1</td>";
                    echo "</tr>";

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

                    echo "<tr>";
                        echo "<td><input type='text' name='id' value='".$row['id']."' /></td>";
                        echo "<td>Email  :<input type='text' name='email' value='".$row['email']."' /></td>";
                        echo "<td>Username  :<input type='text' name='username' value='".$row['username']."' /></td>";
                        echo "<td>Password  :<input type='text' name='password' value='".$row['password']."' /></td>";
                    echo "</tr>";

            }

            echo "<input type='submit' name='update' value='UPDATE' />";
        ?>
    <table>
</form>

<?php
    $id = $_POST['id'];
    $update = $_POST['update'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];

if(isset($_POST['update'])){

foreach($_POST['email'] as $email){


    mysql_query("   UPDATE users
                       SET email= '$email', username='$username', password='$password'
                     WHERE id = '$id' ");   

}
    header("location: updateusers.php");
    exit;
}   

?>
7
  • What is the error you are facing? can please elaborate? Commented Apr 18, 2014 at 18:52
  • That's a really nice injection you have going on there... Commented Apr 18, 2014 at 19:04
  • mysql_query is now depreciated, see here for alternatives. php.net/mysql_query. Commented Apr 18, 2014 at 19:17
  • @dave: depreciated? Lol. Commented Apr 18, 2014 at 19:23
  • sorry, well I no longer appreciate it :p , deprecated may be a better phrase! Commented Apr 18, 2014 at 19:24

1 Answer 1

0

$_POST['email'] is not an array. You cannot loop it. You must declare input tag of email as an array to use it in foreach.

replace this

echo "<td>Email  :<input type='text' name='email' value='".$row['email']."' /></td>";

with

echo "<td>Email  :<input type='text' name='email[]' value='".$row['email']."' /></td>";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot, But it seems to have another problem, This only ipdates the last row it does not affect on every row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.