如何在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.

dqj5046
dqj5046 谁投票我的问题是负面的,为什么?
2 年多之前 回复
drsb77336
drsb77336 我使用:$Sql_Query=“UPDATERistoratoreAKSETnomeR='$nome',cognomeR='$cognome',data_nascitaR='$data_nascita',sessoR='$sesso',nome_ristorante='$nome_ristorante'”;但不起作用
2 年多之前 回复
dongxuan58311366668
dongxuan58311366668 我已修改问题,我已将一个PHP代码插入FirstForm,我用它来插入第一个id_ak和数字。
2 年多之前 回复
doumao1519
doumao1519 在这种情况下,您的错误是您使用INSERT而不是UPDATE。(仅在查询中使用update替换insert是不够的,update还有其他语法)
2 年多之前 回复
duan7007
duan7007 谢谢你的帮助,但我不能理解,你能告诉我一个简单的例子吗?我很感激你,找到像你这样帮助我的人并不容易。
2 年多之前 回复
duanpen9294
duanpen9294 我能怎么做?首先使用第一个表单保存id_ak和数字。我不能再插入它们了。如果我把所有NULL字段都可以工作吗?
2 年多之前 回复
dongxixiu9134
dongxixiu9134 我有两种形式,在第一种形式我插入NOTNULL字段,然后用第二种形式我去插入填充表的NULL字段。我哪里错了?
2 年多之前 回复
duanfa2014
duanfa2014 使用该插入查询,您尝试创建一个没有所有NOTNULL字段值的条目。这显然不起作用。插入数据时,必须填写所有NOTNULL列。
2 年多之前 回复
dongzhuo7291
dongzhuo7291 他们告诉我,为了防止SQL注入攻击PDO比mysqli更好
2 年多之前 回复

1个回答



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

但是在第二个表单之后,你不应该INSERT另一个条目,而是更新现有的条目(你之前创建的那个条目)。

要更新它,你需要 知道现有条目的ID。 </ p>

有了这个,你可以这样做一个UPDATE查询:</ p>

  UPDATE 
RistoratoreAK
SET
nomeR ='$ nome ',
cognomeR ='$ cognome',
data_nascitaR ='$ data_nascita',
sessoR ='$ sesso',
nome_ristorante ='$ nome_ristorante'
WHERE
id = $ existing_id
</ code > </ pre>
</ div>

展开原文

原文

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

du67560
du67560 谁投票我的问题是负面的,为什么?
2 年多之前 回复
douyoupingji7238
douyoupingji7238 感谢您的建议,我会很好地利用它
2 年多之前 回复
dounue6984
dounue6984 哦,只是一个提示:当查询不起作用时,不要回应“出错”,尝试回显有关错误的有用信息。 mysqli_error($ CON); 比“出问题”更有帮助。 你会发现你的错误更快;)
2 年多之前 回复
dragonpeng200811111
dragonpeng200811111 现在我试试,然后我会告诉你它是怎么回事。 现在,谢谢你,你对CascBee的帮助很大
2 年多之前 回复
duanlu1950
duanlu1950 您可以在插入后使用mysqli_insert_id($ con)来查找其自动生成的ID。 然后将其写入隐藏元素中的第二个表单,以便在提交第二个表单后读取/使用它。
2 年多之前 回复
duanqian2278
duanqian2278 id被称为“ID”,但它是自动递增的。 在$ existing_id变量中,我该使用什么? 我应该说出来,还是应该在SELECT中使用它?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问