我尝试添加选项值时的SQL null值

时间:2018-06-19 09:56:34

标签: php mysql sql database

大家好,我的表格是php。整个表单连接到MySQL。一切正常。但是当我用输入" zameranie"填充选项值时,它会在数据库中显示为0值。所有其他人的价值观,如姓名,密码,电子邮件,性别工作,但教师和方向不是。

Fakulta =教师 Zameranie =方向

抱歉没有在代码中翻译。但我认为没有问题。

<!DOCTYPE HTML>
    <html>
    <head>
         <meta charset="UTF-8">
      <title>Form</title>
    </head>
    <body>
     <form action="insert.php" method="POST">
      <table>
       <tr>
        <td>Meno :</td>
        <td><input type="text" name="username" required></td>
       </tr>
       <tr>
        <td>Heslo :</td>
        <td><input type="password" name="password" required></td>
       </tr>
       <tr>
        <td>Pohlavie :</td>
        <td>
         <input type="radio" name="gender" value="m" required>Muž
         <input type="radio" name="gender" value="f" required>Žena
        </td>
       </tr>
       <tr>
        <td>Email :</td>
        <td><input type="email" name="email" ></td>
       </tr> 
       <tr>
        <td>Fakulta + zameranie</td>
        <td>
         <select name="fakulta" required>
          <option selected hidden value="">Fakulta</option>
          <option value="fri">FRI</option>
          <option value="fhv">FHV</option>
          <option value="sjf">SjF</option>
          <option value="pedas">PEDAS</option>
          <option value="svf">SvF</option>
          <option value="ef">EF</option>
         </select>
         <input type="text" name="zameranie" required>
        </td>
       </tr>
       <tr>
        <td><input type="submit" value="Submit"></td>
       </tr>
      </table>
     </form>
    </body>
    </html>

<?php
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$fakulta = $_POST['fakulta'];
$zameranie = $_POST['zameranie'];
if (!empty($username) || !empty($password) || !empty($gender) || !empty($email) ||
!empty($fakulta) || !empty($zameranie)) {
 $host = "localhost";
    $dbUsername = "root";
    $dbPassword = "";
    $dbname = "youtube";
    //create connection
    $conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
    if (mysqli_connect_error()) {
     die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
    } else {
     $SELECT = "SELECT email From register Where email = ? Limit 1";
     $INSERT = "INSERT Into register (username, password, gender, email, fakulta, zameranie) values(?, ?, ?, ?, ?, ?)";
     //Prepare statement
     $stmt = $conn->prepare($SELECT);
     $stmt->bind_param("s", $email);
     $stmt->execute();
     $stmt->bind_result($email);
     $stmt->store_result();
     $rnum = $stmt->num_rows;
     if ($rnum==0) {
      $stmt->close();
      $stmt = $conn->prepare($INSERT);
      $stmt->bind_param("ssssii", $username, $password, $gender, $email, $fakulta, $zameranie);
      $stmt->execute();
      echo "Úspešne pridané do databázy";
     } else {
      echo "Niekto používa tento mail";
     }
     $stmt->close();
     $conn->close();
    }
} else {
 echo "Všetky položky sú povinné";
 die();
}
?>

Result in database when I fill form

Here is database

2 个答案:

答案 0 :(得分:1)

找到摘录:

//...<br>
$stmt = $conn->prepare($INSERT);<br>
$stmt->bind_param("ssssii", $username, $password, $gender, $email, $fakulta, $zameranie);<br>
//...<br><br>

$stmt->bind_param("ssssii", ...)中,您指示PHP将$fakulta$zameranie绑定为整数而不是字符串。请改用$stmt->bind_param("ssssss", ...)。这应该可以解决您的问题。

您可以在此处详细了解绑定功能:http://php.net/manual/en/mysqli-stmt.bind-param.php

答案 1 :(得分:0)

<?php
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$fakulta = $_POST['fakulta'];
$zameranie = $_POST['zameranie'];
if (!empty($username) || !empty($password) || !empty($gender) || !empty($email) ||
!empty($fakulta) || !empty($zameranie)) {
       $conn = mysqli_connect('localhost', 'root', '', 'test');
    if (mysqli_connect_error()) {
     die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
    } else {
     $SELECT = "SELECT email From register Where email = ? Limit 1";
     $INSERT = "INSERT Into register (username, password, gender, email, fakulta, zameranie) values('$username', '$password', '$gender' ,' $email', '$fakulta','$zameranie')";
     die();
     ?>