Inserting data into database

Hello Everyone i want to insert data in mysql using a dropdown show/hide div where only if the user selects others div will show and insertion of data takes place where as if not required data won’t get inserted?

Here is my code from the form Input.

<script type="text/javascript">
    function ShowHideDiv() {
        var ddlPassport = document.getElementById("ddlPassport");
        var dvPassport = document.getElementById("dvPassport");
        dvPassport.style.display = ddlPassport.value == "others" ? "block" : "none";
    }
</script>
    
<div class="col-25">
    <label>Nationality?</label></div>
    <div class="col-25">
        <select name="nationality" id = "ddlPassport" onchange = "ShowHideDiv()">
        <option value="indian">INDIAN</option>
        <option value="others">Others</option>
        </select></div><br>
<div class="col-75" id="dvPassport" style="display: none">
    Country:
    <input type="text" style="font-size:15px;" id="txtPassportNumber" name="country"><br><br>
    Passport Number:
    <input type="text" style="font-size:15px;" id="txtPassportNumber" name="passportno"><br><br>
    Passport Validity:
    <input type="date" id="txtPassportNumber" name="passportval">
</div>
</div>

If selected INDIAN the div for others will not get inserted but only indian option will get inserted in the database. Kindly someone help. Thanks.

This is my PHP Code for the form.

$mobile=$_POST['mobile'];
$email=$_POST['email'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];    
$purpose=$_POST['purpose'];   
$passtype=$_POST['passtype'];
$company=$_POST['company'];
$validupto=$_POST['validupto']; 
$doctype=$_POST['doctype'];      
$docnumber=$_POST['docnumber'];
$address=$_POST['address'];
$whomtomeet=$_POST['whomtomeet'];     
$designation=$_POST['designation'];
$gender=$_POST['gender'];
$nationality=$_POST['nationality'];
$country=$_POST['country'];      
$passportno=$_POST['passportno'];
$passportval=$_POST['passportval'];
      
      
	 $query="INSERT INTO `visitors` (`mobile`,`email`,`firstname`,`lastname`,`purpose`,`passtype`,`company`,`validupto`,`time`,`date`,`doctype`,`docnumber`,`address`,`whomtomeet`,`designation`,`gender`,`nationality`,`country`,`passportno`,`passportval`) VALUES ('$mobile','$email','$firstname','$lastname','$purpose','$passtype','$company','$validupto',current_timestamp(),current_timestamp(),'$doctype','$docnumber','$address','$whomtomeet','$designation','$gender','$nationality','$country','$passportno','$passportval')";
	

First you would build the part of your query which is always the same.
Then you would need a condition.

if($_POST['nationality'] === 'Others'){
     // Add more data to the query
}

In the condition, you would add the extra data to the query.

None of this is any use.
The first part, passing $_POST data straight to variables is a waste of code, it achieves nothing.
The next part, the query, is how you should not write an insert query.
Don’t put variables directly into a query. Replace those with placeholders. Then at the same time you build the query, build an array of the data variables. Then you can prepare the query, then pass in the data arrow on execute.

Sir i have tried this but instead i can’t insert the data only INDIAN option without inserting the others date which are country…etc!! kindly help.


<form method="post" name="ronel">
<script type="text/javascript">
    function ShowHideDiv() {
        var ddlPassport = document.getElementById("ddlPassport");
        var dvPassport = document.getElementById("dvPassport");
        dvPassport.style.display = ddlPassport.value == "others" ? "block" : "none";
    }
</script>
    
<div class="col-25">
    <label>Nationality?</label></div>
    <div class="col-25">
        <select name="nationality" id = "ddlPassport" onchange = "ShowHideDiv()">
        <option value="indian">INDIAN</option>
        <option value="others">Others</option>
        </select></div><br>
<div class="col-75" id="dvPassport" style="display: none">
    Country:
    <input type="text" style="font-size:15px;" id="txtPassportNumber" name="country"><br><br>
    Passport Number:
    <input type="text" style="font-size:15px;" id="txtPassportNumber" name="passport"><br><br>
    Passport Validity:
    <input type="date" id="txtPassportNumber" name="passportval">
</div>
<div class="row">
           
      <input type="submit" value="Submit" name="ronel">
      </div>
          
  </form>
<?php
$host="localhost";
$username="root";
$pass="";
$db="demo";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Database connection error");
}

if(isset($_POST['ronel'])){


  if($_POST['nationality'] === 'Others'){
     // Add more data to the query

$country=$_POST['country'];
$passport=$_POST['passport'];
$passportval=$_POST['passportval'];    

 
	 $query="INSERT INTO `nationality` (`nationality`,`country`,`passport`,`passportval`) VALUES ('$nationality','$country','$passport','$passportval')";
	
	$res=mysqli_query($conn,$query);
	if($res){
		$_SESSION['success']="Not Inserted successfully!";
		header('Location:');
	}else{
		echo "<script>alert('Data not inserted! Kindly contact ITSUPPORT OTPC');</script>";
	}
      
	}
}
?>


this screenshot is being inserted in the database. But when i try to insert only the indian i get error?

With this line

if($_POST['nationality'] === 'Others'){

I’m surprised it gets into the attempt to insert the extra information, as you define it as:

<option value="others">Others</option>

Doesn’t the lower-case “o” mean that the if() check will never be true? Or is it a typo in the forum post?

Typo aside, where is the code for the insert when nationality is not “others”? And why, if the query with extra columns works correctly, do you set this session variable to what seems like a confusing value?

if($res){
    $_SESSION['success']="Not Inserted successfully!";
    header('Location:');

After you do a header redirect, you should call exit() to stop the rest of the script running.

Dear Sir i am still unable to figure out and data cannot be inserted!

if(isset($_POST['ronel'])){


  if($_POST['nationality'] === 'others'){
     // Add more data to the query

$country=$_POST['country'];
$passport=$_POST['passport'];
$passportval=$_POST['passportval'];    

 
	 $query="INSERT INTO `nationality` (`country`,`passport`,`passportval`) VALUES ('$country','$passport','$passportval')";

If you try the query, with the same values as you get from your form, in something like phpmyadmin, does that give you any clue as to what the problem might be? It might give you an error message to help figure it out.

Do you have any columns that cannot be NULL, for example, that are not in your query? Your earlier code inserted the nationality column, for example, and this later one does not. Perhaps you could show the table structure.

Sir this is my table structure kindly help me

OK, so that shows you that the nationality column cannot be NULL, except that your query:

$query="INSERT INTO `nationality` (`country`,`passport`,`passportval`) 
      VALUES ('$country','$passport','$passportval')";

doesn’t mention it and it has no default. That query will not work until you include the nationality column.

You’ll also need to look at the format of passportval as it is a date column and will want the date to be supplied in yyyy-mm-dd format, the opposite way around to your form prompt.

yes i got it and it is working i can input the data but the when i don’t want the div’s data to insert like without selecting others only india option is not getting inserted.

Well, show us the code for processing the query when “others” is not selected. The code you have posted so far only show the query if “others” is selected.

This is the full code sir.

<form method="post" name="ronel">
<script type="text/javascript">
    function ShowHideDiv() {
        var ddlPassport = document.getElementById("ddlPassport");
        var dvPassport = document.getElementById("dvPassport");
        dvPassport.style.display = ddlPassport.value == "others" ? "block" : "none";
    }
</script>
    
<div class="col-25">
    <label>Nationality?</label></div>
    <div class="col-25">
        <select name="nationality" id = "ddlPassport" onchange = "ShowHideDiv()">
        <option value="indian">INDIAN</option>
        <option value="others">Others</option>
        </select></div><br>
<div class="col-75" id="dvPassport" style="display: none">
    Country:
    <input type="text" style="font-size:15px;" id="txtPassportNumber" name="country"><br><br>
    Passport Number:
    <input type="text" style="font-size:15px;" id="txtPassportNumber" name="passport"><br><br>
    Passport Validity:
    <input type="date" id="txtPassportNumber" name="passportval">
</div>
<div class="row">
           
      <input type="submit" value="Submit" name="ronel">
      </div>
          
  </form>
<?php
$host="localhost";
$username="root";
$pass="";
$db="demo";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Database connection error");
}

if(isset($_POST['ronel'])){


  if($_POST['nationality'] === 'others'){
     // Add more data to the query

$nationality=$_POST['nationality'];
$country=$_POST['country'];
$passport=$_POST['passport'];
$passportval=$_POST['passportval'];    

 
	 $query="INSERT INTO `nationality` (`nationality`,`country`,`passport`,`passportval`) VALUES ('$nationality','$country','$passport','$passportval')";

No, that’s the code for “others”, you need an else to handle the situation when the nationality is not “others”.

Sir how do i write the else statement kindly tell mw thank you.
i have tried like this!!

else ($_POST['nationality'] === 'indian');

Because you’re missing the if, the result of that will be that it’ll either throw a syntax error, or it’ll just set the $_POST variable to that value.

You already have an else clause where you check whether $res is true, it’s the same kind of thing. Something like:

if ($_POST['nationality'] === "others") { 
  // do all the stuff needed if it's "others"
  }
else {
  // do all the stuff needed if it's not "others"
}

As you have a <select> that can only be either “indian” or “others”, there’s no need to be any more specific than to say “else” - any other value will come into here, and in your case there’s only the one other possibility. From a security point of view at some point you will perhaps want to check that the value is “indian”, which could be written as

if ($_POST['nationality'] === "others") { 
  // do all the stuff needed if it's "others"
  }
else if ($_POST['nationality'] === "indian") {
  // do all the stuff needed if it's "indian"
}
else {
 // do whatever you need to if it's a different value somehow
}

If you end up with a lot of different options for “nationality”, I would recommend you look at switch as IMO it’s neater than a load of else if clauses. PHP: switch - Manual

so this is what i have done and i can insert the data now kept values to NULL. Thanks a lot sir.

$nationality=$_POST['nationality'];
$country=$_POST['country'];
$passport=$_POST['passport'];
$passportval=$_POST['passportval'];    

 
	 $query="INSERT INTO `nationality` (`nationality`,`country`,`passport`,`passportval`) VALUES ('$nationality','$country','$passport','$passportval')";

	$res=mysqli_query($conn,$query);
	if($res){
		$_SESSION['success']="Not Inserted successfully!";
		header('Location:');
	}else{
		echo "<script>alert('Data not inserted! Kindly contact ITSUPPORT OTPC');</script>";
	}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.