Page 1 of 1

Enable users to change information that's echo'ed from loop

Posted: Wed May 27, 2009 10:36 am
by KeeganWolf
I'm trying to create a loop that will list all the information within a part of a mysql table, while including a form that will allow them to change the value in a field and save those changes to the table.

I'm not sure about how to post this information after the user has entered the changes.

Here's what I have so far.

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'"; 
     
$result = mysql_query($query) or die(mysql_error());
 
echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Product ID</th>";
echo "<th>Description</th>";
echo "<th>Quantity</th>";
echo "<th>Case</th>";
echo "<th>case</th></tr>";
while($row = mysql_fetch_array($result)){
 
    extract($row);
    echo "<tr><td>";
    echo $row['fomez'];
    echo "</td><td>";
    echo $row['desc'];
    echo "</td><td>";
    echo $row['quant'];
    echo "</td><td>";
    echo $row['case'];
    echo "</td><td>";
    echo "<input type='text' method='post' value='$case'></form>\n";
    //echo "";
    echo "</td></tr>";
}
echo "</table>";
echo "<input type='submit' value='Continue to the next step.'></form>\n";
 
?>

Re: Enable users to change information that's echo'ed from loop

Posted: Wed May 27, 2009 12:42 pm
by anand
Hello, I have 1 advice for you. rather than echo'ing so many times, why don't you use a here document?

You want user to change the value of case, right?

Re: Enable users to change information that's echo'ed from loop

Posted: Wed May 27, 2009 12:50 pm
by anand

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'"; 
     
$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>case</th>
</tr>
CONT;
 
while($row = mysql_fetch_array($result)){
$content .= <<<CON
 
<tr>
<td>{$row['fomez']}</td>
<td>{$row['desc']}</td>
<td>{$row['quant']}</td>
<td>{$row['case']}</td>
<td><form method='post' action='abc.php'><input type='text' name='case' value='$case'></form></td>
</tr>
CON;
}
$content .= "</table>";
echo $content;
echo "<input type='submit' value='Continue to the next step.'></form>\n";
 
?>

Re: Enable users to change information that's echo'ed from loop

Posted: Wed May 27, 2009 1:08 pm
by KeeganWolf
Excellent.
That brings me to ask, the form 'abc.php' would be where the values for 'case' would go to be posted? Is there something I can use within it to modify the 'case' column in my 'inventory4071' table per the users input?

Thank you so much!

Re: Enable users to change information that's echo'ed from loop

Posted: Wed May 27, 2009 2:04 pm
by anand
KeeganWolf wrote: Excellent.
That brings me to ask, the form 'abc.php' would be where the values for 'case' would go to be posted?
Yes, Thats right.
KeeganWolf wrote:Is there something I can use within it to modify the 'case' column in my 'inventory4071' table per the users input?

Thank you so much!
First, you need to get the value and if you want, you can assign it to some variable.

Code: Select all

$case = $_REQUEST['case'];
 
// I am filtering the values which i got. Its optional. You can do this, if you want to do. Its recommended by me though.
/***********************************************/
 $case = addslashes($case);
 $case = htmlentities($case);
 
// Identifier = your identifier.
/***********************************************/
 
if(mysql_query("UPDATE inventory4071 SET case='$case' WHERE identifier=identifier LIMIT 1;")) {
 
echo "success";
}else{
echo "failed";
}