简体   繁体   English

如何使用PHP和MySQL使用where子句更新表?

[英]How to update a table using a where clause using PHP and MySQL?

I'm working on a website that is supposed to pass data from a form on the page and use PHP to update a students table on the server. 我正在一个网站上工作,该网站应该从页面上的表单传递数据并使用PHP更新服务器上的students表。 The condition I'm trying to work on now is Modifying data, so it should be able to update data on the server, but it does not... 我现在要处理的条件是“修改数据”,因此它应该能够更新服务器上的数据,但不能...

Any help or advice you guys can offer will be greatly appreciated. 你们提供的任何帮助或建议将不胜感激。 Thanks in advance. 提前致谢。

Here is my code: 这是我的代码:

<?php

date_default_timezone_set('America/New_York');

$connection = mysql_connect("hostaddress","DBname","password");

            // Check connection
if (!$connection)
{
            echo "Connection failed: " . mysql_connect_error();
}
            else
{
                //select table
                mysql_select_db("DBname");
                echo "Database Found! <br>";

                $query = "UPDATE students
                          SET firstName = $_POST[firstName],
                              lastName = $_POST[lastName]
                          WHERE StudentID = $_POST[StudentID]";

                $res = mysql_query($query);

                if ($res)
                {
                   echo "<p>Record Updated<p>";
                }   
                else
                {
                   echo "Problem updating record. MySQL Error: " . mysql_error();
                }
            }

            mysql_close($connection);
        ?>

I keep getting this error: 我不断收到此错误:

Database Found! 找到数据库! Problem updating record. 问题更新记录。 MySQL Error: You have an error in your SQL syntax; MySQL错误:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ' lastName = , WHERE Stud' at line 2 检查与您的MySQL服务器版本相对应的手册,以在第2行的'lastName =,WHERE Stud'附近使用正确的语法

PS: I already have the students table created with some data. PS:我已经用一些数据创建了students表。 I can access the table and modify it using the terminal and an identical mysql query like the one above but still no luck. 我可以使用终端和相同的mysql查询(如上述查询)访问表并对其进行修改,但是还是没有运气。 Also, I have changed the host address, database name, and password in my code on purpose (just in case someone asks). 另外,我有意更改了代码中的主机地址,数据库名称和密码(以防万一有人询问)。

Ouput of echo $query echo $query输出

UPDATE fall14_bmora013.students SET firstName = , lastName = 
WHERE StudentID = 890

Obvious, you have a syntax error (an extra , before WHERE ) in your UPDATE statement as pointed below 显然,您在UPDATE语句中有语法错误(在WHERE之前,是一个额外的语法),如下所示

UPDATE students SET firstName = $_POST[firstName],
lastName = $_POST[lastName], <-- remove this extra comma
WHERE StudentID = $_POST[StudentID] 

EDIT: 编辑:

Check whether your form element really named as firstName and lastName ? 检查您的表单元素是否真的命名为firstNamelastName check the spelling. 检查拼写。 I doubt the names are different than what you are referring in code. 我怀疑名称与您在代码中引用的名称是否不同。

You wrote it yourself: MySQL Error: You have an error in your SQL syntax; 您自己编写的:MySQL错误:SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ' lastName = , WHERE Stud' at line 2 检查与您的MySQL服务器版本相对应的手册,以在第2行的'lastName =,WHERE Stud'附近使用正确的语法

Print the query string, it's not what you expect it to be. 打印查询字符串,它不是您期望的那样。

Ps You should probably escape your inputs in order to avoid sql-injectiion. Ps您可能应该转义您的输入,以避免sql-injectiion。

Also, please note you should not use mysql_ functions anymore. 另外,请注意,您不应再使用mysql_函数。 read the warning in the following page: http://php.net/manual/en/function.mysql-query.php 请阅读以下页面中的警告: http : //php.net/manual/en/function.mysql-query.php

Once try this... 一旦尝试这个...

$query = "UPDATE students SET firstName ='".$_POST['firstName']."', lastName ='".$_POST['lastName']."' WHERE StudentID = '".$_POST['StudentID']."'"; $ query =“更新学生SET firstName ='”。$ _ POST ['firstName']。“', lastName ='”。$ _ POST ['lastName']。“'WHERE StudentID ='”。$ _ POST ['StudentID' ]。“'”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM