php - post table data

I have a two tables set up with the following sturctures:
			<tr>
				<td>01</td>
				<td>Replace Engine Oil & Filter</td>
				<td><input type='text' name='MaintPrice01' /></td>
				<td><input type='text' name='MaintDuration01' /></td>
				<td><input type='text' name='MaintOPCode01' /></td>
			</tr>
			<tr>
				<td>02</td>
				<td>4 Wheel Alignment</td>
				<td><input type='text' name='MaintPrice02' /></td>
				<td><input type='text' name='MaintDuration02' /></td>
				<td><input type='text' name='MaintOPCode02' /></td>
			</tr>
			<tr>
				<td>03</td>
				<td>Wheel Balance (4 Wheel)</td>
				<td><input type='text' name='MaintPrice03' /></td>
				<td><input type='text' name='MaintDuration03' /></td>
				<td><input type='text' name='MaintOPCode03' /></td>
			</tr>

Open in new window


and the second table is similar:
			<tr>
				<td>01</td>
				<td>ABS Light Diagnosis</td>
				<td><input type='text' name='RepairPrice01' /></td>
				<td><input type='text' name='RepairDuration01' /></td>
				<td><input type='text' name='RepairOPCode01' /></td>
			</tr>		
			<tr>
				<td>02</td>
				<td>Body Appearance and/or Paint Concern</td>
				<td><input type='text' name='RepairPrice02' /></td>
				<td><input type='text' name='RepairDuration02' /></td>
				<td><input type='text' name='RepairOPCode02' /></td>
			</tr>
			<tr>
				<td>03</td>
				<td>Brake System Diagnosis</td>
				<td><input type='text' name='RepairPrice03' /></td>
				<td><input type='text' name='RepairDuration03' /></td>
				<td><input type='text' name='RepairOPCode03' /></td>
			</tr>	

Open in new window


I want each of these rows to go to a single record like this:
Insert into priceEntryDetail (QuestionID, Price, Duration, OPCode) values ('Repair03',2.99,3,'C1234')

I believe if I use arrays in the input item names it would be easier to add these records to mySQL via php, correct?
I found something online like this:
<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />

<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />

<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />

Open in new window

of course mine would be item[0][Price], etc...

Can someone please post what the php loop would look like?

Thanks in advance.
LVL 7
UniqueDataAsked:
Who is Participating?
 
Ray PaseurCommented:
This is my attempt to show the "Active Record" design pattern.  No attempt has been given to normalizing the tables or anything like that, and no attempt has been made to show how to sanitize external data, etc.  All this tries to show is a way of getting the data from a simulated row of a database table ($question) and putting that data into a variable ($form) that gets put on the screen so the client can update the information.  A table maintenance article that shows more details of this work is available here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12335-PHP-and-MySQLi-Table-Maintenance.html

Please see: http://iconoun.com/demo/temp_uniquedata.php

You can experiment with changing the variables and you'll see that the script "remembers" your changes as you submit the form over and over.  When you want to UPDATE the table instead of INSERT, you'll follow similar logic, but you'll use a WHERE clause that references the 'id' field.

<?php // demo/temp_uniquedata.php
error_reporting(E_ALL);

/**
 * SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28589953.html
 *
 * Examples from the question at E-E
 *
 * Insert into priceEntryDetail (QuestionID, Price, Duration, OPCode) values ('Repair03',2.99,3,'C1234')
 *
 * <tr>
 *     <td>01</td>
 *     <td>Replace Engine Oil & Filter</td>
 *     <td><input type='text' name='MaintPrice01' /></td>
 *     <td><input type='text' name='MaintDuration01' /></td>
 *     <td><input type='text' name='MaintOPCode01' /></td>
 * </tr>
 * <tr>
 *     <td>02</td>
 *     <td>4 Wheel Alignment</td>
 *     <td><input type='text' name='MaintPrice02' /></td>
 *     <td><input type='text' name='MaintDuration02' /></td>
 *     <td><input type='text' name='MaintOPCode02' /></td>
 * </tr>
 * <tr>
 *     <td>03</td>
 *     <td>Wheel Balance (4 Wheel)</td>
 *     <td><input type='text' name='MaintPrice03' /></td>
 *     <td><input type='text' name='MaintDuration03' /></td>
 *     <td><input type='text' name='MaintOPCode03' /></td>
 * </tr>
 */


// THIS SIMULATES THE DATA STRUCTURE OF ROWS IN THE 'priceEntryDetail' TABLE
$question = new StdClass;
$question->id       = '43';
$question->title    = 'Replace Engine Oil & Filter';
$question->price    = '49.99';
$question->duration = '3.0';
$question->opcode   = 'C1234';
$rows[$question->id] = $question;

$question = new StdClass;
$question->id       = '17';
$question->title    = '4 Wheel Alignment';
$question->price    = '74.99';
$question->duration = '3.0';
$question->opcode   = 'C5678';
$rows[$question->id] = $question;


// THIS IS THE ACTION SCRIPT THAT IS RUN AFTER THE FORM IS SUBMITTED
if (!empty($_POST))
{
    // USE OUR DATA SET AS THE POINTER TO THE POST DATA
    foreach ($rows as $id => $question)
    {
        // ITERATE OVER THE OBJECT TO ACQUIRE THE POST DATA
        foreach ($question as $property => $value)
        {
            $question->$property = $_POST[$property][$id];
        }

        // COLLAPSE THE OBJECT PROPERTIES INTO A QUERY STRING
        $q_array  = (array)$question;
        $q_values = "'" . implode("', '", $q_array) . "'";
        $sql = "INSERT INTO priceEntryDetail ( id, title, price, duration, opcode ) VALUES ( $q_values )";

        /**
         * UPDATE THE DATABASE HERE
         */
        echo PHP_EOL . "<br>QUERY: <b> $sql </b>";
        echo PHP_EOL;
    }
}


// SHOW HOW TO SET UP AN HTML FORM WITH THIS DATA
$table_rows = NULL;

// CREATE THE ROWS
foreach ($rows as $question)
{
    $id = $question->id;
    $table_rows .= <<<EOD
  <tr>
    <input type="hidden"   name="id[$id]"       value="$question->id"       />
    <td><input type="text" name="title[$id]"    value="$question->title" readonly /></td>
    <td><input type="text" name="price[$id]"    value="$question->price"    /></td>
    <td><input type="text" name="duration[$id]" value="$question->duration" /></td>
    <td><input type="text" name="opcode[$id]"   value="$question->opcode"   /></td>
  </tr>

EOD;
}

// CREATE THE FORM USING THE ROWS
$form = <<<ENDFORM
<form method="post">
<table>
  <tr>
    <th>Title</th>
    <th>Price</th>
    <th>Duration</th>
    <th>OPCode</th>
  </tr>
$table_rows
</table>
<input type="submit" />
</form>
ENDFORM;

// SHOW THE CURRENT STATUS OF THE DATA SET
echo $form;

Open in new window

0
 
aboo_sCommented:
in your php these variables will be in the request ($_REQUEST)
so basically you do this:


for($i=0;i<9;$i++)
{$query="Insert into priceEntryDetail (QuestionID, Price, Duration, OPCode) values ('".$_REQUEST['Repair0'.$i]."',$_REQUEST['RepairDuration0'.$i],3,'C1234')" ;
$db->execute($query);
}

Something like this ,forgive me I haven't got the chance to actually try it!
0
 
Ray PaseurCommented:
I think the most reasonable starting point would be to build the database structure first, and after that is done, then consider what the HTML would look like.  I'll see if I can give you an example in a little while.  For now, you might want to make a Google search for the exact phrase, "Should I Normalize My Database" and read the very interesting comments on all sides of this question.
0
 
UniqueDataAuthor Commented:
actually I found code to use the naming convention I mentioned in my original post.

Using the naming convention:
<input type="text" name="item[0][QuestionID]" />
<input type="text" name="item[0][price]" />
<input type="text" name="item[0][duration]" />
<input type="text" name="item[0][OPCode]" />

<input type="text" name="item[1][QuestionID]" />
<input type="text" name="item[1][price]" />
<input type="text" name="item[1][duration]" />
<input type="text" name="item[1][OPCode]" />

Open in new window


I was able to use:
      $b = array_map("postRecord",  $_POST['item']);

which called:
function postRecord($rec) {
     $sql = "Insert into priceEntryDetail (QuestionID, Price, Duration, OPCode) values ( $rec[QuestionID], $rec[price],$rec[duration],$rec[OPCode])
     if (!$res = mysql_query($sql))  die( $sql ) ;
}

Open in new window


It seems cleaner than Ray's suggestion, but I will go back and re-read Ray's post as I am sure it has nuggets of valuable knowledge since I am fairly new to PHP.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.