Jump to content

Adding data...


2old2learn?

Recommended Posts

if( $value == ** )
I am not sure about the '**' and about its purpose.
foreach ($_POST as $value){if( $value == ** ){ echo "You have not filled in all the fields<br>\n"; display the form; exit();}}echo "Welcome";
Here you are using exit() which will stop execution of the script if it enters in the if part for once. I think you dont want to exit the script for invalid inputs i think you want to show error messages instead of that. So you may want to remove exit().I just notice one thing in it
$formValid=true;foreach($_POST as $key => $value){ if(!isset($_POST[$key])){ $formValid = false; }
$formValid will never be false. Cause foreach will iterate through the $_POST keys which is already had been set and the loop will go till it reaches the all set array keys. So i think there is no condition where $_POST[$key] will be not set . So checking for the variable is set or not will be problemetic to determine.I think isset() will be better in this scenerio.
okay clicking " Submit " still add a record even though no entries made into the fields
echo "1 record added";
Here it will not determine that a record has been added realy or not. you may want to use mysql_affected_rows() which will return how many rows has been inserted. UIf you check it in condition in if-else and then only show the echo "1 record added"; when mysql_affected_rows() is one.But as thescintist pointed you have to first assure that correct values are coming to get inserted in database. So you need to take out the validation part first. the processing part, inserting in database part will only be executed when all values are valid.
Link to comment
Share on other sites

  • Replies 251
  • Created
  • Last Reply
I am not sure about the '**' and about its purpose.Here you are using exit() which will stop execution of the script if it enters in the if part for once. I think you dont want to exit the script for invalid inputs i think you want to show error messages instead of that. So you may want to remove exit().I just notice one thing in it$formValid will never be false. Cause foreach will iterate through the $_POST keys which is already had been set and the loop will go till it reaches the all set array keys. So i think there is no condition where $_POST[$key] will be not set . So checking for the variable is set or not will be problemetic to determine.I think isset() will be better in this scenerio.Here it will not determine that a record has been added realy or not. you may want to use mysql_affected_rows() which will return how many rows has been inserted. UIf you check it in condition in if-else and then only show the echo "1 record added"; when mysql_affected_rows() is one.But as thescintist pointed you have to first assure that correct values are coming to get inserted in database. So you need to take out the validation part first. the processing part, inserting in database part will only be executed when all values are valid.
OOOps!
QUOTE if( $value == ** )

typed in wrong should have been ""Can't remember the isset also allowed a empty row to be added..have to check again...Would like it to return a message that not all fields have been entered or entered correctly..

Link to comment
Share on other sites

OOOps!
QUOTE if( $value == ** )

typed in wrong should have been ""Can't remember the isset also allowed a empty row to be added..have to check again...Would like it to return a message that not all fields have been entered or entered correctly..

ok, lets take a step back for a second. First let's establish the structure I'm trying to explain, and we can determine whether empty or isset is more practical afterwards. Here's the code snippet I've been giving you, with your code inserted. We could modify it to match the example you found where it just check's for empty strings, but for now, let's understand what I've been trying to say.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = false;//here we find out if any value are not set, to set a basic validation flag//we can modify this to the example you found, but the point is to understand//why we are doing this in the first place.foreach($_POST as $key => $value){  if(!isset($_POST[$key])){	$formValid = false;  };};?>//if at this point $formValid is still == true, we can be sure that our basic form validation//has passed and we can proceed with doing all our database stuff//else, we can output an error to the userif($formValid){  $con = mysql_connect("localhost","root","");  if (!$con){   die('Could not connect: ' . mysql_error());  };  mysql_select_db("tecicc", $con);  $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered,   supplier, date_job_completed, date_billing_given_to_accounting)values('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";  if (!mysql_query($sql,$con)){	die('Error: ' . mysql_error());  }  echo "1 record added";  mysql_close($con);}else{  echo 'there was a problem with your form';};?> </body></html>

that's all I was trying to get at. Hope the bigger picture is clearer now. Once we establish the basic flow and control structures, we can tweak it to suit your needs.

Link to comment
Share on other sites

ok, lets take a step back for a second. First let's establish the structure I'm trying to explain, and we can determine whether empty or isset is more practical afterwards. Here's the code snippet I've been giving you, with your code inserted. We could modify it to match the example you found where it just check's for empty strings, but for now, let's understand what I've been trying to say.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = false;//here we find out if any value are not set, to set a basic validation flag//we can modify this to the example you found, but the point is to understand//why we are doing this in the first place.foreach($_POST as $key => $value){  if(!isset($_POST[$key])){	$formValid = false;  };};?>//if at this point $formValid is still == true, we can be sure that our basic form validation//has passed and we can proceed with doing all our database stuff//else, we can output an error to the userif($formValid){  $con = mysql_connect("localhost","root","");  if (!$con){   die('Could not connect: ' . mysql_error());  };  mysql_select_db("tecicc", $con);  $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered,   supplier, date_job_completed, date_billing_given_to_accounting)values('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";  if (!mysql_query($sql,$con)){	die('Error: ' . mysql_error());  }  echo "1 record added";  mysql_close($con);}else{  echo 'there was a problem with your form';};?> </body></html>

that's all I was trying to get at. Hope the bigger picture is clearer now. Once we establish the basic flow and control structures, we can tweak it to suit your needs.

Okay I will modify my main script to include your formvalid snipet...right now just got home and making dinner..then will make the change..thanks..
Link to comment
Share on other sites

Okay I've added the $formvalid script to my insert.php script like you say I should start doing...so in doing that and test I do get message" sorry, there was a problem with the form. please try again1 record added " but still also get the message 1 record added..and I check my database and a blank row has been added..Here is the newly edited script..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = true;?><?phpforeach($_POST as $key => $value){  if(empty($_POST[$key])){	$formValid = false;  };};if($formValid){  //create query and execute call to the database}else{  echo ' sorry, there was a problem with the form.  please try again';};?><?php $con = mysql_connect("localhost","root",""); if (!$con)   {   die('Could not connect: ' . mysql_error());   } mysql_select_db("tecicc", $con); $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting) values ('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";if (!mysql_query($sql,$con))   {   die('Error: ' . mysql_error());   } echo "1 record added"; mysql_close($con); ?> </body></html>

so..I think not sure though making headway...yes/no...but it still add a blank row even though it says " sorry, there was a problem with the form. please try again "..is headway being made now????? eh!

Link to comment
Share on other sites

There's nothing there to stop the insert from happening if the data isn't valid. You have a comment in the if statement to run the query if the statement was true, but the actual query is below the if statement where it gets executed regardless of what happened with the if statement.

Link to comment
Share on other sites

There's nothing there to stop the insert from happening if the data isn't valid. You have a comment in the if statement to run the query if the statement was true, but the actual query is below the if statement where it gets executed regardless of what happened with the if statement.
Do you mean this comment??
//create query and execute call to the database
I just removed it .. and about to run a test again...thanks if thats what your talking about...Well tested and still same result...I am hoping that I am starting to catch on to the info of what you guys are saying..also after it says either error occurred or 1 record added I would like it to redirect to home page(index.php)
Link to comment
Share on other sites

The place where that comment was is where you should have all of the code to build and run the query. Right now the code to run the query is not in an if statement, so it always gets executed.
Hmmm I think I know exactly what you are saying..I hope...
if($formValid){  $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting) values ('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";}else{  echo ' sorry, there was a problem with the form.  please try again';};?>

Hope this is what you mean...my fingers crossed it is....LOL

Link to comment
Share on other sites

Put all of the code you want to run there, from the database connection down to the line saying that a record was inserted. The entire insert process.
Okay Bud...thanks I think I have it here is the code...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = true;?><?phpforeach($_POST as $key => $value){  if(empty($_POST[$key])){	$formValid = false;  };};if($formValid){  $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting) values ('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";}else{  echo ' sorry, there was a problem with the form.  please try again';};?><?php $con = mysql_connect("localhost","root","");  if (!$con) if (!mysql_query($sql,$con))   {   die('Could not connect: ' . mysql_error());   } mysql_select_db("tecicc", $con);    {   die('Error: ' . mysql_error());   } echo "1 record added"; mysql_close($con); ?> </body></html>

But I see the " Error" at the end of the message..

sorry, there was a problem with the form. please try againError:
So honestly you think I am starting to get the understanding...???
Link to comment
Share on other sites

I haven't worked with you enough to know if you're understanding or not, but just look at the structure of the code. You have the if statement checking if the form is valid. If the form is valid, you create a query (but don't run it). If the form is not valid, you print an error message. Then, regardless of whether or not the form is valid, you connect to the database and try to run the query that you created, then print the response message. The problem is that you only create the query if the form passed, but you always try to run the query. All of the insert code, from creating the database connection, to building the query, to running the query and printing the output needs to go in that if statement, not just the part where you build the query.Also, this code isn't structured correctly:

if (!$con) if (!mysql_query($sql,$con))   {   die('Could not connect: ' . mysql_error());   }mysql_select_db("tecicc", $con);   {   die('Error: ' . mysql_error());   }

Read that line-by-line, it doesn't make sense. It should be structured like this:

if (!$con){  die('Could not connect: ' . mysql_error());}mysql_select_db("tecicc", $con);if (!mysql_query($sql,$con)){  die('Error: ' . mysql_error());}

Link to comment
Share on other sites

I'm not sure what you are trying to change. did you try the code as I posted it? The whole point is to only execute the database stuff IF the condition is true. You really shouldn't have had to change anything I gave you. Maybe just study my code snippet some more? I tried to give it to you exactly as was intended... :)edit: I'm also confused why you kept adding opening and closing php tags. It's all one script. you should only need one opening and one closing php tag.anyway, have you read some of the tutorials? They will give you the basics on stuff like control structures and whatnot.http://www.w3schools.com/php/php_syntax.asp

Link to comment
Share on other sites

I haven't worked with you enough to know if you're understanding or not, but just look at the structure of the code. You have the if statement checking if the form is valid. If the form is valid, you create a query (but don't run it). If the form is not valid, you print an error message. Then, regardless of whether or not the form is valid, you connect to the database and try to run the query that you created, then print the response message. The problem is that you only create the query if the form passed, but you always try to run the query. All of the insert code, from creating the database connection, to building the query, to running the query and printing the output needs to go in that if statement, not just the part where you build the query.Also, this code isn't structured correctly:
if (!$con) if (!mysql_query($sql,$con))   {   die('Could not connect: ' . mysql_error());   }mysql_select_db("tecicc", $con);   {   die('Error: ' . mysql_error());   }

Read that line-by-line, it doesn't make sense. It should be structured like this:

if (!$con){  die('Could not connect: ' . mysql_error());}mysql_select_db("tecicc", $con);if (!mysql_query($sql,$con)){  die('Error: ' . mysql_error());}

Okay cool..but made the change you suggested..what I meant that I understood your first suggestion of place the excutable code where it belongs..thanks...you guys are great...
Link to comment
Share on other sites

I'm not sure what you are trying to change. did you try the code as I posted it? The whole point is to only execute the database stuff IF the condition is true. You really shouldn't have had to change anything I gave you. Maybe just study my code snippet some more? I tried to give it to you exactly as was intended... :)edit: I'm also confused why you kept adding opening and closing php tags. It's all one script. you should only need one opening and one closing php tag.anyway, have you read some of the tutorials? They will give you the basics on stuff like control structures and whatnot.http://www.w3schools.com/php/php_syntax.asp
Hey I give you.. alot of credit to what I am getting.." justsomeguy " noted something I tried it and now the script runs..but no row is added..it says an error occurred..yes, I have quickly looked over the tutorials..but will again at a slower pace..I will also look at the added php tags as you suggested..I am looking to clean it up like you said..babe steps...Okay now with yours and the help of " justsomeguy" it works...no records added when the input boxes are left empty..and shows this message now...
sorry, there was a problem with the form. please try againError:
Link to comment
Share on other sites

okay here is with tags cleared..is this right??

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = true;foreach($_POST as $key => $value){  if(empty($_POST[$key])){	$formValid = false;  };};if($formValid){    $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting) values ('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";}else{  echo ' sorry, there was a problem with the form.  please try again';}; $con = mysql_connect("localhost","root","");  if (!$con) if (!mysql_query($sql,$con))   {   die('Could not connect: ' . mysql_error());   }   	mysql_select_db("tecicc", $con);	   {   die('Error: ' . mysql_error());   } echo "1 record added"; mysql_close($con); ?> </body></html>

Link to comment
Share on other sites

nope. I'm not sure what I can do to make it any clearer. You just really need to understand if/else statements. You are still putting code that should only happen if the form is valid outside of the if condition. Take it slow, and look at my example again.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = false;//here we find out if any value are not set, to set a basic validation flag//we can modify this to the example you found, but the point is to understand//why we are doing this in the first place.foreach($_POST as $key => $value){  if(!isset($_POST[$key])){	$formValid = false;  };};?>//if at this point $formValid is still == true, we can be sure that our basic form validation//has passed and we can proceed with doing all our database stuff//else, we can output an error to the userif($formValid){  $con = mysql_connect("localhost","root","");  if (!$con){   die('Could not connect: ' . mysql_error());  };  mysql_select_db("tecicc", $con);  $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered,   supplier, date_job_completed, date_billing_given_to_accounting)values('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";  if (!mysql_query($sql,$con)){	die('Error: ' . mysql_error());  }  echo "1 record added";  mysql_close($con);}else{  echo 'there was a problem with your form';};?> </body></html>

Now look at what you did.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = true;foreach($_POST as $key => $value){  if(empty($_POST[$key])){	$formValid = false;  };};if($formValid){    $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting)values('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";}else{  echo ' sorry, there was a problem with the form.  please try again';};///////////////////////////////////////////////////////////////this code should be in the if(condition is true) block//of the if/else statement$con = mysql_connect("localhost","root","");if (!$con)if (!mysql_query($sql,$con))   {   die('Could not connect: ' . mysql_error());   }	   mysql_select_db("tecicc", $con);	   {   die('Error: ' . mysql_error());   }echo "1 record added";mysql_close($con);/////////////////////////////////////////////////////////////////////?> </body></html>

Can you see the difference? I also noticed you're still doing this weird thing..

if (!$con)if (!mysql_query($sql,$con))   {   die('Could not connect: ' . mysql_error());   }	   mysql_select_db("tecicc", $con);	   {   die('Error: ' . mysql_error());   }echo "1 record added";

Link to comment
Share on other sites

nope. I'm not sure what I can do to make it any clearer. You just really need to understand if/else statements. You are still putting code that should only happen if the form is valid outside of the if condition. Take it slow, and look at my example again.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = false;//here we find out if any value are not set, to set a basic validation flag//we can modify this to the example you found, but the point is to understand//why we are doing this in the first place.foreach($_POST as $key => $value){  if(!isset($_POST[$key])){	$formValid = false;  };};?>//if at this point $formValid is still == true, we can be sure that our basic form validation//has passed and we can proceed with doing all our database stuff//else, we can output an error to the userif($formValid){  $con = mysql_connect("localhost","root","");  if (!$con){   die('Could not connect: ' . mysql_error());  };  mysql_select_db("tecicc", $con);  $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered,   supplier, date_job_completed, date_billing_given_to_accounting)values('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";  if (!mysql_query($sql,$con)){	die('Error: ' . mysql_error());  }  echo "1 record added";  mysql_close($con);}else{  echo 'there was a problem with your form';};?> </body></html>

Now look at what you did.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = true;foreach($_POST as $key => $value){  if(empty($_POST[$key])){	$formValid = false;  };};if($formValid){    $sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting)values('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";}else{  echo ' sorry, there was a problem with the form.  please try again';};///////////////////////////////////////////////////////////////this code should be in the if(condition is true) block//of the if/else statement$con = mysql_connect("localhost","root","");if (!$con)if (!mysql_query($sql,$con))   {   die('Could not connect: ' . mysql_error());   }	   mysql_select_db("tecicc", $con);	   {   die('Error: ' . mysql_error());   }echo "1 record added";mysql_close($con);/////////////////////////////////////////////////////////////////////?> </body></html>

Can you see the difference? I also noticed you're still doing this weird thing..

if (!$con)if (!mysql_query($sql,$con))   {   die('Could not connect: ' . mysql_error());   }	   mysql_select_db("tecicc", $con);	   {   die('Error: ' . mysql_error());   }echo "1 record added";

Okay what I meant of cleaning up..was I got rid of all extra php tags..only one at the start and only one at the end..now I think I re-adjusted the code like you mentioned..correct me where you see the errors..gonna test it now...
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /></head><body><?php$formValid = true;foreach($_POST as $key => $value){  if(empty($_POST[$key])){	$formValid = false;  };};if($formValid){  con = mysql_connect("localhost","root","");  if (!$con){   die('Could not connect: ' . mysql_error());   };   	mysql_select_db("tecicc", $con);		$sql="INSERT INTO twal ( id, srnumber, tenant, floor, tower, job_description, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting) values ('$_POST[id]','$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[tower]','$_POST[job_description]','$_POST[labour_cost]','$_POST[material_cost]','$_POST[date_twa_sent_to_tenant]','$_POST[date_twa_approved]','$_POST[date_parts_ordered]','$_POST[supplier]','$_POST[date_job_completed]','$_POST[date_billing_given_to_accounting]')";	if (!mysql_query($sql,$con)){      die('Error: ' . mysql_error());   } echo "1 record added"; mysql_close($con);}else{echo ' sorry, there was a problem with the form.  please try again';}; ?> </body></html>

If I understand you correctly and the corrected code as you say is right..I just tested and all is well..I get this message now when no data is entered..

sorry, there was a problem with the form. please try again
Is this right now...???
Link to comment
Share on other sites

if it's doing what you want, then I would say so. :):)
Cool, so I am making some head way..but now..how can I still have the form show and the error message show under the form???
Link to comment
Share on other sites

You can copy that PHP code to the top of your form page. You can add an if statement like this to check whether or not the form was even submitted, and if it's not submitted then skip the processing part:

if (isset($_POST['id'])){  // process form}

For the error messages, instead of printing everything put the errors in a variable. Start the variable off as empty, and add errors to it if they happen:

$errors = '';$messages = '';if (isset($_POST['id'])){  // process form  $formValid = true;  foreach($_POST as $key => $value){	if(empty($_POST[$key])){	  $formValid = false;	}  }  if($formValid){	// do database stuff	$messages .= '1 record added<br>';  }  else  {	$errors .= 'sorry, there was a problem with the form.  please try again<br>';  }}

In your HTML, just pick a spot and print the variables out. Since you start them off empty, if the form wasn't processed they won't print anything.

...<body>...<span class="messages"><?php echo $messages; ?></span><span class="errors"><?php echo $errors; ?></span>

Link to comment
Share on other sites

You can copy that PHP code to the top of your form page. You can add an if statement like this to check whether or not the form was even submitted, and if it's not submitted then skip the processing part:
if (isset($_POST['id'])){  // process form}

For the error messages, instead of printing everything put the errors in a variable. Start the variable off as empty, and add errors to it if they happen:

$errors = '';$messages = '';if (isset($_POST['id'])){  // process form  $formValid = true;  foreach($_POST as $key => $value){	if(empty($_POST[$key])){	  $formValid = false;	}  }  if($formValid){	// do database stuff	$messages .= '1 record added<br>';  }  else  {	$errors .= 'sorry, there was a problem with the form.  please try again<br>';  }}

In your HTML, just pick a spot and print the variables out. Since you start them off empty, if the form wasn't processed they won't print anything.

...<body>...<span class="messages"><?php echo $messages; ?></span><span class="errors"><?php echo $errors; ?></span>

Okay just remember I am taking baby steps so I maybe slow on giving results..So your first example code:
if (isset($_POST['id'])){  // process form}

I did add this to your example code:

if (isset($_POST['id'])){  include('twal.php');}

TESTEDThis works, it displays the form and the message " Sorry, there was a problem with the form. Please check all data entries, try again " for a blank entry And for a filled order it works form shows and " 1 Record Added " is also displayed..ThanksThe rest of your examples haven't tried or figured out yet.. :) :)

Link to comment
Share on other sites

Now I would like to take this time to offer my thanks to the following members here for all their help in the starting of my project and getting it off the ground..I gave a small preview of what has been doing..and they were all smiles of what they saw..I thank you guys/girls..for all your insightful input..and these members are:justsomeguythescientistbirbalIf it wasn't for your help this wouldn't have started..I look forward to further insightful knowledge from these members and count on their knowledge base...as I continue with this project..and just in a short time..I think I made some headway in learning php scripting but still know I have a long long way to go..Thank You

Link to comment
Share on other sites

Hey reading over some php/mysql books I see I can create drop down list, checkboxes, radio buttons...greatNow here is a questionIf I create a drop down list say: Locationex: Location: Location 1 Location 2 Location 3Now say I select " Location 3 ", once I finish all my entries, and then click " Submit " button, can it be sent to a separate table called " Location 3 " vice versa with " Location 2 " or " Location 1 "What I mean is I have 3 separate tables called location1, location2, location3..so once I select the appropriate location it will be saved in the appropriate table..??? Can this be done...

Link to comment
Share on other sites

Archived

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


×
×
  • Create New...