我的用户提交的数据不会进入我的数据库,我有一个表单,在其中插入他们的信息,然后它应该只插入数据库。我可以很好地连接到我的数据库并获取信息,它只是不插入它?
表格:
<div class="form">
<form id="addsteps" action="add.php" method="post" autocomplete="off">
<div class="field-wrap">
<input type="date" required autocomplete="off" name="date" style="color: black; text-align: center;"/>
</div>
<div class="field-wrap">
<input type="number" required autocomplete="off" name="steps" style="color: black;"/>
</div>
<button type="submit" class="button button-block" name="submit" />Submit</button>
</form>
</div>
代码:
<?php include '../db_connection.php';
session_start();
$unique_id = $_SESSION['unique_id'];
$_SESSION['date'] = $_POST['date'];
$_SESSION['steps'] = $_POST['steps'];
$_POST['date'];
$_POST['steps'];
$date = $link->escape_string($_POST['date']);
$steps = $link->escape_string($_POST['steps']);
$sql = "INSERT INTO step_count (unique_id, date, steps) "
. "VALUES ('$unique_id','$date','$steps')";
?>
它可以很好地获取数据,当使用var_dump时我得到了
string(81) "INSERT INTO step_count (unique_id, date, steps) VALUES ('1','2017-12-16','12345')"
所以我只是想知道我哪里出错了,因为它不会进入数据库?
这是db_connection文件:
<?php
$link = mysqli_connect("localhost", "root", "", "repsandsteps");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
} ?>
答案 0 :(得分:1)
你应该像这样执行查询。
<?php include '../db_connection.php';
session_start();
$unique_id = $_SESSION['unique_id'];
$_SESSION['date'] = $_POST['date'];
$_SESSION['steps'] = $_POST['steps'];
$date = mysqli_real_escape_string($link, $_POST['date']);
$steps = mysqli_real_escape_string($link, $_POST['steps']);
$sql = "INSERT INTO step_count (unique_id, date, steps) "
. "VALUES ('$unique_id','$date','$steps')";
mysqli_query($link,$sql);