我是php的初学者,我正在尝试更新特定行但不起作用。我需要你的帮助: 的 update.php DIR。 '/db_connect.php'; //连接到db $ db = new DB_CONNECT();
if (isset($_POST['id'])) {
$id = $_POST["id"];
$location = $_POST["location"];
$sql=mysql_query("UPDATE citizenalert set location={'$location'} WHERE id ={'$id'}");
}
?>
db_config.php
<?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "ikirenga"); // database name
define('DB_SERVER', "localhost"); // db server
?>
db_connect.php
<?php
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
}
}
?>
request_update.php
<!doctype html>
<html lang="en">
<body>
<form name="updates" method="post" action="update.php" >
<label>User location</label> <input id="location" type="text" name="location" > <br><br>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
答案 0 :(得分:0)
请使用以下语法
<?php
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
if (isset($_POST['id'])) {
$id = $_POST["id"];
$location = $_POST["location"];
$sql = mysql_query(" UPDATE citizenalert set location = " . $location . " WHERE id = " . $id . ");
}
感谢。
答案 1 :(得分:0)
更改您的SQL查询,如下所示
$sql=mysql_query("UPDATE citizenalert set location='{$location}' WHERE id = '{$id}'");
答案 2 :(得分:0)
如果我测试你的代码:
<?php
$id = 1;
$location = "test";
echo $sql="UPDATE citizenalert set location={'$location'} WHERE id ={'$id'}";
?>
提供此查询:
UPDATE citizenalert set location={'test'} WHERE id ={'1'}
请在这里查看两个有问题的值。
<强>问题:强>
您在引号之外使用{}
。你需要在单引号中使用它。
使用相同示例修改查询:
echo $sql="UPDATE citizenalert set location='{$location}' WHERE id ='{$id}'";
<强>结果:强>
UPDATE citizenalert set location='test' WHERE id ='1'
旁注:
mysqli_*
或PDO
,因为mysql_*
已弃用并已在 PHP 7 中关闭。准备声明的其他参考资料: php mysql bind-param, how to prepare statement for update query