我已经尝试了一切。这是我的最后一招。下面的代码应该将从表单收到的信息提交给数据库,以便稍后显示。现在我知道查询是正确的但它没有运行。我确信这是某种错字,我只是看不到,但任何帮助都会非常感激。
if($_SERVER['REQUEST_METHOD']=="POST"){
$blog = $_POST['blog_post'];
$time = date('Y-m-d h-m-s');
$id = $_SESSION['author_id'];
if($blog != ""){
$query = "INSERT INTO `blog`(`date`,`post`,`author_id`) VALUES('$time','$blog',$id)";
$result = mysqli_query($link, $query);
$row = mysqli_affected_rows($link);
if($result = mysqli_query($link,$query)){
include 'header.php';
$url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url = rtrim($url, '/\\');
$url .= '/blog.php';
echo '<h3>Your blog has been posted. Go to <a style="color:white" href="'.$url.'">'.$url.'</a>';
include 'footer.php';
}else{
echo "that didn't work";
}
}else{
include 'header.php';
echo '<h1>You did not enter a blog post. Please try again.';
include 'footer.php';
}
}else{
redirect('index.php');
}
?>
答案 0 :(得分:0)
测试查询失败的最佳方法是显示错误:
if(!mysqli_query($link,$query)) { die(mysqli_error($link)); }
或
mysqli_query($link,$query) or die(mysqli_error($link));
在您拥有的每个mysqli_query
行中进行更改,并查看引发的错误。
http://php.net/manual/en/mysqli.error.php
至于我在下面的评论。您是否还调试连接到数据库(http://php.net/manual/en/mysqli.construct.php)的任何错误:
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
答案 1 :(得分:0)
然后,如果您的会话变量中没有 author_id ,则可能是您的整个脚本,因为我让它工作(请注意author_id的硬编码。
因为你的牧师没有在他们的ENTIRETY中包含php会话变量
数据已保存到数据库中。
<?php
include 'sql_connect.php';
if($_SERVER['REQUEST_METHOD']=="POST"){
$blog = $_POST['blog_post'];
$time = date('Y-m-d h-m-s');
$id = $_SESSION['author_id'];
$id=123;
if($blog != ""){
$query = "INSERT INTO `blog`(`date`,`post`,`author_id`) VALUES('$time','$blog',$id)";
//echo $query;
$result = mysqli_query($link, $query);
$row = mysqli_affected_rows($link);
if($result){
// include 'header.php';
// $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
// $url = rtrim($url, '/\\');
// $url .= '/blog.php';
// echo '<h3>Your blog has been posted. Go to <a style="color:white" href="'.$url.'">'.$url.'</a>';
echo "seemed to work";
}else{
echo "that didn't work";
}
}else{
//include 'header.php';
//echo '<h1>You did not enter a blog post. Please try again.';
//include 'footer.php';
}
}else{
//redirect('index.php');
}
?>