Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple form entry 1

Status
Not open for further replies.

overyde

Programmer
Joined
May 27, 2003
Messages
226
Location
ZA
Hi,
I'm calling data from a mysql db and pumping the data back into a form (i.e. text fields etc) by use of an array so that multiple entries can be updted in one go. My problem is that when I press the update button, only the bottom/last entry in the form gets updated(understandably). How do I get a loop working so that the data from the form gets pumped into the dbase for the right fields. My form looks like so:

******************form**********************************
<?php
session_start();
?>
<link rel="STYLESHEET" type="text/css" href="style.css">
<html>
<body bgcolor="#D9E1F4">
<?php

// open the connection
$conn = mysql_connect("localhost", "", "");

// pick the database to use
mysql_select_db("imperial",$conn);

// create the sql statement
$sql = "SELECT id, sub, vname, vtel from place";

// execute the sql statement
$result = mysql_query($sql, $conn) or die(mysql_error());




// print the table and form with data pumped back into it
print "<form method=\"post\" action=\"changeallprocess.php\"><table cellpadding=5 border=1 width=100% bordercolor=white bgcolor=\"#D9E1F4\">";

// go through each row in the result set and display data
while ($newArray = mysql_fetch_array($result)) {
// give a name to the fields
$id = $newArray['id'];
$sub = $newArray['sub'];
$vname = $newArray['vname'];
$vtel = $newArray['vtel'];

echo "
<tr>
<td><font size=2 face=\"verdana\">$vname:</td>
<td><input type=\"hidden\" name=\"id\" value=\"$id\">$id</td>
<td><input type=\"text\" name=\"sub" value=\"$sub\"></td>
<td><input type=\"text\" name=\"vtel\" value=\"$vtel\"></td>
</tr>
";

}
print "<td colspan=4 align=\"center\" valign=\"top\"><input type=\"Submit\" name=\"submit\" value=\"Modify Venues\"></td></tr>
</form></table>";
?>
</body>
</html>

Reality is built on a foundation of dreams.
 
Code:
// go through each row in the result set and display data
$i=0;
while ($newArray = mysql_fetch_array($result)) {
    // give a name to the fields
    $id = $newArray['id'];
    $sub = $newArray['sub'];
    $vname = $newArray['vname'];
    $vtel = $newArray['vtel'];
        
    echo "
<tr>
<td><font size=2 face=\"verdana\">$vname:</td>
<td><input type=\"hidden\" name=\"id[$i]\" value=\"$id\">$id</td>
<td><input type=\"text\" name=\"sub[$i]" value=\"$sub\"></td>
<td><input type=\"text\" name=\"vtel[$i]\" value=\"$vtel\"></td>
</tr>
";
 $i++;   
}
[code]

this way you will assign each item corerectly with an index to the array, instead of overwriting the arrary everytime you write the next line.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
this is the part that i get stuck...the actual processing. Would it go some thing similar to:
Code:
<?php

// THIS PAGE IS USED TO PROCESS THE DATA CAPTURED FROM CHANGEALL CONCERNING INDEPENDENT RESTAURANTS & FRANCHISE HQ

session_start();

// open the connection
$conn = mysql_connect("localhost","","");

// pick the database to use
mysql_select_db("imperial", $conn);

// create the SQL statement
$sql = "UPDATE place SET vtel = '$_POST[vtel]', sub = '$_POST[sub]' where id = '$_POST[id]'";


// execute the SQL statment
	if (mysql_query($sql, $conn)) {
		echo "Record Added!<br>
		Would you like to edit/add another<br>
		<a href=\"changeall.php\">Yes</a> ";
		}else{
		echo "something went wrong";
	}
?>
[CODE]

Reality is built on a foundation of dreams.
 
this is the part that i get stuck...the actual processing. Would it go some thing similar to:
Code:
<?php

// THIS PAGE IS USED TO PROCESS THE DATA CAPTURED FROM CHANGEALL 

session_start();

// open the connection
$conn = mysql_connect("localhost","","");

// pick the database to use
mysql_select_db("imperial", $conn);

// create the SQL statement
$sql = "UPDATE place SET vtel = '$_POST[vtel]', sub = '$_POST[sub]' where id = '$_POST[id]'";


// execute the SQL statment
    if (mysql_query($sql, $conn)) {
        echo "Record Added!<br>
        Would you like to edit/add another<br>
        <a href=\"changeall.php\">Yes</a> ";
        }else{
        echo "something went wrong";
    }
?>

Reality is built on a foundation of dreams.
 
I would create the fieldnames dynamically so they contain the actual record ID. During processing just split the fieldnames at the delimiting character:
Code:
# creating unique POST vars
echo "
<tr>
<td><font size=2 face=\"verdana\">$vname:</td>
<td><input type=\"hidden\" name=\"id_$id\" value=\"$id\">$id</td>
<td><input type=\"text\" name=\"sub_$id" value=\"$sub\"></td>
<td><input type=\"text\" name=\"vtel_$id\" value=\"$vtel\"></td>
</tr>
";

You could then loop through the posted vars and stick them into an associative array keted by record id:
Code:
foreach($_POST as $key=>$value){
   # only entries that have the underscore
   if(strstr($key,'_'){
      # split on underscore
      $item = explode('_',$key);
      $tmpArray[$item[1]][$item[0]] = $value;
   }
}
You'd end up with a mutidimensional array like this:
Array id "n"
['sub'] => 'whatever'
['vtel'] => 'whatever'

Then just loop through that array and use the SQL you had.
 
I think I'm being really stupid here!

DRJ478:
All I get is the value of the submit button????
This is SOOO frustrating!!!

KarveR:
I'm not sure what to do with this form you set out!

Reality is built on a foundation of dreams.
 
Suggestion:
Inspect the generated HTML of the form and see if the name attribute is correctly set. If you post a link I'll look at it.
 
print_r($_POST); - take note of the values in the array.

DERAIL slightly.

If you show say 10 lines of details, and you only update 1, you are creating an unnecessary amount of work for the server.
If each line has its own update button, you can be more efficient - maybe.
Depends realistically on how much data you will be changing at one time.

Otherwise loop through
the sets and update the db, we assigned each item a number so
id1,sub1,vtel1 go together, id2,sub2,vtel2 make the next set and so on.

You know how many sets there are from the loop displaying the records so pass that value to the processing form :
<input type=hidden name=total_lines value=$i>

and to process:
Code:
$i-0;
while ($i < $total_lines){
$sql = "UPDATE place SET vtel = '$_POST[vtel$i]', sub = '$_POST[sub$i]' where id = '$_POST[id$i]'";
mysql_query($sql);
$i++;

}

Untested code, but should give you the idea to learn how to fish.


______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks for your help. I get this error though:
Parse error: parse error, unexpected T_VARIABLE, expecting ']' in C:\Program Files\Apache Group\Apache2\htdocs\imperial\vote\addmod\changeallprocess.php on line 15

I was also thinking along those lines???

Reality is built on a foundation of dreams.
 
question now is, whats line 15 ... and all lines up to 15....

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
The problem I found is the syntax to join a variable to the output of the form.
(ie $_POST[vtel] works but $_POST[vtel$i] gives the error:
Parse error: parse error, unexpected T_VARIABLE, expecting ']')

So the trick is, I'd imagine, is to find out the correct syntax in joining the $_POST[data] to a variable.

Reality is built on a foundation of dreams.
 
oops my bad ...

Code:
$sql = "UPDATE place SET vtel = '$_POST[vtel".$i."]', sub = '$_POST[sub".$i."]' where id = '$_POST[id".$i."]'";

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
KarveR and DRJ478 thank you soooo much. Here, have some stars!!!!

Reality is built on a foundation of dreams.
 
Now I have another error about the same thing:
Parse error: parse error, unexpected '"', expecting ']'

This is really getting to me!!!

Reality is built on a foundation of dreams.
 
overyde if its really causing you probs, mail me the zipped code at
coding [AT] virtualkev dot com and I'll take a closer look.

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top