Jump to content

Defining selection boxes to echo on new page


Scooby-Doo

Recommended Posts

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

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.