php - "INSERT INTO"没有向数据库中插入数据

标签 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

最佳答案

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

此外,您可以尝试调试。

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

关于php - "INSERT INTO"没有向数据库中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36277771/

相关文章:

php - 使用php exec函数进行Powershell远程执行

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

PHP MySQL 和计算错误

php - 无法访问 CodeIgniter 站点

mysql - 如何获得总天数

php - MySQL通过PHP更新多行

php - PHP 字符串中的三元运算符

php - 如何使用同一域的 memcached 在多个服务器之间共享 php session

PHP + pthreads : Build one big object - parallel-processing

php - 在我的搜索中出现错误 (php)