1

I am using this code to send data to mysql database and its functioning well though users are sending blank data and duplicating too.

<?php 
  require "conn.php";
  $lostidno = $_POST ["lostidno"];
  $phonenol = $_POST ["phonenol"];

  $mysql_qry = "INSERT INTO lostdb.lost (lostidno, phonenol) VALUES ('$lostidno', '$phonenol')";  

  if ($conn->query($mysql_qry) === TRUE) 
  {
    echo "Information Recieved!";
  }
  else
    echo "Sorry! An Error Occured:" . $mysql_qry . "br" . $conn->error;

  $conn->close();

?>

How do I prevent this?

Ish
  • 2,085
  • 3
  • 21
  • 38
Henry Gathigira
  • 265
  • 4
  • 11

5 Answers5

0

1. To avoid blank values

Check the values of variables before executing insert query on database using empty() PHP function.

2. Avoid duplicate values

You can do it in two ways, Either specify UNIQUE constriant in database TABLE or run SELECT query before inserting data into database.

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
0

You can use trim function to remove spaces at the beginning and the ending of a string. After that you are able to check if the two parameters aren't empty:

<?php 
require "conn.php";
$lostidno = trim($_POST["lostidno"]);
$phonenol = trim($_POST["phonenol"]);

if(!empty($lostidno) && !empty($phonenol))
{
    $mysql_qry = "INSERT INTO lostdb.lost (lostidno, phonenol) VALUES ('$lostidno', '$phonenol')";  

    if ($conn->query($mysql_qry) === TRUE) {
        echo "Information Recieved!";
    }
    else {
        echo "Sorry! An Error Occured:" . $mysql_qry . "br" . $conn->error;
    }

    $conn->close();
}
?>

You should also have a look at this to prevent SQL injections.

Community
  • 1
  • 1
mario.van.zadel
  • 2,919
  • 14
  • 23
0
//this to check the duplicate records
Fire the select query : select * from lostdb.lost where col1= $lostidno and col2 = $phonenol;

//this is to check the blank condition
if($lostidno !== "" && $phonenol != "")
{
  $mysql_qry = "INSERT INTO lostdb.lost (lostidno, phonenol) VALUES ('$lostidno', '$phonenol')"; 
}
Ish
  • 2,085
  • 3
  • 21
  • 38
0

Use trim function to remove blank spaces from string.

<?php 
require "conn.php";
$lostidno = trim($_POST ["lostidno"]);
$phonenol = trim($_POST ["phonenol"]);

//Check record exist or not.
$unique_check = "select count(*) as cnt from lostdb.lost where lostidno='".$lostidno."' and phonenol='".$phonenol."'";
//Execute above query and check record already exist or not. If record not exist then only allow to insert it.

$result_unique_check = $conn->query($mysql_qry)->one();
$record_exist = $result_unique_check['cnt'];

if($record_exist > 0){
    echo "Record alreay exist.";    
}else{
    //Insert new record
    $mysql_qry = "INSERT INTO lostdb.lost (lostidno, phonenol) VALUES ('$lostidno', '$phonenol')";  

    if ($conn->query($mysql_qry) === TRUE) 
    {
    echo "Information Recieved!";
    }
    else

    echo "Sorry! An Error Occured:" . $mysql_qry . "br" . $conn->error;

    $conn->close();   
}

?>
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
0

The best way to prevent duplicates is to mark the unique database columns as UNIQUE

ALTER TABLE <table_name> ADD UNIQUE(<column_name>);

To prevent blank data from ever getting into the database you need to check them with a few modifications in the code you posted:

if( ($lostidnol != "") && ($phonenol != "") )
{
    if ($conn->query($mysql_qry) === TRUE) 
    {
        echo "Information Recieved!";
    }
    else
    {
        //error
    }
}
else
{
    //Notify the user that they're trying to insert blanks
}

If you want to provide proper user experience you should also provide some client side validation using JavaScript or jQuery

dimlucas
  • 5,040
  • 7
  • 37
  • 54