我幾乎沒有編程經驗,並嘗試這第一個項目,我有點卡住如何更新數據庫,所以我點擊編輯和正確的記錄被加載到編輯屏幕更新。 phpPHP表單不更新mysql數據庫
當我點擊更新時,我收到updated.php的消息,說數據庫已更新,但數據庫沒有更新,當我顯示記錄時它們與更新前相同,謝謝提前爲您提供所有幫助。
下面的代碼:
update.php
$objConnect = mysql_connect("localhost","username","password") or die(mysql_error());
$objDB = mysql_select_db("teldirdb");
$id = $_GET['id'];
$strSQL = "SELECT * FROM teldir where id = '$id' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult = mysql_fetch_array($objQuery)
?>
<form id="FormName" action="updated.php" method="post" name="FormName">
<table width="448" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="150" align="right"><label for="fname">fname</label></td>
<td><input name="fname" maxlength="30" type="text" value="<?=$objResult["fname"];?>"> </td>
</tr>
<tr>
<td width="150" align="right"><label for="lname">lname</label></td>
<td><input name="lname" maxlength="30" type="text" value=" <?=$objResult["lname"];?>"></td>
</tr>
<tr>
<td width="150" align="right"><label for="tel">tel</label></td>
<td><input name="tel" maxlength="15" type="text" value="<?=$objResult["tel"];?>"></td>
</tr>
<tr>
<td width="150" align="right"><label for="adress1">adress1</label></td>
<td><input name="adress1" maxlength="30" type="text" value="<?=$objResult["adress1"];?>"></td>
</tr>
<tr>
<td width="150" align="right"><label for="adress2">adress2</label></td>
<td><input name="adress2" maxlength="30" type="text" value="<?=$objResult["adress2"];?>"></td>
</tr>
<tr>
<td width="150" align="right"><label for="pcode">pcode</label></td>
<td><input name="pcode" maxlength="8" type="text" value="<?=$objResult["pcode"];?>"> </td>
</tr>
<tr>
<td width="150" align="right"><label for="email">email</label></td>
<td><input name="email" maxlength="30" type="text" value="<?=$objResult["email"];?>"></td>
</tr>
<tr>
<td width="150" align="right"><label for="lastcontactdate">lastcontactdate</label></td>
<td><input name="lastcontactdate" maxlength="30" type="text" value="<?=$objResult["lastcontactdate"];?>"></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="" type="submit" value="Update"></td>
</tr>
</table>
</form>
updated.php
<?php
header('Refresh: 5; URL=view11.php');
$objConnect = mysql_connect("localhost","root","2fudge") or die(mysql_error());
$objDB = mysql_select_db("teldirdb");
$id = $_REQUEST['id'];
$fname = trim(mysql_real_escape_string($_POST["fname"]));
$lname = trim(mysql_real_escape_string($_POST["lname"]));
$tel = trim(mysql_real_escape_string($_POST["tel"]));
$adress1 = trim(mysql_real_escape_string($_POST["adress1"]));
$adress2 = trim(mysql_real_escape_string($_POST["adress2"]));
$pcode = trim(mysql_real_escape_string($_POST["pcode"]));
$email = trim(mysql_real_escape_string($_POST["email"]));
$lastcontactdate = trim(mysql_real_escape_string($_POST["lastcontactdate"]));
$rsUpdate = mysql_query("UPDATE teldir
SET fname = '$fname', lname = '$lname', tel = '$tel', adress1 = '$adress1', adress2 = '$adress2', pcode = '$pcode', email = '$email', lastcontactdate = '$lastcontactdate'
WHERE id = '$id' ");
if($rsUpdate) { echo "Successfully updated"; } else { die('Invalid query: '.mysql_error()); }
爲什麼沒有人用準備好的發言?這太痛苦了。 – Kurt 2012-08-02 09:28:08
您有[主要安全漏洞](http://bobby-tables.com/),並且正在使用'mysql_ *'函數,這些函數在PHP手冊中被標記爲已棄用。 – Quentin 2012-08-02 09:28:20
'回聲'您的查詢,並檢查它是你想要的。 – Jocelyn 2012-08-02 09:29:29