有人可以帮我弄清楚为什么在我的create_topic.php脚本中无法使用sql插入,但是在phpMyAdmin中手动输入时却可以正常工作吗?我正在尝试将数据插入表中,但是没有创建任何行。 这是PHP:
else { $ query =“开始工作;”; $ result = mysqli_query($ conn,$ query);
if (!$result) { echo '1: An error occured while creating your topic. Please try again later.'; } else { $topic_subject = 'test'; $topic_cat = 1; $user_id = 1; $sql = "INSERT INTO topics(topic_subject, topic_date, topic_cat, topic_by) VALUES('topicsubject', NOW(), 1, 1);"; $result = mysqli_query($conn, $sql);
当我在php站点中运行此代码时,它不会插入数据库中。但是,当我将INSERT直接复制并粘贴到phpMyAdmin的SQL终端中时,它可以工作。但是,当我手动然后将其插入时,我可以看到自从我在php中尝试失败以来该行的AUTO_INCREMENT已经增加,因此我知道该脚本正在发送到数据库,由于某种原因它没有成功。
我还有其他的php脚本,例如signup.php和create_cat.php都可以使用,所以我知道我的$ conn已正确设置。 我还尝试运行mysqli_error($ conn),但未返回任何内容。
如果需要的话,这里是完整的代码。
include 'connect.php';
include 'header.php';
echo '<h2>Create a topic</h2>';
if ($_SESSION['signed_in'] == false) {
echo 'Sorry, you have to be <a href="signin.php">signed in</a> to create a topic.';
}
else {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$sql = "SELECT cat_id, cat_name, cat_description FROM categories";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo 'Error while selecting from database. Please try again later.';
}
else {
if (mysqli_num_rows($result) == 0) {
if ($_SESSION['user_level'] == 1) {
echo 'You have not created categories yet';
}
else {
echo 'Before you can post a topic, you must wait for an admin to create some category';
}
}
else {
echo '<form method="post" action="">
Subject: <input type="text" name="topic_subject">
Category:';
echo '<select name="topic_cat">';
while($row = mysqli_fetch_assoc($result)) {
echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
}
echo '</select>';
echo 'Message: <textarea name="post_content" /></textarea>
<input type="submit" value="create topic" />
</form>';
}
}
}
else {
$query = "BEGIN WORK;";
$result = mysqli_query($conn, $query);
if (!$result) {
echo '1: An error occured while creating your topic. Please try again later.';
}
else {
$topic_subject = 'test';
$topic_cat = 1;
$user_id = 1;
$sql = "INSERT INTO topics(topic_subject, topic_date, topic_cat, topic_by) VALUES('topicsubject', NOW(), 1, 1);";
$result = mysqli_query($conn, $sql);