用PHP更新MySQL查询

时间:2014-02-16 13:47:20

标签: php mysql sql

我试图覆盖MySQL中的当前数据,以便能够更新所有内容。 我是新手,我没有看到以下代码的任何错误:

PHP代码:

<?php
// see if the form has been completed
if (isset($_POST['submit'])){
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];

    if($firstname && $surname){
        // connect to the server
        include_once("php_includes/db_conx.php");

        // check if that user exist 
        $exists = mysql_query ("SELECT * FROM users WHERE firstname='$firstname'") or die ("the query could not be connected");
        if (mysql_num_rows ($exists) != 0) {
            // update the description in the database
            mysql_query("UPDATE firstname SET surname='$surname' WHERE firstname='$firstname'") or die ("update could not be applied");
            echo "successful";
        } else echo "the name does not edist";  
    } else echo "you need to enter both of the fields try again:";
}
?>  

我得到的错误是

  

无法连接查询

但我尝试了查询,很好。

HTML:

<html>
<head>
<title>update MySql form</title>
</head>
<body>

<div id="pageMiddle">  

    <form action="user1.php" method="POST">
        <div>
        <p>First Name: <input type="text" name="firstname" id="firstname" ></p>
        <p>Surname: <input type="text" name="surname" id="surname"></p>
        <p><input type="submit" name="submit" id="submit" value="Update Description"></p>
        </div>
    </form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的表名为“用户”,但您在名字上运行UPDATE。将其更改为:

UPDATE users SET surname...

然后做

$exists = mysql_query ("SELECT * FROM users WHERE firstname='" . $firstname . "'")

以便隔离您是否对该变量有价值。我建议打印查询字符串以进行测试。