Hey guys,
I'm hoping this is an easy solution to the following IF statements I have listed in my code below.
The validations work to an extent, however I'm running into contradictions between verify blank fields, as well verifying non unique values within the first field ($p_name).
Scenario 1 -> Entered/Submitted with blank values in all fields (Success - validation returned)
Scenario 2 -> Entered/Submitted unique values into all 4 fields (Success - data inserted)
Scenario 3 -> Entered/Submitted non unique value in the first field (Success - validation returned)
Scenario 4 -> Entered/Submitted with blank value in the first field, and the remaining 3 fields contain values (Failure - validation returned, however the data is still inserted from the 3 populated fields).
I'm basically seeing what I can do to resolve Scenario 4. Huge thanks in advance.
<?php
//including the database connection file
include_once("config.php");
if(isset($_POST['Submit']))
{
$p_name=$_POST['pie_name'];
$u_profile=$_POST['user_profile'];
$env=$_POST['environment'];
$instance=$_POST['instance_number'];
// checking empty fields
if(empty($p_name) || empty($u_profile) || empty($env) || empty($instance))
{
//if PIE Name field is empty
if(empty($p_name))
{
echo "<font color='red'>Pie Name field is empty.</font><br/>";
}
//if User Profile field is empty
if(empty($u_profile))
{
echo "<font color='red'>User Profile field is empty.</font><br/>";
}
//if Environment field is empty
if(empty($env))
{
echo "<font color='red'>Environment field is empty.</font><br/>";
}
//if Instance Number field is empty
if(empty($instance))
{
echo "<font color='red'>Instance field is empty.</font><br/>";
}
//link to the previous page
{
// echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
echo "<br/>Check 1!!!!!</a>";
}
}
if(!empty($p_name) || !empty($u_profile))
{
//if PIE Name field already exists
if(mysql_num_rows(mysql_query("SELECT pie_name FROM pies WHERE pie_name = '$p_name'")))
{
echo "<font color='red'>Pie Name is not unique.</font><br/>";
echo "<br/>Check 2!!!!!</a>";
}
else
{
// considering System Profile ID as auto increment - primary key
$add_to_system_profile = "insert into system_profile (environment,user_profile,instance_number) values ('$env','$u_profile','$instance')";
mysql_query($add_to_system_profile);
$last_inserted_mysql_id = mysql_insert_id();
$add_to_pies_query = "insert into pies (pie_name, s_id) values ('$p_name','$last_inserted_mysql_id')";
mysql_query($add_to_pies_query);
// display success message
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
}
}
?>