2old2learn? Posted May 23, 2011 Share Posted May 23, 2011 Hey; I hope I can explain this properly for you to understand.. I have a form with a select option and wanting to have when user selects one of the drop down options it , then submit is clicked that it saves to the selected option... Here is what I think is right ..but could be wrong..if anything would like to know if I am close..???...LOL sample code: <html><body><?php //Select table and insert info to selected table $select = table(""); if ($select == "xyz") { $sql="INSERT INTO xyz ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')"; } elseif ($select == "zyx") { $sql="INSERT INTO zyx ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')"; } elseif ($select == "yxz") { $sql="INSERT INTO yxz ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')"; } elseif ($select == "yxz1") { $sql="INSERT INTO yxz1 ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')"; } endif { echo "Sorry no db found"; } ?></body></html> ThanksEdited 6:05am ..Just looking script over I think it maybe wrong..the select is called when the location is displayed..so I think it should be like this..not sure though... //Select table and insert info to selected table $select from table== field("location"); The selection is in the field " Location" which directs the selected field to be saved to the same table name...Example :If location is " thisdb" then also table name is " thisdb "...so info is sent to that database... Link to comment Share on other sites More sharing options...
birbal Posted May 23, 2011 Share Posted May 23, 2011 $_POST[tenant] index of it in associative array should be quoted $_POST['tenant'] else it will take it as CONSTANT first and throw some notice and then php will assume it as a string. but it will already throw a error. same with other associative array. } endif echo "Sorry no db found"; }there is proably a bracet miss match and there is no endif there. probably you need 'else' there unless you are not going to add another condition. if there is need to add another condition you need to use iflese thenother than that it is looks fine and will serve the purpose but If table name is too many then rather than writing the query part again and again with if else it will be better to use 'switch case' to check (aka validate) the value for $select then use it in the query in the place of table name. and then executing itrather than that i would go for something like that with less amount of code....assuming $select is an option coming from drop down.in_array() will check that certain value is in an array or not.$validTable=array('xyz','zyx','xyz1'); // prepare table name which should be considered validif(in_array($select,$vaildTable)) //It will check that $select is a in valid list of validTable array. { // table is valid in $select prepare for DB work $sql="INSERT INTO $select ( srnumbe.......}else{echo 'No matched table there';} Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 $_POST[tenant] index of it in associative array should be quoted $_POST['tenant'] else it will take it as CONSTANT first and throw some notice and then php will assume it as a string. but it will already throw a error. same with other associative array.there is proably a bracet miss match and there is no endif there. probably you need 'else' there unless you are not going to add another condition. if there is need to add another condition you need to use iflese thenIf table name is too many then rather than writing the query part again and again with if else it will be better to use 'switch case' to check (aka validate) the value for $select then use it in the query in the place of table name. and then executing itrather than that i would go for like that with less amount of code...something like...assuming $select is an option coming from drop down.in_array() will check that certain value is in an array or not.$validTable=array('xyz','zyx','xyz1'); // prepare table name which should be considered validif(in_array($select,$vaildTable)) //It will check that $select is a in valid list of validTable array. { // table is valid in $select prepare for DB work $sql="INSERT INTO $select ( srnumbe.......}else{echo 'No matched table there';} If you mean $select is in the drop down the answer is no...but xyz,zyx,xyz1 are part of the drop down...that are selected..and once one of those options is select and the rest of the form fields are completed..then user clicks submit..and all data is sent to relevent table...But I like your idea of an array..certainly makes it easier and shorter coding.. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 okay here is the code for the select: <td>Location:</td><td><select name="location" > <option value="Selection"> Make Selection </option> <option value="xyz"> xyz </option> <option value="yzx"> yzx </option> <option value="xzy"> xzy </option> <option value="xyz1"> xyz1 </option></td> When one of the options is selected and the rest of the form fields is completed then submit is clicked and the data is sent to the option selected table...above is just part of form Link to comment Share on other sites More sharing options...
birbal Posted May 23, 2011 Share Posted May 23, 2011 then select data will be in $_POST['location'] or $_GET['location'] depending on your form method post or get relatively . then You should check for $_POST['lcoation'] or $_GET['location'] for valid value and then use it in query if value is valid. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 then select data will be in $_POST['location'] or $_GET['location'] depending on your form method post or get relatively . then You should check for $_POST['lcoation'] or $_GET['location'] for valid value and then use it in query if value is valid.Its $_POST['location'] method..So would it be something like this...??$Select from table where $_POST['location']; Link to comment Share on other sites More sharing options...
birbal Posted May 23, 2011 Share Posted May 23, 2011 Its $_POST['location'] method..So would it be something like this...??are not you selecting the table name from that drop down?so that it will be like$select=$_POST['location'];$sql="INSERT INTO $select ( srnumbe....... here $select will evaluate the table name in the query.and $_POST['location'] is not method it is a super global associative array Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 are not you selecting the table name from that drop down?so that it will be like$select=$_POST['location'];$sql="INSERT INTO $select ( srnumbe....... here $select will evaluate the table name in the query.and $_POST['location'] is not method it is a super global associative array Yes i am selecting table name...from locationWhat about this??? Is this right then also...if ($select == "xyz") or if ($select =$_POST['location']) Link to comment Share on other sites More sharing options...
birbal Posted May 23, 2011 Share Posted May 23, 2011 $select=$_POST['location];if ($select == "xyz") or if ($_POST['location'] == "xyz") will be right. watch for '==' double equal to. there is diffrenec in double equal to and single equal to. double equal to check for condition where as single equal to assighn value. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 $select=$_POST['location];if ($select == "xyz") or if ($_POST['location'] == "xyz") will be right. watch for '==' double equal to. there is diffrenec in double equal to and single equal to. double equal to check for condition where as single equal to assighn value. okay about double equal signs..so I did sort of manage to get it right..thanks...I went with this...if($_POST['location'] ="xyz")elseif($_POST['location'] ="yxz") and so on..until last database is selected... Link to comment Share on other sites More sharing options...
thescientist Posted May 23, 2011 Share Posted May 23, 2011 like birbal already mentioned, you need to use == when doing comparisons or else after the first if, $_POST['location'] will equal xyz Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 like birbal already mentioned, you need to use == when doing comparisons or else after the first if, $_POST['location'] will equal xyz Oh I miss understood it..so it has to be " == " instead of " = " ...thanks..will make correction.. Link to comment Share on other sites More sharing options...
thescientist Posted May 23, 2011 Share Posted May 23, 2011 yes. one is for assignment, two and three are for comparing.http://www.w3schools.com/php/php_operators.asp Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 yes. one is for assignment, two and three are for comparing.http://www.w3schools.com/php/php_operators.asp thanks looking at it further it makes alot of sense..the best part is I am starting to understand this a little more and more..at least I think I am... Link to comment Share on other sites More sharing options...
2old2learn? Posted June 5, 2011 Author Share Posted June 5, 2011 Sorry even after being given the answers..I didn't do anything testing of it for a long time as a matter fact..was busy with other things...but today started working again on project..and ran into some little errors.. Parse error: syntax error, unexpected '{' in C:\Program Files\xampp\htdocs\tecicc\insert.php on line 49Below is my if/elseif statements and I get..error message... $select=$_POST['location']; if ( $select= '250y' ) { $sql="INSERT INTO 250y ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')";} elseif($select == '20q') { $sql="INSERT INTO 20q ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')";} elseif($select== '1dundas') { $sql="INSERT INTO 1dundas ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')";} else($select=='galleria_mall') { <<<<< Line 49 error message.. $sql="INSERT INTO galleria_mall ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')";} I hope I am getting this right... Link to comment Share on other sites More sharing options...
thescientist Posted June 6, 2011 Share Posted June 6, 2011 do all your curly braces match? the answer should be right around line 49... Link to comment Share on other sites More sharing options...
2old2learn? Posted June 6, 2011 Author Share Posted June 6, 2011 Well, I've looked it over and over I guess I need fresh eyes..to find the "{" error... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /><?phpinclude('config.php');foreach($_POST as $key => $value){ if(empty($_POST[$key])){ $formValid = false; };};?></head><body><?php$formValid = true;if($formValid){ //$con = mysql_connect("localhost","root",""); // if (!$con){ //die('Could not connect: ' . mysql_error()); // }; // mysql_select_db("tecicc", $con); //$select = $_POST['location']; $validTable=array('250y','20q','1dundas','galleria_mall'); if(in_array($select,$vaildTable)) //if ($_POST['location'] = "20q") { $sql="INSERT INTO $select ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')";} if (!mysql_query($sql,$con)){ die('Error: ' . mysql_error()); } echo "<script type='text/javascript'>alert('1 Record added');</script>"; if (isset($_POST['srnumber'])) { include('index_sr.php'); }mysql_close($con);{else}echo "<script type='text/javascript'>alert('Sorry, there was a problem with the form. Please check all data entries, try again');</script>"; if (isset($_POST['srnumber'])) { include('index_sr.php'); }}; ?> </body></html> Link to comment Share on other sites More sharing options...
2old2learn? Posted June 7, 2011 Author Share Posted June 7, 2011 this is my selection option set up is this right also.. <td>Location:</td><td><select name="location" > <option value="Selection"> Make Selection </option> <option value="250y"> 250Yonge </option> <option value="20q"> 20 Queen </option> <option value="1dundas"> 1 Dundas </option> <option value="galleria_mall"> Galleria/Mall </option></td> Link to comment Share on other sites More sharing options...
2old2learn? Posted June 7, 2011 Author Share Posted June 7, 2011 Well, I've looked it over and over I guess I need fresh eyes..to find the "{" error...<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>insert.php</title><link rel="stylesheet" type="text/css" href="/icc/skins/My_Start_Style.css" /><?phpinclude('config.php');foreach($_POST as $key => $value){ if(empty($_POST[$key])){ $formValid = false; };};?></head><body><?php$formValid = true;if($formValid){ //$con = mysql_connect("localhost","root",""); // if (!$con){ //die('Could not connect: ' . mysql_error()); // }; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Okay found the " {" error... //mysql_select_db("tecicc", $con); //$select = $_POST['location']; $validTable=array('250y','20q','1dundas','galleria_mall'); if(in_array($select,$vaildTable)) //if ($_POST['location'] = "20q") { $sql="INSERT INTO $select ( srnumber, tenant, floor, location, job_description, employee, labour_cost, material_cost, date_twa_sent_to_tenant, date_twa_approved, date_parts_ordered, supplier, date_job_completed, date_billing_given_to_accounting, employee_comments, managers_comments) values ('$_POST[srnumber]','$_POST[tenant]','$_POST[floor]','$_POST[location]','$_POST[job_description]','$_POST[employee]','$_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]','$_POST[employee_comments]','$_POST[managers_comments]')";} if (!mysql_query($sql,$con)){ die('Error: ' . mysql_error()); } echo "<script type='text/javascript'>alert('1 Record added');</script>"; if (isset($_POST['srnumber'])) { include('index_sr.php'); }mysql_close($con);{else <<<<<<< This is where the error is poping up...?????}echo "<script type='text/javascript'>alert('Sorry, there was a problem with the form. Please check all data entries, try again');</script>"; if (isset($_POST['srnumber'])) { include('index_sr.php'); }}; ?> </body></html> Okay found the "{" error..so now I get this error...Parse error: syntax error, unexpected T_ELSE in C:\Program Files\xampp\htdocs\tecicc\insert.php on line 51 Link to comment Share on other sites More sharing options...
thescientist Posted June 7, 2011 Share Posted June 7, 2011 same deal, make sure all you're if/else statements match. It is often easier to see these kinds of thing when you use consistent indenting, tabbing, spacing, etc. if($condition == true){ //im gonna do some stuff //some pretty cool stuff if($stuffToDoIsCool == true){ //now we're getting somewhere! //wooo! }else{ //i guess I'm not that cool //but better than being down there! };}else{ //it's really lonely down here //all the cool stuff is happening up there //:(}; just make sure everything lines up and follow standard conventions when it comes to writing out your code. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.