在 php 中更新查询无法识别变量


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 加密,只是为了看看这是否有效并且它们会有值,但显然我只想为密码执行此操作。

编辑

:这是编辑客户端.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>

更改:

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

自:

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

你忘了method="post"

在限制调试时间时,您始终需要检查值/错误。

例如,做这样的事情会更好:

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字符串中使用双引号允许您直接引用变量,而如果您使用单引号,则必须结束字符串并插入变量。