“INSERT INTO”未将数据插入数据库

时间:2016-03-29 07:15:09

标签: php mysql

我知道之前已经多次询问过这个问题了。其中之一是这一个:https://stackoverflow.com/questions/18869513/php-insert-into-not-working但尝试给出这个解决方案,对我来说不起作用。所以我有这个主页:

<html>
<head>
    <title>My First PHP Website</title>
</head>
<?php
    session_start();
    if($_SESSION['user']){}
    else{header("location:index.php");}
    $user = $_SESSION['user'];
?>

    <body>
        <h2>Homepage</h2>
        <p>Hello
            <?php Print "$user"?>
        </p>
        <a href="logout.php">Click Here to Logout</a>
        <br>
        <br>
        <form action="add.php" method="POST">
            Add more to list:
            <input type="text" name="details">
            <br> Public Post?
            <input type="checkbox" name="public[]" value="yes">
            <br>
            <input type="submit" value="Add to List">
        </form>
        <h2 align="center">My List</h2>
        <table border="1px" width="100%">
            <tr>
                <th>ID</th>
                <th>Details</th>
                <th>Edit</th>
                <th>Delete</th>
                <th>Public Posts</th>
            </tr>
            <?php
                $connect = mysqli_connect("localhost","root","") or die(mysqli_error());
                mysqli_select_db($connect, "first_db") or die ("Cannot Connect to Database");
                $query = mysqli_query($connect,"SELECT * from list");
                while($row = mysqli_fetch_array($query))
                {
                    Print "<tr>";
                        Print '<td align="center">'. $row['id'] . "</td>";
                        Print '<td align="center">'. $row['details'] . "</td>";
                        Print '<td align="center">'. $row['date_posted']." - ".$row['time_posted']."</td>";
                        Print '<td align="center">'. $row['date_edited']." - ".$row['time_edited']."</td>";
                        Print '<td align="center"><a href="edit.php">edit</a> </td>';
                        Print '<td align="center"><a href="delete.php">delete</a> </td>';
                        Print '<td align="center">'. $row['public']. "</td>";
                    Print "</tr>";
                }
            ?>
        </table>
    </body>
</html>

和此操作页面:

<?php
    session_start();
    if($_SESSION['user']){
    }
    else{
        header("location:index.php");
    }
    if($_SERVER["REQUEST_METHOD"] == "POST"){   
        $connect = mysqli_connect("localhost","root","");
        $link = mysqli_connect("localhost","root","","first_db");
        $details = mysqli_real_escape_string($connect, $_POST['details']);
        $time = strftime("%X");
        $date = strftime("%B %d, %Y");
        $decision ="no";
        mysqli_connect("localhost", "root","") or die(mysqli_error());
        mysqli_select_db($connect,"first_db") or die("Cannot connect to database");
        foreach($_POST['public'] as $each_check)
        {
            if($each_check !=null ){
                $decision = "yes";
            }
        }
        mysqli_query ($link,"INSERT INTO list (details) VALUES ('$details')");

        header("location: home.php");
    }
    else
    {
        header("location:home.php");
    }   
?>

这应该是将输入的数据插入我的数据库,但遗憾的是它不会做任何事情。我无法弄清楚这个解决方案。我希望你们能帮助我。提前谢谢!

table structure

4 个答案:

答案 0 :(得分:0)

您不需要两个连接变量$ connect和$ link。使用任何一个。

此外,您可以尝试调试。

mysqli_query ($link,"INSERT INTO list (details) VALUES ('".$details."')") or die(mysqli_error($link));

答案 1 :(得分:0)

试试这个

$details = "'".$details."'";
mysqli_query ($link,"INSERT INTO list (details) VALUES ($details)");

答案 2 :(得分:0)

试试这个:

if($_SERVER["REQUEST_METHOD"] == "POST")
{   
        // $connect = mysqli_connect("localhost","root","");
        // mysqli_select_db($connect,"first_db") or die("Cannot connect to database");

编辑:

$link = mysqli_connect("localhost","root","","first_db");
$details = mysqli_real_escape_string($link, $_POST['details']);
$time = strftime("%X");
$date = strftime("%B %d, %Y");
$decision ="no";

foreach($_POST['public'] as $each_check)
{
    if($each_check !=null ){
        $decision = "yes";
    }
}
mysqli_query ($link,"INSERT INTO list (details) VALUES ('".$details."')");

        header("location: home.php");
}

答案 3 :(得分:0)

您的两个页面存在很多问题。尽量清除它。

对于您的表单页面...
放在页面的顶部,在html标记上方:

error_reporting(E_ALL);
ini_set('display_errors', '1');
ob_start();
session_start();

if(!$_SESSION['user']){
 header("location:index.php");
exit();
}
$user = $_SESSION['user'];

从你拥有它们的地方删除它们。前两行将通知您PHP代码的任何错误
请注意!$_SESSION部分。这样做:如果未设置sessios,请执行某些操作。使用这种语法,你不需要这里的其他人
标题重定向后始终exit()
现在,对于你的第二页(提交),我做了很多改变。

error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
// check if session is set and if $_POST is there.. if any of these checks fails, redirect to index page
if( !$_SESSION['user'] || !isset($_POST) ){
    header("Location: index.php");
    exit();
}

// db connection
$connect = mysqli_connect("localhost", "root", "", "first_db");
if (!$connect) { echo 'Connect to DB Error ('.mysqli_connect_errno().')'. mysqli_connect_error(); exit(); }
mysqli_query($connect,  " SET NAMES 'utf8' ");

// check/sanitize $_POST variables
$details = '';
if (isset($_POST['details'])) { $details = $_POST['details']; }

// declare other variables defaults values
$time = strftime("%X");
$date = strftime("%B %d, %Y");
$decision = "no";

// insert data to db
$query = "INSERT INTO list (details) VALUES ('".mysqli_real_escape_string($connect, $details)."') ";
$result =  mysqli_query($connect, $query) or die(mysqli_error($connect));


// if all are ok, sent user to new page
header("location: home.php");

如果发生任何错误,它将出现在您的屏幕上
希望这可以帮助你。