Jump to content


Populate Form to many Tables in a Database

  • Please log in to reply
3 replies to this topic

#1 aeafisme23

  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationUSA

Posted 03 August 2006 - 08:02 PM

To better understand my issue, I will include all code and small screenshots of precisely what needs to be done. Hence, I am only stuck one part of the code, which is populating add.php's form to populate my database. Only problem is the mysql insert part that screws me up because usually when you insert, it asks you to insert into what table and not individually assign text boxes to populate certain tables in created database.

First here is the database code:

Here is a relational view of my database:

Now that you understand the database structure we can move on.

--- Dropdown multi-chain select, pulls from database IF manually entered in to mySQL
(dd3.php) below


// username and password to log onto db server
// name of database

////// DONOT EDIT BELOW  /////////
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());

<title>Year/Make/Model Multiple DD - Populated from  Form</title>
<SCRIPT language=JavaScript>
function reload(form)
var val=form.make.options[form.make.options.selectedIndex].value; 
self.location='dd3.php?make=' + val ;
function reload3(form)
var val=form.make.options[form.make.options.selectedIndex].value; 
var val2=form.model.options[form.model.options.selectedIndex].value; 

self.location='dd3.php?make=' + val + '&submodel=' + val2 ;


///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT make,make_id FROM make order by make"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
$cat=$HTTP_GET_VARS['make']; // This line is added to take care if your global variable is off
if(isset($make) and strlen($make) > 0){
$quer=mysql_query("SELECT DISTINCT model,model_id FROM model where make_id=$make order by model"); 
}else{$quer=mysql_query("SELECT DISTINCT model,model_id FROM model order by model"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

/////// for Third drop down list we will check if sub category is selected else we will display all the subcategory3///// 
$cat3=$HTTP_GET_VARS['submodel']; // This line is added to take care if your global variable is off
if(isset($submodel) and strlen($submodel) > 0){
$quer3=mysql_query("SELECT DISTINCT submodel FROM submodel where model_id=$submodel order by submodel"); 
}else{$quer3=mysql_query("SELECT DISTINCT submodel FROM submodel order by submodel"); } 
////////// end of query for third subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='retrieve.php'>";
//////////        Starting of first drop downlist /////////
echo "<select name='make' onchange=\"reload(this.form)\"><option value=''>Select Make</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['make_id']==@$make){echo "<option selected value='$noticia2[make_id]'>$noticia2[make]</option>"."<br>";}
else{echo  "<option value='$noticia2[make_id]'>$noticia2[make]</option>";}
echo "</select><br><br>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='model' onchange=\"reload3(this.form)\"><option value=''>Select Model</option>";
while($noticia = mysql_fetch_array($quer)) { 
if($noticia['model_id']==@$submodel){echo "<option selected value='$noticia[model_id]'>$noticia[model]</option>"."<br>";}
else{echo  "<option value='$noticia[model_id]'>$noticia[model]</option>";}
echo "</select><br><br>";
//////////////////  This will end the second drop down list ///////////

//////////        Starting of third drop downlist /////////
echo "<select name='submodel' ><option value=''>Select Submodel</option>";
while($noticia = mysql_fetch_array($quer3)) { 
echo  "<option value='$noticia[submodel]'>$noticia[submodel]</option>";
echo "</select><br><br>";
//////////////////  This will end the third drop down list ///////////

echo "<input type=submit value=Submit>";
echo "</form>";



This works beautifully, no problems at all, i can populate via mySQL and retrieve all information to retrieve.php. But who in their right mind would enter EVERY single car make, model, and submodel (along with products not in this thread)? No one, so I want to create a form so he can update the make, model, and submodel when he has a product for that exact match.

--- Form that needs to populate Database ---  ****this part is the only bad part***
(add.php) below
In this code i took the OO approach with no prevail, by no means is this the only way I want to do it, I just need to figure out how to populate the dd3.php from add.php.

if (isset($_POST['submit'])) {
echo "submitting <br>";

// new Car MAKE:;  replaced title with submodel here
$newmake = new make;
$newmake->submodel = ($_POST['submodel']);

// new Car MODEL replaced fname and firstname with model might have to put in a newmodel to put in make as link
$newmodel = new model;
$newmodel ->model = ($_POST['model']);
// $newmodel ->make = ($_POST['make']);

// new Car SUBMODEL
$newsubmodel = new submodel;
$newsubmodel ->make = $newmake->id;
$newsubmodel ->model = $newmodel->id;

echo "done, check out the database";
} else {
<form method="POST" action="add.php">

<table width="720" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">  <tr>
    <td>Car Make:</td>
    <td><input name="make" /></td>
    <td>Car Model:</td>
    <td><input name="model" /></td>
    <td>Car Submodel:</td>
    <td><input name="submodel" /></td>
    <td><input type="submit" name="submit" value="submit" /></td>
    <td><input type="reset" /></td>

This is the next part that makes it tick:
// conn.php

// A database persitable object, pretty useless unless you extend it.   
class Persistable {

      // Unique database identifier.
      var $id; 
      // Table name that is used in the database
      var $table; 
      // Map of column labels in the database to object variables
      var $columns = array(); 

      // Persist a new object in the database. 
      function PersistNew() { 
        mysql_connect('localhost', 'username', 'password')
                  or die("Could not connect: " . mysql_error());
              or die ("Unable to select database");
        $sql = "INSERT INTO `".$this->table."` ";
        $labels = "(`id` ";
        $values = ") VALUES ('' ";

        foreach ($this->columns as $column) {
          $labels .= ", `".$column[0]."` ";
          $temp = '$this->'.$column[1];
          $values .= ", '"."$temp"."' ";    

        $sql .= $labels.$values.")";
        eval ("\$sql = \"$sql\";");
        $result = mysql_query($sql);
        $this->id = mysql_insert_id();
}// End Class Persistable

class make extends Persistable {

      var $make;//String of submodel title.

      function make() { //Constructor for make.
        $this->table = 'make';
        $this->columns = array( array('make','make')
} // End Class make 

class model extends Persistable {
      var $model; // String of Cars model.  
      // var $model;  String of Cars Model.
      function model() { //Constructor for model.
        $this->table = 'model';
        $this->columns = array( 
		//,        array('lastname','lname')
} // End Class model

class submodel extends Persistable {
      var $submodel; // integer model unique id.  
      // NOTHING should be here? var $author; //  integer author unique id.
      function submodel() { //Constructor for submodel.
        $this->table = 'submodel';
        $this->columns = array( array('submodel','submodel')
} // End Class submodel

I am SO lost when it comes to getting this to work, although i see no error message (peachy) I am sure i have one of my classes messed up, but I would perfer to do this a non OO way and just hardcode in php.

So my final thoughts are, is there a way to :
  $sql = "INSERT INTO make(make_id, make) VALUES ('$make_id','$make')";  and
  $sql = "INSERT INTO model (model_id, model) VALUES ('$model_id','$model')"; and
  $sql = "INSERT INTO submodel(model_id, submodel) VALUES ('$model_id','$submodell')";
in the same page without having to use Object Oriented programming using something called Persisting data?

I am not asking for code on how to do this, however it would be very much appreciated, i am however looking for LOGIC, i am getting caught up on one small thing and that is just populating a form to many tables.  Thank you all.

Extra info: To see a demo of what i want here are some images of the process it would look like.

1)submit user/pass > http://www.kindervis...wayne/login.jpg 
2)go to add form > http://www.kindervis.../userselect.jpg
3)submit the make/model/submodelhttp://www.kindervis...ydwayne/add.jpg
4)see results in the dropdown at http://www.kindervis...ydwayne/dd3.jpg

#2 aeafisme23

  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationUSA

Posted 04 August 2006 - 02:39 PM

I suppose too much code below deters people so heres my short question. Is there a way to populate a database from one php page using a form with many textboxes to "many tables" in the "same database" such as
$sql = "INSERT INTO make(make_id, make) VALUES ('$make_id','$make')";  and
  $sql = "INSERT INTO model (model_id, model) VALUES ('$model_id','$model')"; and
  $sql = "INSERT INTO submodel(model_id, submodel) VALUES ('$model_id','$submodell')";

all on the same page? Is there a logic behind this? Thanks all!!!

#3 HeyRay2

  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 04 August 2006 - 02:53 PM

It sounds like you already have the queries written and ready to go. As long as the variables you mention in them exist, you can run a unlimited amount of queries. So, why not just do this:

// Write the queries
$sql = "INSERT INTO make(make_id, make) VALUES ('$make_id','$make')";
$sql2 = "INSERT INTO model (model_id, model) VALUES ('$model_id','$model')";
$sql3 = "INSERT INTO submodel(model_id, submodel) VALUES ('$model_id','$submodell')";

// Execute the queries
mysql_query($sql) or die(mysql_error());
mysql_query($sql2) or die(mysql_error());
mysql_query($sql3) or die(mysql_error());

#4 aeafisme23

  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationUSA

Posted 04 August 2006 - 03:00 PM

How do I assign the certain textboxes such as  Make to be assigned to $SQL and Model to be assigned to query $SQL2 etc...? This has to be an elementary procedure but for the life of me I can not find in my reference book PHP and MySQL -Web development. This would be very helpful if someone responds again, thanks again for the reply, it was very appreciated and a great answer to my question!!!!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users