我想用一个提交按钮更新多个mysql行。但是我的php代码似乎有问题。
<?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;
}
?>
答案 0 :(得分:0)
$_POST['email']
不是数组。你无法循环它。您必须将电子邮件的输入标记声明为数组,以便在foreach中使用它。
替换此
echo "<td>Email :<input type='text' name='email' value='".$row['email']."' /></td>";
与
echo "<td>Email :<input type='text' name='email[]' value='".$row['email']."' /></td>";