I know how to post multiple rows using a if and foreach loop. I can post multiple rows from one form.


here is the form snippet.....

<tr>
<td><input name="entry[0][place]" type="text" /></td>
<td><input name="entry[0][team]" type="text" /></td>
<td><input name="entry[0][wl]" type="text" /></td>
<td><input name="entry[0][gb]" type="text" /></td>
<td><input name="entry[0][pcage]" type="text" /></td>
<td><input name="entry[0][rs]" type="text" /></td>
<td><input name="entry[0][ra]" type="text" /></td>
<td><input name="entry[0][ags]" type="text" /></td>
</tr>

<tr>
<td><input name="entry[1][place]" type="text" /></td>
<td><input name="entry[1][team]" type="text" /></td>
<td><input name="entry[1][wl]" type="text" /></td>
<td><input name="entry[1][gb]" type="text" /></td>
<td><input name="entry[1][pcage]" type="text" /></td>
<td><input name="entry[1][rs]" type="text" /></td>
<td><input name="entry[1][ra]" type="text" /></td>
<td><input name="entry[1][ags]" type="text" /></td>
</tr>
 
 <tr>
<td><input name="entry[2][place]" type="text" /></td>
<td><input name="entry[2][team]" type="text" /></td>
<td><input name="entry[2][wl]" type="text" /></td>
<td><input name="entry[2][gb]" type="text" /></td>
<td><input name="entry[2][pcage]" type="text" /></td>
<td><input name="entry[2][rs]" type="text" /></td>
<td><input name="entry[2][ra]" type="text" /></td>
<td><input name="entry[2][ags]" type="text" /></td>
</tr>

here is the code that post it as an array of data.....

$mysql = mysql_connect($hostname, $username, $password);
mysql_select_db($database);


//? for php engine
if (isset($_POST['btnSub']))
{
	//check for records later i
    $sql = "INSERT INTO major (place,team, wl, gb, pcage, rs, ra, ags) VALUES\n ";
	

    foreach ($_POST['entry'] as $data)
    {
        $dataArray[] = "('" . join ("','", $data) . "')";
    }

    $sql .= join (",\n", $dataArray);

    /*echo '<pre>', $sql, '</pre>';    */         // view query
    mysql_query($sql) or die(mysql_error());
	header("location:ft7tinput.html"); 
	
}

How can I edit the form I tried this........

<tr>

I did this so you know what I'm trying to target....

<td><input value="<?php echo $data[0] ?>" "name="entry[0][place]" type="text" /></td>

Ok I tried to use the same if and foreach loop to edit the rows...........

<td><input name="entry[0][team]" type="text" /></td>
<td><input name="entry[0][wl]" type="text" /></td>
<td><input name="entry[0][gb]" type="text" /></td>
<td><input name="entry[0][pcage]" type="text" /></td>
<td><input name="entry[0][rs]" type="text" /></td>
<td><input name="entry[0][ra]" type="text" /></td>
<td><input name="entry[0][ags]" type="text" /></td>
</tr>

<tr>
<td><input name="entry[1][place]" type="text" /></td>
<td><input name="entry[1][team]" type="text" /></td>
<td><input name="entry[1][wl]" type="text" /></td>
<td><input name="entry[1][gb]" type="text" /></td>
<td><input name="entry[1][pcage]" type="text" /></td>
<td><input name="entry[1][rs]" type="text" /></td>
<td><input name="entry[1][ra]" type="text" /></td>
<td><input name="entry[1][ags]" type="text" /></td>
</tr>
 
 <tr>
<td><input name="entry[2][place]" type="text" /></td>
<td><input name="entry[2][team]" type="text" /></td>
<td><input name="entry[2][wl]" type="text" /></td>
<td><input name="entry[2][gb]" type="text" /></td>
<td><input name="entry[2][pcage]" type="text" /></td>
<td><input name="entry[2][rs]" type="text" /></td>
<td><input name="entry[2][ra]" type="text" /></td>
<td><input name="entry[2][ags]" type="text" /></td>
</tr>

Here is the edit code......

//? for php engine
if (isset($_GET['btnSub']))
{
	//check for records later i
    $sql = "SELECT FROM major (place,team, wl, gb, pcage, rs, ra, ags) VALUES\n ";
	

    foreach ($_GET['entry'] as $data)
    {
        $dataArray[] = "('" . join ("','", $data) . "')";
    }

    $sql .= join (",\n", $dataArray);

    /*echo '<pre>', $sql, '</pre>';    */         // view query
    mysql_query($sql) or die(mysql_error());
	header("location:ft7tinput.html"); 
	
}

How do I edit rows using the same form?

Your select query is the problem.

SELECT FROM major (place,team, wl, gb, pcage, rs, ra, ags)

SELECT queries have the following simplified syntax:

SELECT
    select_expr, ...
    [FROM table_references
    [WHERE where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]

The full syntax is at: http://dev.mysql.com/doc/refman/5.0/en/select.html

So you'd want something like:

SELECT (place,team, wl, gb, pcage, rs, ra, ags) FROM major limit 0, 5

This would select the columns (place,team, wl, gb, pcage, rs, ra, ags) from rows 0 through to 5
(ie: 0 - 5 by the select order which by default is the ascending primary key index).

From there, you just need to print out your form with a foreach loop:

<tr>
<td><input name="entry[1][place]" type="text" /></td>
<td><input name="entry[1][team]" type="text" /></td>
<td><input name="entry[1][wl]" type="text" /></td>
<td><input name="entry[1][gb]" type="text" /></td>
<td><input name="entry[1][pcage]" type="text" /></td>
<td><input name="entry[1][rs]" type="text" /></td>
<td><input name="entry[1][ra]" type="text" /></td>
<td><input name="entry[1][ags]" type="text" /></td>
</tr>

would be something like:

<?php foreach($row as $i=>$row) : // lets say row is each table row ?>
<tr>
<td><input name="entry[<?php echo $i; ?>][place]" type="text" /></td>
<td><input name="entry[<?php echo $i; ?>][team]" type="text" /></td>
<td><input name="entry[<?php echo $i; ?>][wl]" type="text" /></td>
<td><input name="entry[<?php echo $i; ?>][gb]" type="text" /></td>
<td><input name="entry[<?php echo $i; ?>][pcage]" type="text" /></td>
<td><input name="entry[<?php echo $i; ?>][rs]" type="text" /></td>
<td><input name="entry[<?php echo $i; ?>][ra]" type="text" /></td>
<td><input name="entry[<?php echo $i; ?>][ags]" type="text" /></td>
</tr>

<?php endforeach; ?>

Some security notes:

I'd advise preventing SQL injections in the following code you have:

$mysql = mysql_connect($hostname, $username, $password);
mysql_select_db($database);


//? for php engine
if (isset($_POST['btnSub']))
{
	//check for records later i
    $sql = "INSERT INTO major (place,team, wl, gb, pcage, rs, ra, ags) VALUES\n ";
	

    foreach ($_POST['entry'] as $data)
    {
        $dataArray[] = "('" . join ("','", $data) . "')";
    }

    $sql .= join (",\n", $dataArray);

    /*echo '<pre>', $sql, '</pre>';    */         // view query
    mysql_query($sql) or die(mysql_error());
	header("location:ft7tinput.html"); 
	
}

You'll need to put a foreach in front of the first join that first makes sure the SQL is escaped properly.

foreach ($_POST['entry'] as $data)
    {
        foreach($data as $i=>$x) // prevents mysql injections
                $data[i] = mysql_real_escape_string($x);
        $dataArray[] = "('" . join ("','", $data) . "')";
    }

or something similar. Otherwise you are giving anyone using the form the ability to run arbitrary mysql queries against your database if your PHP does not automatically escape strings with the magic_quotes directive. Even if you use magic_quotes, its better to stripslashes() the string and then use mysql_real_escape_string or mysql_escape_string depending on php version. mysql_real_escape_string implements the SQL standard of escaping SQL strings while magic_quotes in PHP only uses \ to escape everything.

This article has been dead for over six months. Start a new discussion instead.