PHP 不执行更新

发布于 2024-12-10 19:22:08 字数 4930 浏览 0 评论 0原文

我创建了一个数据库,其中包含用户的名字、姓氏、电子邮件和临时密码。当用户第一次登录时,他们会看到一个个人资料,其中包含数据库中已有的信息以及他们必须填写的一些附加字段。单击“提交”后,表单应该会更新他们在数据库中的个人资料,但并没有。该数据库称为“用户”。有人可以告诉我我做错了什么吗?

 <?php
$testerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); // filter everything but numbers and letters
$tester = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["tester"]); // filter everything but numbers and letters
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]); // filter everything but numbers and letters

include "scripts/connect_to_mysql.php"; 
    $sql = mysql_query("SELECT * FROM users WHERE id='$testerID' AND username='$tester' AND password='$password' LIMIT 1"); // query the person
    $row = mysql_fetch_array($sql);
    $fname = $row['fname'];
    $lname = $row['lname'];
    $email = $row['email'];

$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount == 0) { // evaluate the count
     echo "Your login session data is not on record in the database.";
     exit();
}
?>
<!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; charset=utf-8" />
<title>Tester Home</title>
</head>

<body>
<table width="886">
  <tr>
    <td width="876"><h1>Welcome 
    <?php  
    echo $fname;
    ?> 
    to the Closed Beta</h1></td>
  </tr>
</table>
<p>&nbsp;</p>
<div id="content">
<?php
$date = getdate();
// Parse the form data and add inventory item to the system
if (isset($_POST['$new_password'])) {
    $new_email = mysql_real_escape_string($_POST['email']);
    $new_password = mysql_real_escape_string($_POST['new_password']);
    $phone_model = mysql_real_escape_string($_POST['phone_model']);
    $carrier = mysql_real_escape_string($_POST['carrier']);

    $sql_update = mysql_query("UPDATE users SET email='$new_email', password='$new_password', phone_model='$phone_model', carrier='$carrier' WHERE id='$testerID'");
}
if(is_null($test_start)){ 
    echo "
    <form action=\"index.php\" enctype=\"multipart/form-data\" name=\"myForm\" id=\"myform\" method=\"post\">
    <table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"6\">
      <tr>
        <td width=\"20%\" align=\"right\">ID: </td>
        <td width=\"80%\"><label>
          $testerID 
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Username: </td>
        <td><label>
          $tester
          </label></td>
      </tr>
      <tr>
        <td align=\"right\">First Name: </td>
        <td><label>
          $fname
          </label></td>
      </tr>
      <tr>
        <td align=\"right\">Last Name: </td>
        <td><label>
          $lname
          </label></td>
      </tr>
      <tr>
        <td align=\"right\">Email Address: </td>
        <td><label>
          <input type=\"text\" name=\"email\" id=\"email\" value=\"\"/>
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Old password: (the one you were assigned)</td>
        <td><label>
          <input type=\"text\" name=\"old_password\" id=\"old_password\" value=\"$password\"/>
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">New Password: </td>
        <td><label>
          <input type=\"text\" name=\"new_password\" id=\"newPassField\" />
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Confirm New Password: </td>
        <td><label>
          <input type=\"text\" name=\"confirm_password\" id=\"newPassField\" />
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Phone Model: </td>
        <td><label>
          <input type=\"text\" name=\"phone_model\" id=\"phone_model\" value=\"$phone_model\"/> (a 4 digit number)
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Carrier: </td>
        <td><label>
          <input type=\"text\" name=\"carrier\" id=\"carrier\" cols=\"64\" rows=\"5\" value=\"$carrier\"/>
          </label></td>
      </tr>
          <input type=\"submit\" name=\"button\" id=\"button\" value=\"Update\" />
    </table>
    </form>";   
}else{

}
?>
</div>
<p>&nbsp;</p>
</body>
</html>

I created a database with user's first name, last name, email, and temp password. When a user logs in for the first time they are shown a profile with the information already in the database as well as some additional fields they must fill in. On clicking submit the form should then update their profile in the database but it doesn't. The database is called 'users'. Could someone please tell me what I'm doing wrong?

 <?php
$testerID = preg_replace('#[^0-9]#i', '', $_SESSION["id"]); // filter everything but numbers and letters
$tester = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["tester"]); // filter everything but numbers and letters
$password = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION["password"]); // filter everything but numbers and letters

include "scripts/connect_to_mysql.php"; 
    $sql = mysql_query("SELECT * FROM users WHERE id='$testerID' AND username='$tester' AND password='$password' LIMIT 1"); // query the person
    $row = mysql_fetch_array($sql);
    $fname = $row['fname'];
    $lname = $row['lname'];
    $email = $row['email'];

$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount == 0) { // evaluate the count
     echo "Your login session data is not on record in the database.";
     exit();
}
?>
<!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; charset=utf-8" />
<title>Tester Home</title>
</head>

<body>
<table width="886">
  <tr>
    <td width="876"><h1>Welcome 
    <?php  
    echo $fname;
    ?> 
    to the Closed Beta</h1></td>
  </tr>
</table>
<p> </p>
<div id="content">
<?php
$date = getdate();
// Parse the form data and add inventory item to the system
if (isset($_POST['$new_password'])) {
    $new_email = mysql_real_escape_string($_POST['email']);
    $new_password = mysql_real_escape_string($_POST['new_password']);
    $phone_model = mysql_real_escape_string($_POST['phone_model']);
    $carrier = mysql_real_escape_string($_POST['carrier']);

    $sql_update = mysql_query("UPDATE users SET email='$new_email', password='$new_password', phone_model='$phone_model', carrier='$carrier' WHERE id='$testerID'");
}
if(is_null($test_start)){ 
    echo "
    <form action=\"index.php\" enctype=\"multipart/form-data\" name=\"myForm\" id=\"myform\" method=\"post\">
    <table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"6\">
      <tr>
        <td width=\"20%\" align=\"right\">ID: </td>
        <td width=\"80%\"><label>
          $testerID 
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Username: </td>
        <td><label>
          $tester
          </label></td>
      </tr>
      <tr>
        <td align=\"right\">First Name: </td>
        <td><label>
          $fname
          </label></td>
      </tr>
      <tr>
        <td align=\"right\">Last Name: </td>
        <td><label>
          $lname
          </label></td>
      </tr>
      <tr>
        <td align=\"right\">Email Address: </td>
        <td><label>
          <input type=\"text\" name=\"email\" id=\"email\" value=\"\"/>
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Old password: (the one you were assigned)</td>
        <td><label>
          <input type=\"text\" name=\"old_password\" id=\"old_password\" value=\"$password\"/>
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">New Password: </td>
        <td><label>
          <input type=\"text\" name=\"new_password\" id=\"newPassField\" />
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Confirm New Password: </td>
        <td><label>
          <input type=\"text\" name=\"confirm_password\" id=\"newPassField\" />
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Phone Model: </td>
        <td><label>
          <input type=\"text\" name=\"phone_model\" id=\"phone_model\" value=\"$phone_model\"/> (a 4 digit number)
        </label></td>
      </tr>
      <tr>
        <td align=\"right\">Carrier: </td>
        <td><label>
          <input type=\"text\" name=\"carrier\" id=\"carrier\" cols=\"64\" rows=\"5\" value=\"$carrier\"/>
          </label></td>
      </tr>
          <input type=\"submit\" name=\"button\" id=\"button\" value=\"Update\" />
    </table>
    </form>";   
}else{

}
?>
</div>
<p> </p>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

此生挚爱伱 2024-12-17 19:22:08

您拥有 isset($_POST['$new_password']) 而不是 isset($_POST['$new_password'])。注意添加的 $

You have isset($_POST['$new_password']) instead of isset($_POST['new_password']). Notice the added $

何以心动 2024-12-17 19:22:08

如果你的sql中有错误,那么找出错误的最好方法是添加错误检查代码,

or die(mysql_error());

我已将其添加到你的查询末尾

$sql_update = mysql_query("UPDATE users SET email='$new_email', password='$new_password', phone_model='$phone_model', carrier='$carrier' WHERE id='$testerID'") or die(mysql_error());

if there is an error in your sql then the best way to find out what it is, is to add in error checking code

or die(mysql_error());

i have added it to the end of your query here

$sql_update = mysql_query("UPDATE users SET email='$new_email', password='$new_password', phone_model='$phone_model', carrier='$carrier' WHERE id='$testerID'") or die(mysql_error());
云仙小弟 2024-12-17 19:22:08

您在哪里定义了用于数据库选择的mysql_select_db

另外,我不太习惯申请 UPDATE 选择,但如果您知道 ids,您可以将 INSERTDUPLICATE 值一起使用或为每个用户固定的类似列。类似于:

$query = "INSERT INTO users (_columns_) VALUES (_$columns_) ON DUPLICATE KEY UPDATE _column_='_$column_'";

使用以下命令更改您的列和表单中发布的值当然是 post 方法。如果需要,添加一个 WHERE 子句,即使认为这将是在数据库上查找的内容。

Where have you defined your mysql_select_db for the DB selection?

Also, I'm not quite used to apply for UPDATE selections, but you could use INSERT with a DUPLICATE value, if you know the ids or a similar column that is fixed for each user. Something like:

$query = "INSERT INTO users (_columns_) VALUES (_$columns_) ON DUPLICATE KEY UPDATE _column_='_$column_'";

Changing your columns and the posted values from the form with a post method, of course. Add there a WHERE clause if needed, even thought that would be something to look for on the db.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文