用PHP更新MySQL查询
我试图覆盖MySQL中的当前数据,以便能够更新所有内容。 我是新手,我看不到以下代码有任何错误: PHP代码:用PHP更新MySQL查询,php,mysql,sql,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){
<?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:";
}
?>
您的表名为“users”,但您正在对firstname运行更新。将其更改为:
UPDATE users SET surname...
那就做吧
$exists = mysql_query ("SELECT * FROM users WHERE firstname='" . $firstname . "'")
以隔离您是否具有该变量的值。我建议出于测试目的打印查询字符串。你真的应该读到这是更新查询错误我讨厌人们看不起人我说我是新手而不是die(“查询无法连接”)
trydie(mysql\u error())
我得到了“未选择数据库”现在@ShankarDamodaran@ShankarDamodaran这确实奏效了,非常感谢:我仍然得到了同样的结果。有人能不能给我一个更好的方式呢?我不介意从头开始
$exists = mysql_query ("SELECT * FROM users WHERE firstname='" . $firstname . "'")