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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?