[英]Update query in php not recognizing variables
我試圖使用mysql查詢更新客戶信息。 變量已在此處定義:
$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加密只是為了看看它是否有效並且它們將具有值,但顯然我只想為密碼執行此操作。
編輯:這是edit-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>
您總是希望在限制花費調試時間時檢查值/錯誤。
例如,做這樣的事情會更好:
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字符串中使用雙引號允許您直接引用變量,而如果您使用了單引號,則必須結束字符串並插入變量。
更改:
echo '<form name="update-client" action="update-client.php" />';
至:
echo '<form name="update-client" action="update-client.php" method="post"/>';
你忘了method="post"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.