Update multiple rows from html form to mysql table

Codes here !

Moderators: egami, macek, gesf

Post Reply
DutchBen
New php-forum User
New php-forum User
Posts: 36
Joined: Thu Oct 24, 2002 10:28 am
Location: Amsterdam

Wed Nov 06, 2002 12:30 pm

Hi,

Here's a way to do it.

Code: Select all

<?
while ($myrow = mysql_fetch_array($result)) {
?>
<input type="hidden" name="id[]" value="<?=$myrow['id']?>">
<input type="Text" name="name[]" value="<?=$myrow['name']?>">
<?}?>


After submitting the form an array 'id' and an array 'name' is created (because of the [] after the name). Updating is much easier now eg.

foreach($id as $key => $value){

.........
$query = 'UPDATE table SET name='name[$key]' where id="$value"';
........

}

Hope it helps

Ben

DutchBen
New php-forum User
New php-forum User
Posts: 36
Joined: Thu Oct 24, 2002 10:28 am
Location: Amsterdam

Mon Nov 11, 2002 10:46 am

nipster2 wrote:foreach($id as $key => $value){
$sql = 'UPDATE table SET name='name[$key]' where id="$value"';
}

Sometimes you overlook the obvious.. escape your quotes like so

Code: Select all

$sql = "UPDATE table SET name='name[$key]' where id='$value'" ;


Also ... keep the mysql_query inside the foreach loop .. otherwise only the last insert will be executed.

Jacques
New php-forum User
New php-forum User
Posts: 10
Joined: Fri Oct 18, 2002 8:07 am

Fri Dec 20, 2002 3:45 pm

Hello everyone,

I am attempting to insert into table2 2 values drawn from same drop down box. The values are taken from table1

I can do it with one value. The other one is driving in circle. Can anyone help me please

Thanks

Code: Select all

$sql = mysql_query("select category from catlisting") or die("Unable to process queries at this time: " . mysql_error()); 
//echo "$sql<br>";
echo "$sql<br>";
while ($i = mysql_fetch_array($sql))
{
$category = $i["category"];
echo "$category<br>";
$result .= "<OPTION value=\"$category\">$category</OPTION>";
}
?>
        <form action="test.php" method="post">
<?
 for($i=0;$i<3;$i++){
$i = $i + 1
?>
   <input name=category type=hidden class=input value="<?php echo $i[category]; ?>" size="50" maxlength="70">
        <select class=select name=category[]>
        <option value="" >Select category<?     echo "$result"; ?>
       </option>
        </SELECT>   
<?
}
?>
    <INPUT TYPE=SUBMIT  VALUE="Enter" ID="category">   
   </FORM>

<?
      foreach $_POST["category"] as $newcat {

//      if ($category !="")   {
      $sql1 = "INSERT INTO $mysql_cat (id, username, category) "." VALUES "." ('', '$username', "$newcat")";
echo "$sql1<br>";      
      $result = mysql_query($sql1) or die("ERROR: cannot INSERT data into database");
//      }
}
?>

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 3 guests