我正在尝试使用 PHP 将数据添加到 MySql 数据库中。
代码:
<?php
session_start();
include('config.php');
if(isset($_POST['Submit'])) {
$city = $_POST['city'];
$from_station = $_POST['from_station'];
$from_date = $_POST['from_date'];
// Prepare and execute the SQL query
$sql = "INSERT INTO journeys (city, from_station, from_date) VALUES ('$city', '$from_station', '$from_date')";
$result = $conn->query($sql);
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html lang="pt_PT">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IR PLANNER</title>
<link rel="stylesheet" href="Styles\general_styles.css">
<link rel="stylesheet" href="Styles\citymanager_styles.css">
</head>
<body>
<form class="modal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<label for="city">City:</label>
<input type="text" name="city" id="city">
<label for="from_station">From Station:</label>
<input type="text" name="from_station" id="from_station">
<label for="from_date">From Date:</label>
<input type="date" name="from_date" id="from_date">
<input type="submit" value="Submit" id="Submit">
</form>
</body>
</html>
按下提交按钮时,不会显示错误。然而,当检查数据库表时,什么也没有出现。
我已阅读文章,并且 sql 连接(config.php)工作正常(在其他页面和文件中工作)并且 sql 查询也很好。
有人遇到这个问题吗?
您需要在提交按钮中传递 name 属性,直到您无法从中获取任何值。在您的条件下,它总是返回 false。所以不会更新任何内容,也不会生成错误。只需更新这里是更新的代码。
<?php
session_start();
include('config.php');
if(isset($_POST['Submit'])) {
$city = $_POST['city'];
$from_station = $_POST['from_station'];
$from_date = $_POST['from_date'];
// Prepare and execute the SQL query
$sql = "INSERT INTO journeys (city, from_station, from_date) VALUES ('$city', '$from_station', '$from_date')";
$result = $conn->query($sql);
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html lang="pt_PT">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IR PLANNER</title>
<link rel="stylesheet" href="Styles\general_styles.css">
<link rel="stylesheet" href="Styles\citymanager_styles.css">
</head>
<body>
<form class="modal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<label for="city">City:</label>
<input type="text" name="city" id="city">
<label for="from_station">From Station:</label>
<input type="text" name="from_station" id="from_station">
<label for="from_date">From Date:</label>
<input type="date" name="from_date" id="from_date">
<input type="submit" name="Submit" value="Submit" id="Submit">
</form>
</body>
</html>
您可以采取一些措施来分析问题/优化代码。
例如:
if(isset($_POST['Submit'])) {
var_dump(__LINE__);
现在运行代码以查看到达了哪些行。
此代码:
<input type="submit" value="Submit" id="Submit">
需要
name
属性,才能作为数据发送到服务器:
<input type="submit" name="Submit" value="Submit" id="Submit">
代替当前的插入代码,制作它
$stmt = $conn->prepare("INSERT INTO journeys (city, from_station, from_date) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $city, $from_station, $from_date);
$stmt->execute();
这样就不会发生SQL注入了。
在这些线上:
$result = $conn->query($sql);
if (mysqli_query($conn, $sql)) {
替换为第 3 点的代码。
<?php
session_start();
include('config.php');
if(isset($_POST['Submit'])) {
$city = $_POST['city'];
$from_station = $_POST['from_station'];
$from_date = $_POST['from_date'];
// Prepare and execute the SQL query
$stmt = $conn->prepare("INSERT INTO journeys (city, from_station, from_date) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $city, $from_station, $from_date);
$stmt->execute();
}
?>
<!DOCTYPE html>
<html lang="pt_PT">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IR PLANNER</title>
<link rel="stylesheet" href="Styles\general_styles.css">
<link rel="stylesheet" href="Styles\citymanager_styles.css">
</head>
<body>
<form class="modal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<label for="city">City:</label>
<input type="text" name="city" id="city">
<label for="from_station">From Station:</label>
<input type="text" name="from_station" id="from_station">
<label for="from_date">From Date:</label>
<input type="date" name="from_date" id="from_date">
<input type="submit" name="Submit" value="Submit" id="Submit">
</form>
</body>
</html>