为什么mysqli的查询没有将值插入数据库?

时间:2015-12-24 08:18:42

标签: php mysqli

我尝试了简单的代码,但不是将值插入数据库表,它触发插入的javascript警告失败我尝试了一切仍然无法将值插入表中。我是mysqli的新手。

        <?php       
  $con = mysqli_connect('localhost','root','','database')or die(mysqli_error($con));

      // Check connection
     if (mysqli_connect_errno())
        {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
         }
        else
         {
           echo "Your Database is connected , No issues there..";

         }
             $Name = $_POST['Name'];
              $q =mysqli_query($con,"INSERT INTO  tablename(Field_Name) VALUES ('$Name')")or die(mysqli_error($con));

          echo $q;

       if($q)
      {

        header("Location:newpage.php");

        }
         else
          {      
             ?>
               <script>alert('insertion failed');</script>

           <?php
             } 

             mysqli_close($con);
               }

          ?>

1 个答案:

答案 0 :(得分:4)

这一行:

$q =mysqli_query($con,"INSERT INTO  tablename(Field_Name) VALUES ('$Name')"or die(mysqli_error($con));

你似乎错过了一个结束括号。

$q =mysqli_query($con,"INSERT INTO  tablename(Field_Name) VALUES ('$Name')") or die(mysqli_error($con));

如果您仍然有错误,请确保

  • 表名正确
  • 您的字段正确

我还建议你改变

$Name = $_POST['Name']; 

$Name = mysqli_real_escape_string($con, $_POST['Name']);