我正在嘗試使用MYSQL數據庫在PHP中進行基本更新。即使我輸入打印mysql_error();
,它也不會丟失任何錯誤。我使用三個文件,第一個是他們選擇編輯的donorid的初始屏幕。接下來進入編輯窗體,在窗體上他們可以選擇要編輯的字段。然後嘗試用最後一個文件更新數據庫。它們分別被稱爲edit.php,edit_form.php,edit_data.php。它不會更新。我不確定我錯過了什麼,任何幫助將不勝感激。編輯並更新mysql中的php
edit.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table>
<tr>
<td align="center">EDIT DATA</td>
</tr>
<tr>
<td>
<table border="1">
<form action="edit_form.php" method="POST">
<?php
include"db.inc.php";//database connection
$order = "SELECT * FROM donors";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[firstname]</td>");
echo ("<tr><td>$row[lastname]</td>");
echo ("<td>$row[donorid]</td>");
echo ("<td>$row[address]</td>");
<input type= "submit" name= "submit" value="Edit data"/>;
}
?>
</table>
</td>
</tr>
</table>
</body>
</html>
edit_form.php
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<?php
mysql_select_db("charitabledb") or die(mysql_error());
include "db.inc.php";
$donorid = (varchar)$_GET['donorid'];
$query = mysql_query("SELECT * FROM donors WHERE donorid = '$donorid'") or
die(mysql_error());
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$address = $row['address'];
}
?>
<form action="update.php" method="post">
<input type="hidden" name="ID" value="<?=$donorid;?>">
First Name: <input type="text" name="firstname" value="<?=$firstname?>"><br>
Last Name: <input type="text" name="lastname" value="<?=$lastname?>"><br>
Address: <input type="text" name="address" value="<?=$address?>"><br>
<input type="Submit">
</form>
<?php
}else{
echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>
</body>
</html>
edit_data.php
<?php
mysql_select_db("charitabledb") or die(mysql_error());
include "db.inc.php";
$donorid = (varchar)$_POST["donorid"];
$firstname = mysql_real_escape_string($_POST["firstname"]);
$lastname = mysql_real_escape_string($_POST["lastname"]);
$address = mysql_real_escape_string($_POST["address"]);
$query="UPDATE donors
SET firstname = '$firstname', lastname = '$lastname', address =
'$address'
WHERE donorid='$donorid'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>($donorid) Record Updated<p>";
}else{
echo "<p>($donorid) Not Updated<p>";
}
?>
下面是編輯,以試圖模仿我一直在考慮這樣的建議遠。提交按鈕不起作用,不知道如何使它在一個表內工作。
只是一個FYI ... **'mysql_ *'**函數已被棄用...考慮PDO或MySQLi代替... [PHP.net](http://php.net/manual/en /function.mysql-query.php) – brbcoding 2013-03-25 18:56:06