duanpo1498 2018-03-14 15:09
浏览 146
已采纳

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

I have a database in which there are NOT NULL and NULL fields (the NULLs are of the VARCHAR type).
When I try to enter data in the NULL fields via my query, it does not insert them.
The data isn't entered all at the same time:

  • with a form I insert the data in the NOT NULL fields
  • with another form insert the data in the NULL fields.

Why doesn't the query for entering data in the NULL fields sork?   I tried to find an answer to similar questions, but they don't work or are not suitable for my problem:

  1. MySQL Insert Select - NOT NULL fields

  2. Insert NULL into DATE field MySQL 5.6

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!") );
    }

 ?>

SECOND FORM 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);
?>

My DB contains a table called "RistoratoreAK", the fields are :

 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

note: Excuse me if the code isn't secure (I didn't use PDO), this code is just a test to learn how to upload data to the database.

  • 写回答

1条回答

  • duadpnld426905 2018-03-14 15:49
    关注

    After the first form, you INSERT a new entry into your table with the id and id_ak. This is fine, and it works.

    But after the second form, you should not INSERT another entry, but UPDATE an existing one instead (the one that you created before).
    To update it, you need to know the id of the existing entry.

    Having that, you can make an UPDATE query like this:

    UPDATE 
        RistoratoreAK
    SET
        nomeR = '$nome',
        cognomeR = '$cognome',
        data_nascitaR = '$data_nascita',
        sessoR = '$sesso',
        nome_ristorante = '$nome_ristorante'
    WHERE 
        id = $existing_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制