这个问题我已经有几天了,我似乎无法看到问题所在。我已经通过多种方式查看了这些函数,甚至查找了php网站,但我还是无法解决这个问题。这是我遇到麻烦的代码:
$sql = mysqli_query($db_connect, "INSERT INTO forum_post(post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) VALUES($log_username, $log_id, now(), 'a', '$forum_section_title', '$forum_section_id', '$post_title', '$post_body')");
$this_id = mysqli_insert_id($db_connect);
header("location: ../view_thread.php?id=$this_id");
...编辑
所以我可以展示我是如何处理错误处理的(相反我可以看到消息):
$sql = mysqli_query($db_connect, "INSERT INTO forum_post(post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) VALUES('$log_username', '$log_id', now(), 'a', '$forum_section_title', '$forum_section_id', '$post_title', '$post_body')");
if(mysqli_error($db_connect)) {
$message = mysqli_error($db_connect);
header("location: ../message.php?msg=$message");
}
$this_id = mysqli_insert_id($db_connect);
header("location: ../view_thread.php?id=$this_id");
exit();
答案 0 :(得分:0)
我会建议使用PDO,因为它更安全且更直接。并且因为我不会让你犯错误并且有问题。
这是您在PDO中的代码。测试一下:
try {
// Prepare our connection string
$con = new PDO("mysql:host=".HOST.";dbname=".DBNAME, USER, PASS);
}
catch(PDOException $e) {
// Connection error jumps here
echo $e->getMessage();
}
// Prepare our Insert Query
$query = $con->prepare("INSERT INTO forum_post(post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) VALUES(:post_author, :post_author_id, :date_time, :type, :section_title, :section_id, :thread_title, :post_body)");
// Get our data aligned in an array.
$data = array('post_author' => $log_username, 'post_author_id' => $log_id, 'date_time' => now(), 'type' => 'a', 'section_title' => $forum_section_title, 'section_id' => $forum_section_id, 'thread_title' => $post_title, 'post_body' => $post_body);
try {
// Try to execute our Insert Query with the given Data
$query->execute($data);
}
catch(PDOException $e) {
// Insert error jumps here
echo $e->getMessage();
}
$this_id = $con->lastInsertId();
header("location: ../view_thread.php?id=$this_id");
请务必将代码中提到的占位符替换为主机,数据库,用户和密码...
答案 1 :(得分:0)
我不确定您是如何使用mysqli_error($db_connect);
的,因为在您这样做时会收到错误。
一个问题是您没有引用字符串值:
$sql = mysqli_query($db_connect, "INSERT INTO forum_post(post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body)
VALUES($log_username, $log_id, now(), 'a', '$forum_section_title', '$forum_section_id', '$post_title', '$post_body')");
// ^^^^^^^^^^^^^ here ^^^^^ and here
// (assuming $log_id is an integer...)
但我同意@MiroMarkarian你应该使用预备语句(也可以在mysqli中)。
答案 2 :(得分:0)
检查您的php安装是否设置为报告错误。然后死于不成功的MySQL查询执行并检查你得到的错误。
要启用错误报告,请将以下行放在php文件的顶部:
<?php
error_reporting(E_ALL ^ E_NOTICE);