Jump to content

Updating more then one row


tjverge

Recommended Posts

I'm back, I have a code that based on my understanding should work, but needless to say doesn't

 

<?php
$sql ="select * from `piprice`";
$result = mysql_query($sql);
$count = mysql_num_rows($result);

echo "<table border=1><tr><td>Product Name</td><td>Item ID</td><td>Price</td><td>Enabled</td>";
echo "<form action=main.php?id=test1.php method=post>";
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>".$row['product name']."</td>";
echo "<td>".$id[] = $row['item id'];$row['item id']."</td>";
echo "<td>".$row['price']."</td>";
$enabled[] = $row['enabled'];
echo"<td><select name=".$enabled."><
<option Value=".$row['enabled'].">".$row['enabled']."</option>
<option Value=Yes>Yes</option>
<option Value=No>No</option>
/select></td</tr>";
}
echo "<input name=submit type=submit value=Update>";
echo "</form></table>";
if(isset($_POST['submit']))
{

for($i=0;$i<$count;$i++){
$sql1="UPDATE `piprice` SET `enabled` = '$enabled[$i]' WHERE `item id` ='$id[$i]'";
$result1=mysql_query($sql1) or die (mysql_error());

}
}
?>

 

Basically I want to be able to pick yes/no and have it update in the database.

 

One a side note is there a good site for me to learn php on (I just know the basics) besides asking 101 questions here  :confused:

Link to comment
https://forums.phpfreaks.com/topic/227081-updating-more-then-one-row/
Share on other sites

Ok I think the problem is here

$enabled[] = $row['enabled'];
echo"<td><select name=".$enabled."><
<option Value=".$row['enabled'].">".$row['enabled']."</option>
<option Value=Yes>Yes</option>
<option Value=No>No</option>
/select>

 

I need to give it a name so I can pull the post date, the only problem is I'm unsure how to do that where it in a loop

Problem solved, code posted below for anyone else who is having this kind of problem.

 

<?php
$sql ="select * from `piprice`";
$result = mysql_query($sql);
$count = mysql_num_rows($result);

echo "<table border=1><tr><td>Product Name</td><td>Item ID</td><td>Price</td><td>Enabled</td>";
echo "<form action=main.php?id=test1.php method=post>";
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>".$row['product name']."</td>";
echo "<td>".$id[] = $row['item id'];$row['item id']."</td>";
echo "<td>".number_format($row['price'],2)."</td>";
echo"<td><select name = ename[]>
<option Value=".$row['enabled'].">".$row['enabled']."</option>
<option Value=Yes>Yes</option>
<option Value=No>No</option>
/select></td</tr>";
}
echo "<input name=submit type=submit value=Update>";
echo "</form></table>";
if(isset($_POST['submit']))
{
$ename = $_POST['ename'];
for($i=0;$i<$count;$i++){
echo $name;
$sql1="UPDATE `piprice` SET `enabled` = '$ename[$i]' WHERE `item id` ='$id[$i]'";
$result1=mysql_query($sql1) or die (mysql_error());

}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.