Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL插入PHP不起作用_Php_Mysql - Fatal编程技术网

MySQL插入PHP不起作用

MySQL插入PHP不起作用,php,mysql,Php,Mysql,我目前希望使用PHP运行一个基本的insert查询,将HTML表单数据提交到MySQL数据库。 不幸的是,插入进程没有运行。 在我的Insert语法中,我尝试过包括$_POST[fieldname],我尝试过包括下面的变量,我甚至尝试过使用不同的撇号,但似乎没有任何效果 作为补充,我还收到了大量的wamp去润滑错误,这是压倒性的,我已经在php.ini和php for apache.ini文件中禁用了这些错误,并且还在继续 如果有人能告诉我我的插入和其他任何错误,我将非常感谢 我会直接把这篇介绍

我目前希望使用PHP运行一个基本的insert查询,将HTML表单数据提交到MySQL数据库。 不幸的是,插入进程没有运行。 在我的Insert语法中,我尝试过包括$_POST[fieldname],我尝试过包括下面的变量,我甚至尝试过使用不同的撇号,但似乎没有任何效果

作为补充,我还收到了大量的wamp去润滑错误,这是压倒性的,我已经在php.ini和php for apache.ini文件中禁用了这些错误,并且还在继续

如果有人能告诉我我的插入和其他任何错误,我将非常感谢

我会直接把这篇介绍保留下来。 用户登录,如果他们试图在未登录的情况下登录,则返回登录页面登录。 我使用外部配置文件连接到数据库,以便在其他地方托管时将更新保存在50个位置。 配置文件工作正常,因此下面未显示。 数据库名为mydb。 我将文本字段项存储到变量中,然后在insert查询中使用变量。 unitID是一个自动递增字段,因此在运行insert时将其留空。 不幸的是,mysql数据库中没有任何内容。 提前谢谢。 PS文本字段名都正确匹配

<?php
    //Start the session
    session_start();
    //check the user is logged in
    if (!(isset($_SESSION['Username']) )) {
        header ("Location: LoginPage.php?i=1");
        exit();
    }
    //Connect to the database
    include 'config.php';
    $UserName = $_SESSION['Username'];
    $UserIdentification = $_SESSION['UserID'];          
    if(isset($_GET['i'])){
        if($_GET['i'] == '1'){
            $tblName="sightings";
            //Form Values into store
            $loco =$_POST['txtloco'];
            $where =$_POST['txtwhere'];
            $when =$_POST['txtdate'];
            $time =$_POST['txttime'];
            $origin =$_POST['txtorigin'];
            $dest =$_POST['txtdest'];
            $headcode =$_POST['txtheadcode'];               
            $sql= "INSERT INTO sightings (unitID, Class, Sighted, Date, Time, Origin, Destination, Headcode, UserID) VALUES ('','$loco', '$where', '$when', '$time', '$origin', '$dest', '$headcode', '$UserIdentification')";
            mysql_select_db('mydb');
            $result=mysql_query($sql, $db);
                if($result){
                    $allocationsuccess = "Save Successful";
                    header ('Refresh: 2; url= create.php');
                }
                else {
                    $allocationsuccess = "The submission failed :(";
                }
        }   
    }
?>

“unitID是一个自动递增字段,因此我在运行时将其留空
“插入”

这不是它的工作原理。您必须在INSERT语句中完全省略它。代码认为您试图将该字段设置为空字符串,这是不允许的

$sql= "INSERT INTO sightings (Class, Sighted, Date, Time, Origin, Destination, Headcode, UserID) VALUES ('$loco', '$where', '$when', '$time', '$origin', '$dest', '$headcode', '$UserIdentification')";
应该解决这个问题。MySQL将自动为字段生成一个值,并在创建行时为您插入该值

如果您的代码记录了由
mysql\u error()
生成的消息,只要
mysql\u query()
返回
false
,那么您就会看到查询生成了一个错误,这可能会为您提供发生了什么的线索


另外,如评论中所述,您需要使用更新的mysql代码库和更好的技术(包括参数化)重新编写代码,以避免您当前面临的各种漏洞。

说。与您一起学习。我建议使用
PDO
,我希望它比使用非参数化查询更加简单、干净、安全。另外,可能会帮助您在
MySQLi
PDO
之间进行选择。请不要对新代码使用
mysql.*
函数。它们不再被维护,社区已经开始使用,PHP 7中的
mysql.*
函数已经被正式删除。相反,您应该学习并使用
PDO
mysqli.*
。如果你不能决定,.
去润滑错误,这是压倒性的,我在php.ini和php for apache.ini文件中禁用了,但仍然会出现。
hmm我想知道为什么会这样?也许停止使用贬值的函数?你为什么要忽略这些错误呢?这是不负责任的。您是否尝试从insert查询中删除UnitID?此问题现已解决。非常感谢您的帮助