Solved

php - post table data

Posted on 2015-01-02
4
81 Views
Last Modified: 2015-01-23
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.
0
Comment
Question by:UniqueData
  • 2
4 Comments
 
LVL 10

Expert Comment

by:aboo_s
ID: 40528796
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40528997
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40529192
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
 
LVL 7

Author Comment

by:UniqueData
ID: 40529361
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now