into而不向数据库中添加特定条目的行的教程问题

mjqavswn  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(404)

下面是视频教程“how to create a database website with php and mysql 07-add-in input form”的最后一部分:
https://www.youtube.com/watch?v=mgig00d1xzc&list=plhpyefl5u-i0zeadf0iplyvm8zoknz70r&index=7
最后是我的代码,用于在数据库中插入新的\u jokes.php脚本的部分(到目前为止,本系列的所有内容都可以很好地工作)
基本上,我得到的似乎是经典的“插入到”不工作,虽然我所有的语法看起来正确。我是不是漏掉了什么明显的东西?我没有得到错误,只是行没有添加。

<?php

include "db_connect.php";

$new_joke_question = $_GET["newjoke"];
$new_joke_answer = $_GET["newanswer"];

// Search the database for the word chicken
echo "<h2>Trying to add a new joke and answer: $new_joke_question  
$new_joke_answer </h2>";

$sql = "INSERT INTO Jokes_table (JokeID, Joke_question, Joke_answer) VALUES 
(NULL, '$new_joke_question', '$new_joke_answer' )";
$result = $mysqli->query($sql);

include "search_all_jokes.php";
?>
<a href="index.php">Return to the main page</a>

以下是所需的db\u connect.php代码:

<?php

// four variables to connect the database
$host = "localhost";
$username = "root";
$user_pass = "usbw";
$database = "test";

// create a database connection instance
$mysqli = new mysqli($host, $username, $user_pass, $database);

?>

这里是search\u all\u jokes.php(它有小错误检查):

// if there are any values in the table, select them one at a time 
if ($mysqli->connect_errno) {
    echo "Connection to MySQL failed: (" . $mysqli->connect_errno . ") " . 
$mysqli->connect_error;
} 
echo $mysqli->host_info . "<br>";
$sql = "SELECT JokeID, Joke_question, Joke_answer FROM Jokes_table";
$result = $mysqli->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "JokeID: " . $row["JokeID"]. " - Joke_question: " . 
$row["Joke_question"]. " " . $row["Joke_answer"]. "<br>";
    }
} else {
    echo "0 results";
}

?>

下面是在myphpadmin中查看的表结构屏幕截图:

我在new_jokes.php中添加了错误捕获,灵感来自于这个堆栈溢出帖子:insert into syntax error
并得到以下错误:错误:您的sql语法有错误;检查与您的mysql服务器版本对应的手册,以通过tcp/ip在第1localhost行的“t jump.”)附近使用正确的语法

kyvafyod

kyvafyod1#

谢谢大家的帮助!语法真的能改变一切。我还将宣读事先准备好的发言,因为这样也可以避免这个问题。我找到了解决方案,通过添加mysqli real\u escape\u string此处引用的函数来清除提交到笑话表中的答案中的单个引号:
(袋鼠能跳得比帝国大厦还高吗?当然,帝国大厦不能跳。)
如文档@miken32中的注解所示,这里的注解是:“但是如果$val1或$val2包含单引号,那么您的sql就错了。因此,在sql中使用它之前,需要对其进行转义;这就是mysql\u real\u escape\u string的用途(尽管事先准备好的声明更好。)”
但现在我找到的YouTube教程第7部分的代码可以工作了,并将其添加到数据库表的一行中,然后在下一个网页上显示完整的新表。我花了很长一段时间在黑暗中拍摄,而答案却相当简单。再次特别感谢@miken32为我指明了正确的方向。
下面是我完成的代码,它至少可以实现教程的目标:

<?php

include "db_connect.php";

$new_joke_question = $_GET["newjoke"];
$new_joke_answer = $_GET["newanswer"];

$new_joke_question = $mysqli->real_escape_string($new_joke_question);
$new_joke_answer = $mysqli->real_escape_string($new_joke_answer);

// Search the database for the word chicken
echo "<h2>Trying to add a new joke and answer: $new_joke_question  $new_joke_answer 
</h2>";

if ($mysqli->connect_errno) {
    echo "Connection to MySQL failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} 
echo $mysqli->host_info . "<br>";
$sql = "INSERT INTO Jokes_table (JokeID, Joke_question, Joke_answer) VALUES ('   ', 
'$new_joke_question', '$new_joke_answer' )";
$result = $mysqli->query($sql);

if ($mysqli->query($sql) === TRUE) {
  echo 'users entry saved successfully';
}   
else {
  echo 'Error: '. $mysqli->error .'<br>';
}

include "search_all_jokes.php";
?>
<a href="index.php">Return to the main page</a>

相关问题