Home > Mobile >  How to stop inserting blank data into database
How to stop inserting blank data into database

Time:09-24

I'm trying to take input from the form but Whenever I'm refreshing the page blank data is inserted in the database, Please help here is the code:

<?php
  session_start();`
  $dbhost = 'localhost';
  $dbname = 'transfer';
  $dbusername = 'root';
  $dbpass = '';
  $conn = mysqli_connect("localhost", "root", "", "transfer");
   //check connection
   if(!$conn){
     die('Could not Connect My Sql:'.mysql_error());
  }
 $SENDER_Name =(isset( $_REQUEST['SENDER_Name'])? $_REQUEST['SENDER_Name'] : '');
 $RECIVER_Name =(isset($_REQUEST['RECIVER_Name']) ? $_REQUEST['RECIVER_Name'] : '');
 $Amount = (isset($_REQUEST['Amount']) ? $_REQUEST['Amount'] : '') ;
 // Performing insert query execution
 // here our table name is amount
 
 $sql = "INSERT INTO amount VALUES('$SENDER_Name', 
     '$RECIVER_Name','$Amount')";
  
    
  if(mysqli_query($conn, $sql)){
   echo nl2br("\n$SENDER_Name\n $RECIVER_Name\n "
       . "$Amount");
} else{
   echo "ERROR: Hush! Sorry $sql. " 
       . mysqli_error($conn);
}
    $conn->close();
?> ```

CodePudding user response:

Your code is working as it is meant to work. If you do not want to insert blank data to the database on every single page load you have to surround your code with a check to see if data is available to insert to the database.

Surround your code with:

if (isset( $_REQUEST['SENDER_Name']) && isset($_REQUEST['RECIVER_Name']) && isset($_REQUEST['Amount'])){
    // all your code comes here from database connection start to close
}
  • Related