使用php更新表中的值
我想用php更新数据库中的表,但它没有更新。我不知道问题出在哪里 emember.php使用php更新表中的值,php,mysql,Php,Mysql,我想用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=
<?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>";
?>
<?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>";
?>
这样做:
<?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>";
?>
首先,如果列“id”是自动递增的,您将无法更新它我在代码中看到的问题:
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>";
?>
<?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>";
?>
在ememember.php
中,像这样访问$row['id']
而不是$row[id]
要更新整个表还是只更新一行?首先尝试回显通过POST方法获得的所有值。。因为如果任何一个值为null,那么整个查询在很多时候都无法工作。。因此,首先检查所有的值。另外,在mysqli\u查询之后,写入echo mysqli\u错误($con)代码>并让我们知道,如果它显示您有任何错误,很难找出哪里出了问题。您需要通过在mysqli_query()之后添加这一行来确定查询是否失败,如下所示:echo mysqli_error()@USER269948好的..很好。。如果你认为其中一个答案解决了你的问题,你应该接受这个答案。这将帮助其他访问者(对此问题)选择已解决的解决方案。谢谢。