2012-06-19 103 views
0

我正在嘗試使用mysql查詢更新客戶信息。這些變量在這裏被定義爲:在php中更新查詢無法識別變量

$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']; 

$password = md5($password); 

當我運行下面的查詢,在數據庫沒有更新

$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."'"; 

我呼應了$qry和結果如下:

update members set 
username='',password='d41d8cd98f00b204e9800998ecf8427e',bizname='',phone='', 
email='',url='',contact='',notes='',sales_rep='',member_type='' 
where member_id='' 

有沒有人有一個想法,爲什麼只有$password變量有一個值?我曾嘗試在其他變量上使用md5加密,只是爲了查看這是否可行,他們將有值,但顯然我只想爲密碼做這件事。

編輯:這是編輯client.php形式

<?php 
require_once('auth.php'); 
require_once('config.php'); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; /> 
<title>Untitled Document</title> 
</head> 

<body> 
<?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>'; 


?> 
</body> 
</html> 
+5

什麼是張貼的HTML代碼? –

+0

您可能在'$ _POST'中有一個拼寫錯誤 –

+4

整個'$ _POST'似乎是空的,也就是空字符串的md5。 – Wrikken

回答

1

變化:

echo '<form name="update-client" action="update-client.php" />'; 

要:

echo '<form name="update-client" action="update-client.php" method="post"/>'; 

你忘method="post"

1

你總是要檢查值/錯誤,你去限制你必須花費的調試時間。

例如,它會更好的做法是做這樣的事情:

if (isset ($_POST['member_id']) && !empty ($_POST['member_id'])) { 
    $member_id = $_POST['member_id']; 
} else { 
    echo 'Error: member_id not provided!'; 
} 

而且你會想要做的,對於每一個領域。另外,你希望在你的代碼中建立一些驗證(只檢查數字,有效的格式,sql注入等),並且只有在輸入完成,有效和安全的情況下才能使用實際的SQL。

這只是非常基礎,但從腳本開始,你會很快找出問題所在。

但更具體地說,您需要發佈您的HTML表單的內容,以便我們可以幫助您。

編輯:此外 - 這是一個好主意,開始格式化SQL查詢的方式,使他們更容易閱讀/調試。你舉個例子,我會這樣寫:

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

你還會看到我沒有使用「。$ var」。因爲在PHP字符串中使用雙引號可以直接引用變量,而如果您使用了單引號,則必須結束字符串並插入變量。

+0

感謝您提供有用的提示! – ZeLoubs