Phpmyadmin数据库不接受记录

时间:2016-04-23 13:56:57

标签: php phpmyadmin wampserver

有人可以帮忙吗?所以我正在使用wamp服务器进行php练习。当我只有3列时,我的表单只接受记录,但是当我添加更多列时,记录没有被添加。

以下代码

html表格

<body>

  <form action = "Form.php" method = "post">

        <label> Title: </label> <input type = "text" name = "title"/>

        <label> First Name: </label> <input type = "text" name = "fname"/>

        <label> Last Name: </label> <input type = "text" name = "lname"/>

        <label> EmailAddress: </label> <input type = "text" name = "address"/>

        <label> Hobby: </label> <input type = "text" name = "hobby"/>

        <label> Sex: </label> <input type = "text" name = "gender"/>

        <label> UserName: </label> <input type = "text" name = "uname"/>



        <input type = "submit" name = "submitbtn" value = "Submit"/>


    </form> 




</body>

Php page

<?php

if (isset($_POST['submitbtn'])){

    $title = $_POST['title'];
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $address = $_POST['address'];
    $hobby = $_POST['hobby'];
    $sex = $_POST['gender'];
    $uname = $_POST['uname'];

    $connect_db = mysqli_connect("localhost","root","","Humans");

    if(!$connect_db){
        echo "Connection failed";
    } else 
         echo "Connection successful";

    if(!mysqli_select_db($connect_db, 'Humans')){
        echo "Database not selected";
    } else
        echo "Database selected";


    $sql = "INSERT INTO People(Title, FirstName, LastName, EmailAddress, Hobby, Sex, UserName)VALUES('$title','$fname','$lname','$address','$hobby','$sex','$uname')";
    if(mysqli_query($connect_db, $sql)){
        echo "Record was added";
    }else
       echo "Record was not added";
 }
 ?>

输出:

Connection successful
 Database selected 
Record was not added 

1 个答案:

答案 0 :(得分:0)

PHP MySQL数据库扩展在出现问题时提供有用的消息和代码,学习使用它们是个好主意。

因此,如果您在邮件中添加mysqli_error($connect_db),就会发现查询中出现了什么问题。

$sql = "INSERT INTO People(Title, FirstName, LastName, EmailAddress,
                           Hobby, Sex, UserName)
        VALUES('$title','$fname','$lname','$address',
               '$hobby','$sex','$uname')";

if(mysqli_query($connect_db, $sql)){
    echo "Record was added";
}else
   // add the error message so you see something useful
   echo "Record was not added " . mysqli_error($connect_db);
}

幸运的是,您可以解决自己的问题,但如果不在您的问题中发布错误消息,我可以扩展此答案