-3

I have tried everything. This is my last resort. The code below is supposed to submit the info received from the form to the database to be displayed later. Now I know the query is correct but it does not run. I am sure this is some sort of typo that I just don't see, but any help would be greatly appreciated.

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');   
}

?> 
4

2 回答 2

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);
}
于 2013-05-25T15:30:38.247 回答
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');   
    }

?>
于 2013-05-25T16:49:40.573 回答