Jump to content

[SOLVED] how to check database


ngreenwood6

Recommended Posts

I am trying to set-up a script that will check to see if there email is already there or not. if it is it will give them an error and not insert it into the database and if it is not then it will insert it into the database. this is my code:

 

<?php


//include our variables
include ("variables.php");

//include database variables
include ("db.php");

//variable to connect to database
$mysqli_connect = mysqli_connect($host,$db_username,$db_password,$db_name)
or die ("Could not connect to database");

//variable to send data to the database
mysqli_query($mysqli_connect,$insertinto_db)
or die ("Error: ".mysqli_error($mysqli_connect));

//error handling
if (!$firstname)
{
echo ("Please enter a first name");
}
else if (!$lastname)
{
echo ("Please enter  last name");
}
else if (!$age)
{
echo ("Please enter your age");
}
else if (!$weight)
{
echo ("Please enter your weight");
}
else if ($age > 99)
{
echo ("Please enter a valid age");
}
else if ($weight > 999)
{
echo ("Please enter a valid weight");
}
else
{
echo ("Thank you for your submission $firstname!");
}
?>

 

This works as far as telling them thank you or giving them the error. The problem is whenever there is an error it still puts the data in the database. Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/117633-solved-how-to-check-database/
Share on other sites

i'm going to guess that it's because you're inserting the data before even running any error checks.  that means that even if your script throws an error, it doesn't matter because the insertion has already been performed.

 

move the query to the else case in your big long if-else set:

 

//error handling
if (!$firstname)
{
echo ("Please enter a first name");
}
else if (!$lastname)
{
echo ("Please enter  last name");
}
else if (!$age)
{
echo ("Please enter your age");
}
else if (!$weight)
{
echo ("Please enter your weight");
}
else if ($age > 99)
{
echo ("Please enter a valid age");
}
else if ($weight > 999)
{
echo ("Please enter a valid weight");
}
else
{
//variable to send data to the database
mysqli_query($mysqli_connect,$insertinto_db)
or die ("Error: ".mysqli_error($mysqli_connect));
echo ("Thank you for your submission $firstname!");
}

i posted the code to reflect what i meant.  move these lines:

 

//variable to send data to the database
mysqli_query($mysqli_connect,$insertinto_db)
or die ("Error: ".mysqli_error($mysqli_connect));

 

which are the lines that are executing your query for you, into the else case of your big conditional block:

 

else
{
//variable to send data to the database
mysqli_query($mysqli_connect,$insertinto_db)
or die ("Error: ".mysqli_error($mysqli_connect));
echo ("Thank you for your submission $firstname!");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.