2017-09-25 86 views
-1

我目前正在运行使用PHP HTML表单数据提交到MySQL数据库基本插入查询。 不幸的是插入过程没有运行。 在我的插入语法中,我尝试过包括$ _POST [fieldname],我尝试过包括下面的变量,并且我甚至用不同的apostrphes玩过,但似乎没有任何工作。的MySQL插入到PHP不工作

为WAMP deprication错误的小菜,即时通讯也越来越卡车装载这是压倒性的,在php.ini和PHP的apache.ini等文件,还来了香港专业教育学院禁用。

如果任何人都可以建议有什么不对我的插入和别的ID是多少心存感激。

虐待保持这种介绍straightfoward。 人登录,如果他们尝试不登录就进入登录页面登录。 我连接到数据库使用外部配置文件,以保存我在50个地方更新时托管其他地方。 配置文件工作正常,所以不显示在下面。 数据库被称为mydb。 Im将文本字段项存储到变量中,然后使用插入查询中的变量。 unitID是一个自动增量字段,所以在运行插入时我留下空白。 不幸的是没有任何进入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 :("; 
       } 
     } 
    } 
?> 
+2

[小博](http://bobby-tables.com/)说** [你可能会在对SQL注入攻击的风险(https://stackoverflow.com/q/60174/)**。通过[参数化查询](https://stackoverflow.com/a/4712113/5827005)了解[准备好的陈述](https://en.wikipedia.org/wiki/Prepared_statement)。我推荐'PDO',我[写了一个函数](http://paragoncds.com/grumpy/pdoquery/#function),使它非常容易,干净,并且比使用非参数化查询更安全。此外,[本文](http://php.net/manual/en/mysqlinfo.api.choosing.php)可以帮助你MySQLi'和'PDO' – GrumpyCrouton

+1

** **请不要使用之间选择'新代码的'mysql_ *'函数。他们不再被维护,社区已经开始[弃用过程](http://news.php.net/php.internals/53799),并且'mysql_ *'函数已经在PHP 7中被正式删除。相反,你应该了解[已准备好的语句](https://en.wikipedia.org/wiki/Prepared_statement)并使用“PDO”或“mysqli_ *”。如果你不能决定,[这篇文章将有助于选择你最好的选择](http://php.net/manual/en/mysqlinfo.api.choosing.php)。 – GrumpyCrouton

+2

'depecication错误是压倒性的,ive禁用php.ini和php的apache.ini文件,并仍然出现。嗯我想知道为什么这是?也许停止使用折旧功能?你为什么会试图忽略这些错误?这是不负责任的。 – GrumpyCrouton

回答

2

“的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_error()每当mysql_query()回报false,那么你会看到一个错误您的查询,这可能给你一个线索,发生了什么事情正在发生的消息。

P.S.正如评论中所提到的,您需要使用更新的mysql代码库和更好的技术(包括参数化)重新编写代码,以避免您目前暴露的各种漏洞。

+0

非常感谢这个问题现在已经解决了 – trainmania100