Jump to content

How to update different rows in teh same table with a form all in one? PHP


Stalingrad

Recommended Posts

Hey. Third question today. Very stuck on this one. I am displaying a ton of while loops to display a bunch of data from two different tables. there is NO possible way i can combine the two tables. I mean, I can, but I'm not going to because they are two totally different thins. Anyway. Here is what I'm trying to do:

Lets say I have 3 items, a grape, an orange and an apple.

- 3 apples

- 2 oranges

- 1 grape

 

I'm displaying an update price box for each different fruit. (so 3 different boxes), and one submit button.

 

I update it, and it does update the price but it only updates the very first row. I want to be able to update every row I types the price in for. Here is my code: (it is quite long):

<?php
session_start();
include("config536.php");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<?php
if(!isset($_SESSION['username'])) {
echo "<banner></banner><nav>$shownavbar</nav><ubar><a href=login.php>Login</a> or <a href=register.php>Register</a></ubar><content><center><font size=6>Error!</font><br><br>You are not Logged In! Please <a href=login.php>Login</a> or <a href=register.php>Register</a> to Continue!</center></content>";
}

if(isset($_SESSION['username'])) {
echo "<nav>$shownavbar</nav><ubar>$ubear</ubar><content><center><font size=6>User Shops</font><br><br>";

$action = $_GET['action'];

if(!isset($action)) {
echo "<a href=?action=edit>Edit Shop</a> | <a href=?action=view&user=$showusername>View Shop</a> | <a href=?action=stock>View Stock</a> | <a href=?action=quick>Quick Stock</a><br><br>";
}
if(isset($action)) {
$getshopq = "SELECT * FROM usershops WHERE owner='$showusername'";
$getshop = mysql_query($getshopq);
while($tri = mysql_fetch_array($getshop)) {
$theid = $tri['ushopid'];
$owner = $tri ['owner'];
$desc = $tri['description'];
$image = $tri['image'];
$name = $tri['name'];
$status = $tri['status'];
$discount = $tri['discount'];
}

$countq = "SELECT * FROM usershops WHERE owner='$showusername'";
$countit = mysql_query($countq);


if($action == "edit") {
echo "<a href=?action=edit>Edit Shop</a> | <a href=?action=view&user=$showusername>View Shop</a> | <a href=?action=stock>View Stock</a> | <a href=?action=quick>Quick Stock</a><br><br><font size=5>Edit Shop</font><br><br>";
if(mysql_num_rows($countit)) {
?>
<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
<?php
}
if(!mysql_num_rows($countit)) {
$create = $_POST['create'];
$thename = $_POST['thename'];
$thedesc = $_POST['thedesc'];
$thestatus = $_POST['thestatus'];
$theimage = $_POST['theimage'];
if(!isset($create)) {
?>
<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
Shop Name: <input type="text" name="thename"><br>Description<br><textarea name="thedesc" rows="5" cols="25"></textarea><br>
Status: <select name="thestatus">
<option>Open</option>
<option>Closed</option>
<option>Gallery</option>
</select><br><br>
<input type="submit" name="create" value="Create Shop"></form>
<?php
}
if(isset($create)) {
if($thename == "" || $thedesc == "") {
echo "<font color=red>Error! Please fill out the entire form!</font><br><br>";
?>
<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
Shop Name: <input type="text" name="thename"><br>Description<br><textarea name="thedesc" rows="5" cols="25"></textarea><br>
Status: <select name="thestatus">
<option>Open</option>
<option>Closed</option>
<option>Gallery</option>
</select><br><br>
<input type="submit" name="create" value="Create Shop"></form>
<?php
}
if($thename != "" && $thedesc != "") {
mysql_query("INSERT INTO usershops (owner, name, description, image, status, discount) VALUES ('$showusername', '$thename', '$thedesc', 'image.gif', '$thestatus', 'None')");
echo "<font color=green>Success! You have created your shop! Click <a href=?action=view>here</a> to view it.</font><br><br>";
}
}
}

}

if($action == "view") {
echo "<a href=?action=edit>Edit Shop</a> | <a href=?action=view&user=$showusername>View Shop</a> | <a href=?action=stock>View Stock</a> | <a href=?action=quick>Quick Stock</a><br><br><font size=5>Shop Name Here</font><br><br>";

$wowq = "SELECT * FROM uitems WHERE username='$showusername' AND location='2'";
$wow = mysql_query($wowq);
while($wrow = mysql_fetch_array($wow)) {
$umid = $wrow['uitemid'];
$umiid = $wrow['theitemid'];
$umloc = $wrow['location'];
$umprice = $wrow['price'];

$infoq = "SELECT * FROM items WHERE itemid='$umiid'";
$info = mysql_query($infoq);
while($row = mysql_fetch_array($info)) {
$myname = $row['name'];
$myid = $row['itemid'];
$myimage = $row['image'];
$mydesc = $ow['description'];
$myrarity = $row['rarity'];

echo "$myname";
}
}
}
if($action == "stock") {
$setprice = $_POST['price'];
$updateprice = $_POST['updateprice'];

echo "<a href=?action=edit>Edit Shop</a> | <a href=?action=view&user=$showusername>View Shop</a> | <a href=?action=stock>View Stock</a> | <a href=?action=quick>Quick Stock</a><br><br><font size=5>Stock Shop</font><br><br>";
$eq = "SELECT * FROM uitems WHERE username='$showusername' AND location='2' GROUP BY theitemid";
$ee = mysql_query($eq);
while($erow = mysql_fetch_array($ee)) {

$eeloc = $erow['location'];
$eeid = $erow['theitemid'];
$eenowid = $erow['uitemid'];
$eeprice = $erow['price'];

$wq = "SELECT * FROM items WHERE itemid='$eeid'";
$ww = mysql_query($wq);
while($wrow = mysql_fetch_array($ww)) {

$cq = mysql_query("SELECT * FROM uitems WHERE username='$showusername' AND location='2' AND theitemid='$eeid'");
$lcq = mysql_num_rows($cq);
$fid = $wrow['itemid'];
$fname = $wrow['name'];
$fimage = $wrow['image'];
$frarity = $wrow['rarity'];
$fdesc = $wrow['description'];

echo "$fname<br><img src=/images/items/$fimage>";
?>
<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
<input type="text" name="price" maxlength="5"><br><br>
<?php
}
}
if(isset($updateprice)) {
mysql_query("UPDATE uitems SET price='$setprice' WHERE username='$showusername' AND theitemid='$fid'");
echo "<font color=green>Success! Your prices have been set and updated!</font>";
}
?>
<input type="submit" name="updateprice" value="Update Prices">
<br>
<?php

}

if($action == "quick") {
echo "<a href=?action=edit>Edit Shop</a> | <a href=?action=view&user=$showusername>View Shop</a> | <a href=?action=stock>View Stock</a> | <a href=?action=quick>Quick Stock</a><br><br><font size=5>Quick Stock</font><br><br>";
}
}

}
?>

</html>

 

To help you find it, I'm asking about the "stock" $_GET part, about three quarters down. Thanks in advance for anybody who can help me, I've been stuck on this script all day! Thanks again!

Link to comment
Share on other sites

Your html form tag:

 

<form action="<?php echo "$PHP_SELF"; ?>" method="POST">

 

Appears inside your loop:

 

while($wrow = mysql_fetch_array($ww)) {

 

If you can do so without breaking your logic, remove the form tag from the loop (so all the inputs are included in a single form) and all your updates will get submitted instead of just the 1 update inside the 1 form that's submitted.

 

That being said, I really only glossed over the code. While I'm certain this is the problem, the exact fix may be more complicated.

Link to comment
Share on other sites

I noticed you have:

 

$action = $_GET['action'];

 

Where do you ever set the get variable action?

 

<form action="<?php echo "$PHP_SELF"; ?>" method="POST">

 

It's not in the form action, so when you submit the form you're submitting it with no get action set. Perhaps this is your problem?

 

I'd really like to help you more, but you've provided absolutely NO formatting to your code, which makes it beyond hard to read. I can't tell when I'm inside an if statement, when I'm in a loop, or where I am! If you could add tabbing or spaces so that your code was more legible it would be a LOT easier to help you.

Link to comment
Share on other sites

You would use an array for the form field name, where the array index is the id of the corresponding item. See this link - http://us2.php.net/manual/en/faq.html.php#faq.html.arrays

 

Then in your form processing code, you would iterate over that array using a foreach(){} loop, to get the array index value (id) and the corresponding price to use in the update query that would be inside that foreach(){} loop.

Link to comment
Share on other sites

Here's a generic example of using an array as a form field name that you can adapt to your code -

 

<?php

// fake data (pretend this came from a database query) -
$fake[] = array('id'=>'3','title'=>'apples','price'=>3.23);
$fake[] = array('id'=>'1','title'=>'grapes','price'=>1.67);
$fake[] = array('id'=>'2','title'=>'oranges','price'=>2.56);



// form processing
if(isset($_POST['update'])){
// pretend there is needed filtering, validation, and escaping logic here...

foreach($_POST['price'] as $key => $value){
	$query = "UPDATE your_table SET price=$value WHERE id = $key";
	echo $query . '<br />'; // show the resulting query
}
}

// form
echo "<form method='post' action=''>";
// while(){ // your actual while statement would go here...
foreach($fake as $row){ // loop over the fake data in place of the above while(){ loop.
echo "{$row['title']}: <input type='text' name='price[{$row['id']}]' value='{$row['price']}'><br />";
}
echo "<input type='submit' name='update'></form>";

Link to comment
Share on other sites

In order to send multiple values from/through a form, the field names must be unique and there must be some way of associating each value with the item it applies to. By using an array for the set of fields, the fields are uniquely identified by their id and you can use php's array functions to process the data that gets submitted.

 

I recommend putting just the code I posted into a .php file and trying it. Also, look at the 'view source' of the form in your browser so that you can see what it is doing.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.