Enable users to change information that's echo'ed from 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

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

Post 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";
 
?>
Last edited by Benjamin on Wed May 27, 2009 10:49 am, edited 1 time in total.
Reason: Added [code=php] tags.
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

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

Post 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?
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

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

Post 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";
 
?>
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

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

Post 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!
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

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

Post 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";
}
 
 
Post Reply