Solved

php - post table data

Posted on 2015-01-02
4
91 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

726 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