如何在NULL字段中插入VARCHAR数据

时间:2018-03-14 15:09:05

标签: php mysql sql-insert

我有一个数据库,其中有NOT NULLNULL个字段(NULL属于VARCHAR类型)。
当我尝试通过查询在NULL字段中输入数据时,它不会插入它们 数据不会同时输入:

  • 使用表单我将数据插入NOT NULL字段
  • 使用其他表单在NULL字段中插入数据。

为什么在NULL字段中输入数据的查询没有? 我试图找到类似问题的答案,但它们不起作用或不适合我的问题:

  1. MySQL Insert Select - NOT NULL fields

  2. Insert NULL into DATE field MySQL 5.6

  3. FIRST FORM register.php

    <?php
    
       if($_SERVER['REQUEST_METHOD']=='POST'){
      // echo $_SERVER["DOCUMENT_ROOT"];  // /home1/demonuts/public_html
    //including the database connection file
           include_once("config.php");
    
        $id_akR = $_POST['id_akR'];
        $numero_telefonoR = $_POST['numero_telefonoR'];
    
    
         if($id_akR == '' || $numero_telefonoR == '' ){
                echo json_encode(array( "status" => "false","message" => "Parameter missing!") );
         }else{
    
                $query= "SELECT * FROM RistoratoreAK WHERE id_akR='$id_akR' OR numero_telefonoR ='$numero_telefonoR' ";
                $result= mysqli_query($con, $query);
    
                $query2 = "SELECT ak_id, numero_telefono FROM AccountKit WHERE ak_id = '$id_akR' OR numero_telefono = '$numero_telefonoR'";
                $result2= mysqli_query($con, $query2);
    
                if(mysqli_num_rows($result) > 0){  
                   echo json_encode(array( "status" => "false","message" => "User already exist in Ristoratore!") );
                }else if(mysqli_num_rows($result2) > 0) {
                echo json_encode(array( "status" => "false","message" => "User already exist in Cliente!") );
    
                }else{ 
                 $query = "INSERT INTO RistoratoreAK (id_akR, numero_telefonoR) VALUES ('$id_akR','$numero_telefonoR')";
                 if(mysqli_query($con,$query)){
    
                     $query= "SELECT * FROM RistoratoreAK WHERE numero_telefonoR ='$numero_telefonoR'";
                             $result= mysqli_query($con, $query);
                         $emparray = array();
                             if(mysqli_num_rows($result) > 0){  
                             while ($row = mysqli_fetch_assoc($result)) {
                                         $emparray[] = $row;
                                       }
                             }
                    echo json_encode(array( "status" => "true","message" => "Successfully registered!" , "data" => $emparray) );
                 }else{
                     echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
                }
            }
                    mysqli_close($con);
         }
         } else{
                echo json_encode(array( "status" => "false","message" => "Error occured, please try again!") );
        }
    
     ?>
    

    第二种形式register2.php

    <?php
    if($_SERVER['REQUEST_METHOD']=='POST'){
    
    include 'config2R.php'; 
    
     $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
    
     $nome = $_POST['nomeR'];
     $cognome = $_POST['cognomeR'];
     $data_nascita = $_POST['data_nascitaR'];
     $sesso = $_POST['sessoR'];
     $nome_ristorante = $_POST['nome_ristoranteR'];
    
    
     $CheckSQL = "SELECT nome_ristorante FROM RistoratoreAK WHERE nome_ristorante='$nome_ristorante'";
    
     $check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
    
     if(isset($check)){
    
     echo 'Ristorante già registrato';
    
     }
    else{ 
    $Sql_Query = "INSERT INTO RistoratoreAK (nomeR,cognomeR,data_nascitaR,sessoR,nome_ristorante) values ('$nome','$cognome','$data_nascita','$sesso','$nome_ristorante')";
    
     if(mysqli_query($con,$Sql_Query))
    {
     echo 'Registration Successfully';
    }
    else
    {
     echo 'Something went wrong';
     }
     }
    }
     mysqli_close($con);
    ?>
    

    我的数据库包含一个名为“RistoratoreAK”的表,字段为:

     id                   INT PrimaryKey
     id_ak                VARCHAR NOT NULL
     number               VARCHAR NOT NULL
     nomeR                VARCHAR NULL
     cognomeR             VARCHAR NULL
     sessoR               VARCHAR NULL
     data_nascitaR        VARCHAR NULL
     nome_ristorante      VARCHAR NULL
    

    注意:对不起,如果代码不安全(我没有使用PDO),这段代码只是一个学习如何将数据上传到数据库的测试。

1 个答案:

答案 0 :(得分:1)

在第一个表单之后,使用id和id_ak将新条目插入到表中。这很好,而且很有效。

但在第二个表单之后,您不应该插入另一个条目,而是更新现有的条目(您之前创建的条目)。
要更新它,您需要知道现有条目的ID。

有了这个,您可以像这样进行UPDATE查询:

UPDATE 
    RistoratoreAK
SET
    nomeR = '$nome',
    cognomeR = '$cognome',
    data_nascitaR = '$data_nascita',
    sessoR = '$sesso',
    nome_ristorante = '$nome_ristorante'
WHERE 
    id = $existing_id