dongping1689
dongping1689
2018-06-19 09:56

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

已采纳

Hello guys here is my form with php. Whole form is connected to MySQL. Everything work. But when I fill option value with input "zameranie", so it show in database like 0 value. All others values like name, password, email, gender work, but faculty and orientation don't.

Fakulta = faculty Zameranie = orientation

sorry for didn't translate in code. But I think there is no problem.

<!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条回答

  • doucan8246326 doucan8246326 3年前

    Find the snippet:

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

    In $stmt->bind_param("ssssii", ...), you're instructing PHP to bind $fakulta and $zameranie as integers instead of strings. use $stmt->bind_param("ssssss", ...) instead. This should fix your issue.

    You can read more about the bind function here: http://php.net/manual/en/mysqli-stmt.bind-param.php

    点赞 评论 复制链接分享
  • dongxing2030 dongxing2030 3年前

    <?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();
         ?>

    </div>
    
    点赞 评论 复制链接分享