【问题标题】:php and mysqli code wont enter data into my data basephp 和 mysqli 代码不会将数据输入到我的数据库中
【发布时间】:2015-05-23 00:39:03
【问题描述】:

数据库名称:forumTutorial
表名: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

我没有收到任何错误.. 它只是说创建失败

<?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>

【问题讨论】:

  • 您需要添加错误处理,以便您查看问题所在。你可能是 sql 注入自己;你应该切换到准备好的语句。
  • php 区分大小写 isSet 不是函数使用 "isset()" 并且您的查询容易受到 SQL 注入的影响
  • PHP 不区分大小写,你从哪里得到的……只有变量在 php 中区分大小写,函数名、类名,甚至命名空间都不区分大小写,除非在它们的自动加载约束中跨度>
  • 感谢指正

标签: php mysqli


【解决方案1】:

您需要在插入查询中指定字段。此外,由于您的 id 字段设置为 AUTO_INCREMENT,因此您不应发送任何值。改变这个:

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

到这里:

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

【讨论】:

  • 非常感谢! :D @K P
猜你喜欢
  • 1970-01-01
  • 2017-08-15
  • 2018-07-13
  • 1970-01-01
  • 2015-05-04
  • 2015-09-14
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多