2012-06-19 53 views
0

我正在構建一個管理員用戶可以更新客戶信息的頁面。我首先查詢客戶信息並將其顯示在文本字段中。然後我可以輸入任何我想爲該客戶提供的新信息。當我在sql中執行更新命令時,它將我帶到更新成功的頁面,但是當我回顧數據庫時,客戶信息沒有改變。更新數據庫中的客戶信息不起作用

(顯示客戶信息頁)

<?php 


//Function to sanitize values received from the form. Prevents SQL injection 
function clean($str) { 
    $str = @trim($str); 
    if(get_magic_quotes_gpc()) { 
     $str = stripslashes($str); 
    } 
    return mysql_real_escape_string($str); 
} 

//define username variable and sanitize 
$username = clean($_POST['username']); 

//Run query for selected user and store in an array 
$result = mysql_query("select * from members where username='".$username."'"); 
$row = mysql_fetch_array($result); 

//display all clients information in a form to edit 
echo '<h1>'.$username.'</h1>'; 
echo '<form name="update-client" action="update-client.php" />'; 
echo '<table>'; 
echo '<tr><td>'; 
echo '<input type="hidden" name="member_id" value="'.$row['member_id'].'"'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Username: <input name="username" type="text" value="'.$username.'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Password: <input name="password" type="text" value="'.$row['password'].'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Business Name: <input name="bizname" type="text" value="'.$row['bizname'].'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Phone: <input name="phone" type="text" value="'.$row['phone'].'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Email: <input name="email" type="text" value="'.$row['email'].'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Website Address: <input name="url" type="text" value="'.$row['url'].'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Contact: <input name="contact" type="text" value="'.$row['contact'].'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Notes: <input name="notes" type="text" value="'.$row['notes'].'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo 'Sales Representative: <input name="sales_rep" type="text" value="'.$row['sales_rep'].'" />'; 
echo '</td></tr>'; 
echo '<tr><td>'; 
echo '<input name="submit" type="submit" value="Edit" />'; 
echo '</td></tr>'; 
echo '</table>'; 
echo '</form>'; 


?> 

更新client.php

<?php 


//Function to sanitize values received from the form. Prevents SQL injection 
function clean($str) { 
    $str = @trim($str); 
    if(get_magic_quotes_gpc()) { 
     $str = stripslashes($str); 
    } 
    return mysql_real_escape_string($str); 
} 

//define variables 
$member_id = $_POST['member_id']; 
$username = $_POST['username']; 
$password = $_POST['password']; 
$bizname = $_POST['bizname']; 
$phone = $_POST['phone']; 
$email = $_POST['email']; 
$url = $_POST['url']; 
$contact = $_POST['contact']; 
$notes = $_POST['notes']; 
$sales_rep = $_POST['sales_rep']; 
$member_type = $_POST['member_type']; 

//encrypt the password 
$password = md5($password); 

//Check for duplicate username 
if($username != '') { 
    $qry_uname = "SELECT * FROM members WHERE username='".$username."'"; 
    $result = mysql_query($qry_uname); 
    if($result) { 
     if(mysql_num_rows($result) > 0) { 
      $errmsg_arr[] = 'Username already in use'; 
      $errflag = true; 
     } 
     @mysql_free_result($result); 
    } 
    else { 
     die("Query failed1"); 
    } 
} 

//update customers information 
$qry = "update members set username='".$username."',password='".$password."',bizname='".$bizname."',phone='".$phone."',email='".$email."',url='".$url."',contact='".$contact."',notes='".$notes."',sales_rep='".$sales_rep."',member_type='".$member_type."' where member_id='".$member_id."'"; 

//Check whether the query was successful or not 
/*if(mysql_query($qry)) { 
    header("location: update-success.php"); 
exit(); 
    } 
else { 
    die("Query failed2"); 
    }*/ 

echo $qry; 

?> 

編輯-client.php有我的代碼有問題嗎?我使用的是Apache服務器

+0

首先,你的清潔功能是可怕的。 (請參閱http://stackoverflow.com/a/7810880/362536)在這種情況下它將適用於您,但請注意。我強烈建議使用準備好的查詢。其次,不要用'@'隱藏錯誤。 – Brad

+0

echo $ qry返回什麼? – 2012-06-19 20:34:41

+0

歡迎來到SO。請使用適當的標籤。 'sql'標籤應該是'mysql'標籤。請記住這一點在下次。享受這樣的時間。 –

回答

0

//檢查是否有重複的用戶名

檢查與用戶名和ID,因爲如果不改變用戶名已經存在。