Storing form values in mysql from form created by loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Storing form values in mysql from form created by loop

Post by KeeganWolf »

I'm trying to list data in a table, and allow the user to modify and store the information. I have two files, one to create the form, and one to proccess it 'proc_print.php' .
I'm kinda new at this, and I'm trying to figure out why it won't save the information to my mysql table. :banghead:
Here's what I have..

Code: Select all

 
 
<?php
// Make a MySQL Connection
include_once "../scripts/connect_to_mysql.php";
 
// Create a page with the specified table info
 
$store = "4071";
$local = "WF";
$query = "SELECT * FROM Inventory$store WHERE local='$local'";
echo "<table width=800 border=1 align=center><tr>";
echo "<td width=200><strong>Store: ", $store, "</strong></td>";
echo "<td width=584><strong>Step 1. Walk In Freezer</strong></td></tr></table><p><br /></p>";
 
$result = mysql_query($query) or die(mysql_error());
$content = <<<CONT
<table border="1" align="center">
<tr>
<th>Product ID</th>
<th>Description</th>
<th>Quantity</th>
<th>Case</th>
<th>Build2</th>
</tr>
CONT;
 
while($row = mysql_fetch_array($result)){
$content .= <<<CON
<tr>
<td>{$row['fomez']}</td>
<td>{$row['desc']}</td>
<td><input type='text' name='quant' value='{$row['quant']}'></td>
<td>{$row['quancase']}</td>
<td><form method='post' action='proc_print.php'><input type='text' name='buil2' value='{$row['buil2']}'></td>
</tr>
CON;
}
$content .= "</table>";
echo $content;
echo "<input type='submit' value='Continue to the next step.'></form>\n";
/// print_r($case);
?>
 
And this file to process the information.

Code: Select all

 
<?php
// Make a MySQL Connection
include_once "../scripts/connect_to_mysql.php";
 
$quant = $_REQUEST['quant'];
$buil2 = $_REQUEST['buil2'];
/***********************************************/
 $case = addslashes($case);
 $case = htmlentities($case);
 $buil2 = addslashes($case);
 $buil2 = htmlentities($case);
/***********************************************/
$deduct = "UPDATE Inventory4071 SET quant='$quant' WHERE fomez=fomez LIMIT 1";
$result = mysql_query($deduct);
 
//if (mysql_query("UPDATE Inventory4071 SET quant='$quant' WHERE fomez=fomez LIMIT 1;")) {
//echo "success";
//}else{
//echo "failed";
//}
 
?>
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Storing form values in mysql from form created by loop

Post by mattpointblank »

Take a look at my reply here:

viewtopic.php?p=543242#p543242

Try adding the 'or die(mysql_error())' part after you run mysql_query and see what it's telling you.
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Re: Storing form values in mysql from form created by loop

Post by KeeganWolf »

Tried that, I'm not getting any sort of error. When I look to my table though, nothing has changed.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Storing form values in mysql from form created by loop

Post by mattpointblank »

Print out the query that's being run. Until you know exactly what's going into the database, it's hard to find the issue.
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Re: Storing form values in mysql from form created by loop

Post by KeeganWolf »

Ok, changed the code for the proc_print.php abit.

Code: Select all

 
// Make a MySQL Connection
include_once "../scripts/connect_to_mysql.php";
 
$quant = $_REQUEST['quant'];
$buil2 = $_REQUEST['buil2'];
/***********************************************/
 $case = addslashes($case);
 $case = htmlentities($case);
 $buil2 = addslashes($case);
 $buil2 = htmlentities($case);
/***********************************************/
$deduct = "UPDATE Inventory4071 SET quant='$quant' WHERE fomez=fomez LIMIT 1";
mysql_query($deduct) or die("Error: " . mysql_error() . "<br />" . $deduct );
echo $deduct;
//if (mysql_query("UPDATE Inventory4071 SET quant='$quant' WHERE fomez=fomez LIMIT 1;")) {
//echo "success";
//}else{
//echo "failed";
//}
 
Now what looks like is happening is that it's only saving the value for $quant in the first item listed by the loop.
Maybe I need to place the form action somewhere else in the script?
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Storing form values in mysql from form created by loop

Post by mattpointblank »

Your <form> only includes one of the variables you're trying to pass - open it before you list the first input.
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Re: Storing form values in mysql from form created by loop

Post by KeeganWolf »

Something like this?

Code: Select all

 
CONT;
 
while($row = mysql_fetch_array($result)){
$content .= <<<CON
<tr>
<form method='post' action='proc_print.php'>
<td>{$row['fomez']}</td>
<td>{$row['desc']}</td>
<td><input type='text' name='quant' value='{$row['quant']}'></td>
<td>{$row['quancase']}</td>
<td><input type='text' name='buil2' value='{$row['buil2']}'></td>
</tr>
CON;
}
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Storing form values in mysql from form created by loop

Post by mattpointblank »

Yep - try that!
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Re: Storing form values in mysql from form created by loop

Post by KeeganWolf »

It only updated 'quant' on the first row. The other table values didn't change, and the echo result was

UPDATE Inventory4071 SET quant='44' WHERE fomez=fomez LIMIT 1
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Storing form values in mysql from form created by loop

Post by mattpointblank »

Ah, you want to update several rows at once? Your current code is producing a form for each update button, so it will only update that row when clicked. To update several rows simultaneously, you need to set your form inputs as arrays (code is like <input name="array[]" type="text" /> - google how to do this. The entire table needs to be wrapped in a form so that all the inputs are associated with the same form - once you change your form fields to arrays you can loop through them in the php and insert the data.

Also, your query says LIMIT 1 so obviously this is only going to affect 1 row in this case anyway.
Post Reply