简体   繁体   English

php和mysqli代码不会在我的数据库中输入数据

[英]php and mysqli code wont enter data into my data base

Database name: forumTutorial 数据库名称: forumTutorial
table name: threads 表名: threads

4 Columns:  
id      - INT     - 5    Length - Primary Key - Auto Increment (AI/A_I).  
title   - VARCHAR - 255  Length
content - VARCHAR - 2000 Length  
author  - VARCHAR - 255  Length

I didnt get any errors.. it just says failed to create 我没有收到任何错误..它只是说创建失败

<?php
session_start();
//$_SESSION['username'] = 'Admin';
$con = mysqli_connect('localhost', 'root', '', 'forumTutorial') or die(mysql_error());
if (isSet($_POST['createThread'])) {
    if (isSet($_POST['title']) && $_POST['title'] != '' && isSet($_POST['content']) && $_POST['content'] != '' && isSet($_SESSION['username']) && $_SESSION['username'] != '') {
        $title = $_POST['title'];
        $content = $_POST['content'];
        $user = $_SESSION['username'];
        $q = mysqli_query($con, "INSERT INTO `threads` VALUES ('', '$title', '$content', '$user')");
        if ($q) {
            echo 'Thread created.';
        }else
            echo 'Failed to create thread.';
    }
}?>
<html>
<head></head>
<body>
    <h1>Create Thread:</h1>
    <form action='forumTutorial.php' method='POST'>
    <table>
        <tbody>
            <tr>
                <td>Title: </td><td><input type='text' name='title' /></td>
            </tr>
            <tr>
                <td>Description: </td><td><input type='text' name='content' /></td>
            </tr>
            <tr>
                <td></td><td><input type='submit' value='Create Thread' name='createThread' /></td>
            </tr>
        </tbody>
    </table>
    </form>
</body>

You need to specify the fields in your insert query. 您需要在插入查询中指定字段。 Also, since your id field is set to AUTO_INCREMENT, you shouldn't send any value. 另外,由于您的ID字段设置为AUTO_INCREMENT,因此您不应发送任何值。 Change this: 更改此:

$q = mysqli_query($con, "INSERT INTO `threads` VALUES ('', '$title', '$content', '$user')");

To this: 对此:

$q = mysqli_query($con, "INSERT INTO `threads` (title, content, author) VALUES ('$title', '$content', '$user')");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM