-1

I am new to PHP. I have developed one PHP page containing a simple form and it's working fine. All data are inserted correctly but sometimes the data is not inserted. Why this happens?

$sql="INSERT INTO form_tbl(name,age,dob) values('$name','$age','$dob')";
$exe=mysqli_query($conn,$sql);

if($exe){
  echo "success";
}else{
  echo "Submit again";
}
Naren
  • 1
  • 2

1 Answers1

0

Couple of things to look for:

Make sure all your variables are not empty or else make sure your table is set up to allow NULL values.

if(isset($name) && $name!=""){
    //exists
}else{
    //doesn't exist or is empty
}

Make sure your variables are of the right type, ex., int, varchar, etc

Paddy Hallihan
  • 1,624
  • 3
  • 27
  • 76
  • yes i allow null values in tables but is that will create an problem.how? – Naren Feb 20 '19 at 06:16
  • Not necessarily, it depends what you are developing. For example you wouldn't allow NULL values if the fields were required. In this case if one of your values wasn't filled in and the table didn't allow a NULL value then it wouldn't insert anything. – Paddy Hallihan Feb 20 '19 at 09:07
  • To troubleshoot this further we'd need more information. Like where are you getting the variables from or how are you validating them (e.g. make sure it's an int or a string, etc,.) Add the following before what you have for troubleshooting: `echo $name; echo $age; echo $dob; echo gettype($name); echo gettype($age); echo gettype($dob);` – Paddy Hallihan Feb 20 '19 at 09:12