I have a database in which there are NOT NULL
and NULL
fields (the NULL
s 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:
NOT NULL
fieldsNULL
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:
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.
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