Scooby-Doo Posted May 23, 2012 Share Posted May 23, 2012 Hi I have a quote system on the sidebar of my page, it is a number of selection boxes two of which populate from a database thrugh php java etc etc When the user presses the Quote button, I want the results to firstly echo on the new page. I understand the echo on the new page what I cant get my head round is defining the selection boxes so they do echo onto the new page Here is the code of one of my selection boxes <select name="inspection" class="selectionbox" id="inspection"> <option value="1" selected="selected">Inspection Type</option> <option value="2">Basic</option> <option value="3">Platinum</option> <option value="3">Pre/Post Accident</option> </select> Any help greatly appreciated...sorry the site is not online as I am still testing getting right..so cant share a web address Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/ Share on other sites More sharing options...
scootstah Posted May 23, 2012 Share Posted May 23, 2012 I don't think I understand what you want. Is this it? echo $_POST['inspection']; Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1347945 Share on other sites More sharing options...
Scooby-Doo Posted May 23, 2012 Author Share Posted May 23, 2012 Thank you, thats half of it done..the next problem is the Make and Model selection boxes....the Make and Model data is being pulled from sql database so if I put echo $_POST['make']; echo $_POST['model']; Its just pulling the DB id numbers instead of what is written on the user selection...obviously I need it to be text But thanks again I cant believe it was that simple for the others, woods and trees come to mind Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1347955 Share on other sites More sharing options...
Scooby-Doo Posted May 23, 2012 Author Share Posted May 23, 2012 Please can someone help with this one as its the last bit of the site I need to do... All i am stuck on now is the Make and Model "id's" from the database and not printing the text from the users selection..just a number...how do I get it to print/echpo the text selection Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1347983 Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 1. It would help if your input options all have DIFFERENT values. 2. Your database should have a table that maps those ID to the text value that you want. Of course, this only works if you fix #1. Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1347985 Share on other sites More sharing options...
Scooby-Doo Posted May 23, 2012 Author Share Posted May 23, 2012 Hi Kays thanks Yes all the input options have different values, just dont know how to go about changing the values so the php pull from the list of "Manufactuers" & "Models" and not "Manufacturer_id" & "Model_id" This is the code from the Make/Manufacturers selection box if it helps <select name="make" class="selectionbox" id="Manufacturer"> <option value="make" selected="selected">Select Make</option> <?php createoptions("Manufacturer", "Manufacturer_id", "Manufacturer"); ?> </select> If you need anymore code from anywhere let me know...and thanks in advance much much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1347994 Share on other sites More sharing options...
Kays Posted May 23, 2012 Share Posted May 23, 2012 So you have finished step 1 and 2? Well, then you would use a JOIN with the table you created in step 2 to find its value. Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1347996 Share on other sites More sharing options...
Jamdog Posted May 23, 2012 Share Posted May 23, 2012 It seems what you are looking for is a simple function or two to grab the details from the database. These can sit at the top of your PHP script: function get_model_by_id($id) { $query = "SELECT model FROM mytable WHERE id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); $c = mysql_num_rows($res); if ($c != 1) { die ("ERROR: Model not found in get_model_by_id (".__FILE__." at line ".__LINE__.")"); } $row = mysql_fetch_assoc($res); $ret = $row['model']; return $ret; } function get_model_by_id($id) { $query = "SELECT make FROM mytable WHERE id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); $c = mysql_num_rows($res); if ($c != 1) { die ("ERROR: Make not found in get_model_by_id (".__FILE__." at line ".__LINE__.")"); } $row = mysql_fetch_assoc($res); $ret = $row['make']; return $ret; } NOTE: You may need to edit the $query lines to suit your own database Now you can just call these functions when you need to: $make_id = $_POST['make']; $model_id = $_POST['model']; $make_name = get_make_by_id($make_id); $model_name = get_model_by_id($model_id); Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1347997 Share on other sites More sharing options...
Scooby-Doo Posted May 23, 2012 Author Share Posted May 23, 2012 Thanks for that...but it doesnt work...can I just point out I have used the name "Make" on my form...in the Database it is down as "Manufacturer"...does that make a difference This is the error I am getting Fatal error: Cannot redeclare createoptions() (previously declared in C:\xampp\htdocs\website\quote.php:37) in C:\xampp\htdocs\website\header.php on line 11 This is the code on line 11 of the Header function createoptions($table , $id , $field) { $sql = "select * from $table ORDER BY $field"; $res = mysql_query($sql) or die(mysql_error()); while ($a = mysql_fetch_assoc($res)) echo "<option value=\"{$a[$id]}\">$a[$field]</option>"; } Thanks again for all your help on this one Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348032 Share on other sites More sharing options...
Scooby-Doo Posted May 23, 2012 Author Share Posted May 23, 2012 Sorry ignore my last post still getting a fault but I put the code in two places by mistake thats why I was getting the error These are the errors I am getting now Notice: Undefined variable: make_id in C:\xampp\htdocs\website\quote.php on line 55 Notice: Undefined variable: table in C:\xampp\htdocs\website\quote.php on line 18 ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id=''' at line 1 (in C:\xampp\htdocs\website\quote.php at line 19) Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348036 Share on other sites More sharing options...
Scooby-Doo Posted May 23, 2012 Author Share Posted May 23, 2012 Right the story so far and I am nearly pulling my hair out here. I have got two tables in the database table 1 name = "manufacturer", column names "Manufacturer_id", "Manufacturer" table 2 name = "model", column names "Model_id", "Manufacturer_id", "Model" The code that Jamdog has posted has now been changed to this function get_model_by_id($id) { $query = "SELECT Model FROM model WHERE id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); $c = mysql_num_rows($res); if ($c != 1) { die ("ERROR: Model not found in get_model_by_id (".__FILE__." at line ".__LINE__.")"); } $row = mysql_fetch_assoc($res); $ret = $row['model']; return $ret; } function get_make_by_id($id) { $query = "SELECT Manufacturer FROM manufacturer WHERE id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); $c = mysql_num_rows($res); if ($c != 1) { die ("ERROR: Make not found in get_make_by_id (".__FILE__." at line ".__LINE__.")"); } $row = mysql_fetch_assoc($res); $ret = $row['make']; return $ret; } When I run the page no faults occur...as soon as I put this code in $make_id = $_POST['make']; $model_id = $_POST['model']; $make_name = get_make_by_id($make_id); $model_name = get_model_by_id($model_id); No matter where I put the code it keeps giving errors Where am I going wrong??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348099 Share on other sites More sharing options...
Jamdog Posted May 23, 2012 Share Posted May 23, 2012 Error messages are usually quite informative if you know how to read them. Unfortunately, it's quite hard to figure out what's going wrong when you say 'I get errors' but don't say what those errors are. Just to clarify though, the two functions should be in your PHP script before they are called (usually I put all functions at the top of the PHP script). What are the error messages you are getting? Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348156 Share on other sites More sharing options...
Scooby-Doo Posted May 23, 2012 Author Share Posted May 23, 2012 Thnaks Jamdog The error message is ERROR: Unknown column 'id' in 'where clause' (in C:\xampp\htdocs\website\quote.php at line 20) function get_make_by_id($id) { $query = "SELECT Manufacturer FROM manufacturer WHERE id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); Like mentioned my column name is Manufacturer from the table called manufacturer and the only other column in their is Manufacturer_id Dont know if it makes any difference but Manufacturer and Models are in two different tables The columns in model are Model, Model_id, Manufacturer_id Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348168 Share on other sites More sharing options...
Scooby-Doo Posted May 24, 2012 Author Share Posted May 24, 2012 Please please can someone help me on this one Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348268 Share on other sites More sharing options...
Jamdog Posted May 24, 2012 Share Posted May 24, 2012 SQL is designed to be almost like reading plain English (although in many cases, it soon gets complicated). So, a simple SELECT query is: SELECT (fields) FROM (table) WHERE (field)=(value); So, looking at the query you currently have: $query = "SELECT Manufacturer FROM manufacturer WHERE id='".$id."'"; and your error message: ERROR: Unknown column 'id' in 'where clause' (in C:\xampp\htdocs\website\quote.php at line 20) We can see that 'id' is not the right field name to use in your query. You said that the ID in your table has the fieldname Model_id (and Manufacturer_id for the other table), so you should be able to just change your query from this: $query = "SELECT Manufacturer FROM manufacturer WHERE id='".$id."'"; to this: $query = "SELECT Manufacturer FROM manufacturer WHERE Model_id='".$id."'"; Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348369 Share on other sites More sharing options...
Scooby-Doo Posted May 24, 2012 Author Share Posted May 24, 2012 Thank you...now if I leave this code out it works with no errors but still displays id numbers <?php $make_id = $_POST['make']; ?> <?php $model_id = $_POST['model'];?> <?php $make_name = get_make_by_id($make_id);?> <?php $model_name = get_model_by_id($model_id);?> As soon as I put this code in it throws up errors ERROR: Make not found in get_make_by_id Is there any particular place these should go... I dont know if the auto populating php is causing any problems...which is this <?php function createoptions($table , $id , $field) { $sql = "select * from $table ORDER BY $field"; $res = mysql_query($sql) or die(mysql_error()); while ($a = mysql_fetch_assoc($res)) echo "<option value=\"{$a[$id]}\">$a[$field]</option>"; } ?> Really appreciate your help on this one Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348378 Share on other sites More sharing options...
Scooby-Doo Posted May 25, 2012 Author Share Posted May 25, 2012 Yeahhh..finally got it to work, this is the code for the quote page <?php include("conndb.php");?> <?php function get_model_by_id($id) { $query = "SELECT manufacturer FROM Manufacturer WHERE Manufacturer_id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); $c = mysql_num_rows($res); if ($c != 1) { die ("ERROR: Model not found in get_model_by_id (".__FILE__." at line ".__LINE__.")"); } $row = mysql_fetch_assoc($res); $ret = $row['manufacturer']; return $ret; } function get_make_by_id($id) { $query = "SELECT model FROM Model WHERE Model_id='".$id."'"; $res = mysql_query($query) or die("ERROR: ".mysql_error()." (in ".__FILE__." at line ".__LINE__.")"); $c = mysql_num_rows($res); if ($c != 1) { die ("ERROR: Make not found in get_make_by_id (".__FILE__." at line ".__LINE__.")"); } $row = mysql_fetch_assoc($res); $ret = $row['model']; return $ret; } ?> <?php $title="Your Personal Online Quote"; $description="."; $keywords="."; include('header.php'); ?> <?php $Manufacturer_id = $_POST['model'];?> <?php $Model_id = $_POST['make'];?> <?php echo $Manufacturer = get_make_by_id($Manufacturer_id);?><br /> <?php echo $Model = get_model_by_id($Model_id);?><br /> <?php echo $_POST['inspection'];?><br /> <?php echo $_POST['delivery'];?><br /> <?php include("footer.php"); ?> Dont know if its right or wrong...but it works. And thanks Jamdog for your help on this one, I would still be scratching my head if left alone. Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348586 Share on other sites More sharing options...
Kays Posted May 25, 2012 Share Posted May 25, 2012 It looks fine to me. You don't have to open and close all those PHP tags. Open one tag and put all your PHP code in between. If you are the ONLY person that would use that code, it's fine. However, if you have other users use it then: I will point out that you should RARELY trust your own input data to be secure. So please don't take on blind faith that the users won't try to break your system. So please secure your $_POST data before passing them on. Still, even if you are the only one using it, it's not a bad idea to secure them. Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348588 Share on other sites More sharing options...
Scooby-Doo Posted May 25, 2012 Author Share Posted May 25, 2012 Ok Thanks Kays will remove some of the php tags...thanks for the security warning, I will Yahoogle now to find out about it. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/262991-defining-selection-boxes-to-echo-on-new-page/#findComment-1348589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.