MySQLi将数据输入到数据库

时间:2016-03-18 14:40:52

标签: php mysql mysqli

如何让我的表单使用MySQLi(mysqli程序)将字段输入到我的数据库我目前有这个但是当我提交时没有错误但没有任何内容进入数据库。

 <?php

    if(isset($_POST["submit"])){
    $Fname = $_POST['firstname'];
    $Lname = $_POST['lastname'];
    $email = $_POST['email'];
    $gender = $_POST['gender'];
    $bday = $_POST['bday'];

$servername = "<--enter servername -->";
$username = "<--enter username -->";
$password = "<--enter password -->";
$database = "<--enter datbase name -->";

  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $database);

    $sql = "INSERT INTO Questions_users (Fname, Lname, email, gender, bday)
    VALUES ('$Fname','$Lname','$email','$gender','$bday')";

    if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);
    }
    ?>

表格是

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="text" name="firstname"></td>
<input type="text" name="lastname"></td>
<input type="email" name="email"></td>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female</td>
<input type="date" name="bday"></td>
<input type="submit" value="Submit"></form>

1 个答案:

答案 0 :(得分:4)

您需要为此设置name属性:

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

在您设置条件if(isset($_POST["submit"])){并且未找到submit元素时,您的PHP代码未执行。

另外,你错过了结束括号:

$sql = "INSERT INTO Questions_users (Fname, Lname, email, gender, bday)
VALUES ('[$Fname]','[$Lname]','[$email','[$gender]','[$bday]')";

'[$email'应为'[$email]'