2016-12-05 37 views
1

我已經在MySQL中下列:PHP和MySQL的:當我編輯和更新我的數據庫只有第一個欄將更新

firstname, lastname,age,email,phone 

當我更新說我有以下幾點:

John Doe 23 [email protected] 5555555555 

但是當我想編輯的任何字段,我提交它出來到:

Mike 0 

和多數民衆贊成它。有任何想法嗎?

我的繼承人edit.php

<html> 
<body> 
<table> 
<?php 
include 'db.inc.php'; 
// Connect to MySQL DBMS 
if (!($connect = @ mysql_connect($hostName, $username, 
    $password))){ 
    showerror(); 
} 
// Use the food database 
if (!mysql_select_db($databaseName, $connect)){ 
    showerror(); 

} 
$uid=$_GET[id]; 
$query=mysql_query("SELECT * FROM contact WHERE id='$uid'"); 
$row=mysql_fetch_array($query); 

?> 
<form method ="post" action="update.php"> 
<table> 
<tr> 
<td><input type="hidden" name="id" value="<?php echo "$row[id]"?>"></td> 
</tr> 

<tr> 
<td>First Name:</td> 
<td><input type="text" name="ud_firstname" value="<?php echo   "$row[ud_firstname]"?>"></td> 
</tr> 

<tr> 
<td>Last Name:</td> 
<td><input type="text" name="ud_lastname" value="<?php echo"$row[ud_lastname]"?>"></td> 
</tr> 

<tr> 
<td>Age:</td> 
<td><input type="text" name="age" value="<?php echo"$row[ud_age]"?>"></td> 
</tr> 

<tr> 
<td>Email:</td> 
<td><input type="text" name="email" value="<?php echo "$row[ud_email]" ?>"> </td> 
</tr> 

<tr> 
<td>Phone Number:</td> 
<td><input type="text" name="phone" value="<?php echo "$row[ud_phone]" ?>"> </td> 
</tr> 
<input type = "submit"> 
</table> 
</form> 
</body> 
</html> 

,然後這是我update.php

include 'db.inc.php'; 
// Connect to MySQL DBMS 
if (!($connect = @ mysql_connect($hostName, $username, 
    $password))){ 
    showerror(); 
    } 

if (!mysql_select_db($databaseName, $connect)){ 
    showerror(); 

} 
$uid =$_POST["id"]; 


$ud_firstname = mysql_real_escape_string($_POST["ud_firstname"]); 
$ud_lastnamename = mysql_real_escape_string($_POST["ud_lastname"]); 
$ud_age = mysql_real_escape_string($_POST["ud_age"]); 
$ud_email = mysql_real_escape_string($_POST["ud_email"]); 
$ud_phone = mysql_real_escape_string($_POST["ud_phone"]); 


mysql_query("UPDATE contact 
    SET firstname = '$ud_firstname', lastname = '$ud_lastname', age = '$ud_age', email='$ud_email', phone='$ud_phone' 
    WHERE id=$uid"); 
if(mysql_affected_rows()>=1){ 
echo "<p>($id) Record Updated<p>"; 
header('location:view.php'); 
}else{ 
    echo "<p>($id) Not Updated<p>"; 
} 
?> 

任何想法?在先進的感謝

+1

我不清楚你描述的問題。錯誤的記錄正在更新嗎?正確的記錄是否更新爲錯誤的值?還有別的嗎? – David

+0

您的表單是否發佈了正確的值?放置'print_r($ _ POST); exit();''update.php'中的某處。 – LePhleg

+0

它的正確記錄和正確的值,但它只更新第一個名字的值,然後將所有其他值設置爲空。 – mdavies23

回答

2

替換下面你update.php

$ud_age = mysql_real_escape_string($_POST["age"]); 
$ud_email = mysql_real_escape_string($_POST["email"]); 
$ud_phone = mysql_real_escape_string($_POST["phone"]); 

OR

表單中的以下內容:

<tr> 
<td>Age:</td> 
<td><input type="text" name="ud_age" value="<?php echo"$row[ud_age]"?>"></td> 
</tr> 

<tr> 
<td>Email:</td> 
<td><input type="text" name="ud_email" value="<?php echo "$row[ud_email]" ?>"> </td> 
</tr> 

<tr> 
<td>Phone Number:</td> 
<td><input type="text" name="ud_phone" value="<?php echo "$row[ud_phone]" ?>"> </td> 
</tr> 

$_POST指數由上name的屬性來定義你的形成輸入字段。這兩個必須匹配。

相關問題