简体   繁体   English

MySQL插入PHP无法正常工作

[英]MySQL Insert Into PHP Not Working

I am currently looking to run a basic insert query using PHP to submit HTML form data to MySQL database. 我目前正在寻找使用PHP运行基本插入查询以将HTML表单数据提交到MySQL数据库的方法。 Unfortunately however the insert process isnt running. 不幸的是,插入过程并未运行。 In my Insert syntax I have tried including $_POST[fieldname], ive tried including variables as below, and ive even played around with different apostrphes but nothing seems to be working. 在我的插入语法中,我尝试过包含$ _POST [fieldname],我尝试过包含以下变量,并且ive甚至使用了不同的撇号,但似乎没有任何作用。

as a side dish, im also getting truck load of wamp deprication errors which is overwhelming, ive disabled in php.ini and php for apache.ini file and still coming up. 作为配菜,即时通讯还获得了大量的沼泽破坏错误,我在php.ini和php中禁用了apache.ini文件,并且仍然在运行。

If anyone can advise what is wrong with my insert and anything else id be much thankful. 如果有人可以告知我插入内容有什么问题,以及其他任何ID,请多谢。

Ill keep this intro straightfoward. 病态保持这个介绍直截了当。 Person logs in, if they try to get in without login they go back to login page to login. 人员登录,如果他们尝试不登录而进入,则返回登录页面进行登录。 I connect to database using external config file to save me updating in 50 places when hosting elsewhere. 我使用外部配置文件连接到数据库,以便在其他地方托管时可以在50个地方保存更新。 Config file is working fine so not shown below. 配置文件工作正常,因此未在下面显示。 database is called mydb. 数据库称为mydb。 Im storing the text field items into variables, then using the variables in the insert query. 我将文本字段项存储到变量中,然后在插入查询中使用变量。 unitID is an auto increment field so I leave that blank when running the insert. unitID是一个自动递增字段,因此在运行插入时将其留空。 Unfortunately nothing is going in to the mysql database. 不幸的是,什么也没有进入mysql数据库。 Thanks in advance. 提前致谢。 PS the text fieldnames are all correctly matched up 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 is an auto increment field so I leave that blank when running the insert" “ unitID是一个自动递增字段,因此在运行插入时我将其留为空白”

That's not how it works. 那不是它的工作原理。 You have to omit it completely from the INSERT statement. 您必须从INSERT语句中完全忽略它。 The code thinks you're trying to set that field to a blank string, which is not allowed. 代码认为您正在尝试将该字段设置为空白字符串,这是不允许的。

$sql= "INSERT INTO sightings (Class, Sighted, Date, Time, Origin, Destination, Headcode, UserID) VALUES ('$loco', '$where', '$when', '$time', '$origin', '$dest', '$headcode', '$UserIdentification')";

should fix that particular issue. 应该解决该特定问题。 MySQL will generate a value automatically for the field and insert it for you when it creates the row. MySQL将自动为该字段生成一个值,并在创建行时为您插入该值。

If your code had been logging the message produced by mysql_error() whenever mysql_query() returns false then you'd have seen an error being generated by your query, which might have given you a clue as to what was happening. 如果您的代码在mysql_query()返回false时就记录了mysql_error()产生的消息,则您将看到查询正在生成错误,这可能为您提供了正在发生的情况的线索。

PS As mentioned in the comments, you need to re-write your code with a newer mysql code library and better techniques including parameterisation, to avoid the various vulnerabilities you're currently exposed to. PS如评论中所述,您需要使用较新的mysql代码库和更好的技术(包括参数化)来重新编写代码,以避免当前遇到的各种漏洞。

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

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