我想使用php在数据库中更新我的表,但它没有更新。我不知道问题出在哪里
emember.php
<?php
$id= $_GET['id'];
$con=mysqli_connect("localhost","root","","members");
$result=mysqli_query($con,"select * from member_info");
echo "<form action='update.php' method='post'>";
while($row=mysqli_fetch_array($result))
{
echo "<table border=1>";
//echo "Profile Id: <input type='text' name='profile_id' value = '$row[profile_id]'> <br>"
echo "<tr>";
echo "<br>"."<tr>"."ID: <input type='text' name='id' value = '$row[id]'"."</tr>";
echo "<br>"."<tr>"."Username: <input type='text' name='username' value = '$row[username]'"."</tr>";
echo "<br>"."Password: <input type='text' name='password' value = '$row[password]'"."<br>";
echo "<br>"."Firstname: <input type='text' name='firstname' value = '$row[firstname]'"."<br>";
echo "<br>"."Lastname: <input type='text' name='lastname' value = '$row[lastname]'"."<br>";
echo "<br>"."Address: <input type='text' name='address' value = '$row[address]'"."<br>";
echo "<br>"."Gender: <input type='text' name='gender' value = '$row[gender]'"."<br>";
echo "<br>"."Birthdate: <input type='text' name='birthdate' value ='.$row[birthdate]'"."<br>";
}
echo"</tr>";
echo "</table>";
echo "<input type='submit' value = 'Save'>";
echo "</form>";
?>
update.php
<?php
$id=$_POST['id'];
$username= $_POST['username'];
$password= $_POST['password'];
$firstname= $_POST['firstname'];
$lastname= $_POST['lastname'];
$address= $_POST['address'];
$gender= $_POST['gender'];
$birthdate= $_POST['birthdate'];
$con= mysqli_connect("localhost","root","","members");
mysqli_query($con,"update member_info set id='$id',username='$username',password='$password',firstname='$firstname',lastname='$lastname',address='$address',gender='$gender',birthdate='$birthdate' where id='$id'");
echo "Successfully updated!";
echo "Back to <a href='index.php'>home</a>";
?>
任何人都可以帮助我的代码出错:
答案 0 :(得分:1)
这样做:
<?php
// Your vars:
$id = $_POST['id'];
$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$gender = $_POST['gender'];
$birthdate = $_POST['birthdate'];
// Use of object based MySQLi:
$mysqli = new mysqli("localhost","root","","members");
// Prepare your query:
$stmt = $mysqli->prepare("UPDATE member_info SET username = ?,
password = ?,
firstname = ?,
lastname = ?,
address = ?,
gender = ?,
birthdate = ?
WHERE id = ?");
$stmt->bind_param($username,
$password,
$firstname,
$lastname,
$address,
$gender,
$birthdate,
$id);
// Execute your query:
$stmt->execute();
// And close it! You're done!
$stmt->close();
echo "Successfully updated!";
echo "Back to <a href='index.php'>home</a>";
?>
答案 1 :(得分:0)
首先,如果列“id”是auto_incremented,您将无法更新
答案 2 :(得分:0)
我在您的代码中看到的问题:
<强> emember.php
<input
标记未关闭
$row[id]
应该按以下方式访问:
echo "<br>"."<tr>"."ID: <input type='text' name='id' value = '".$row["id"]."' /></tr>";
<强> update.php
我认为您不应更新id
,因为您要更新特定id
的记录。
它应该是:
"update member_info set username='$username',password='$password',firstname='$firstname',lastname='$lastname',address='$address',gender='$gender',birthdate='$birthdate' where id='$id'"