簡體   English   中英

使用php更新表中的值

[英]update a the value in the table using php

我想使用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>";
?>

任何人都可以幫助解決我的代碼有什么問題嗎?

像這樣做:

<?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”是auto_incremented,則您將無法更新它

我在您的代碼中看到的問題:

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'"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM