Jump to content

Updating multiple Rows in one page.


Ryflex

Recommended Posts

Hi all,

 

I'm currently working on an admin page and I'm trying to figure out how I can make an update query where all information will be updated.

The code below is where I extract all the Users and their status from the database and now I when the statuses are updated in the text fields they need to be send to the database. Does anyone know how this could be done?

 

Thnx Ryflex

 

<?php

    require_once('auth.php');
    require_once('config.php');

$global_dbh         = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
                      or die("Could not connect to database");
                      mysql_select_db(DB_DATABASE, $global_dbh)
                      or die("Could not select database");

$user_query			= "SELECT *
				   FROM members";
$user_result		= mysql_query($user_query);
?>
<form ID="gotoresource" NAME="gotoresource" METHOD="POST" ACTION="admin_exec.php"> 
<table width="500" border="1" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><b>ID</b></td>
<td><b>User</b></td>
<td><b>Status</b></td>
</tr>
<?php
while($row = mysql_fetch_assoc($user_result))
{
echo "<tr>";
echo "<td>"; 
echo $row['member_id'];
echo "</td>";
echo "<td>"; 
echo $row['login'];
echo "</td>";
?>
<td>
<input type="text" name="status" value="<?php echo $row['status'];?>" />
</td>
    <?php
echo "</tr>";
}	

?>
<input type="image" src="/images/button.gif" alt="Submit button">
</form> 
<html>
<head>
<title>AdminPage</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>

</body>

</html>

Link to comment
Share on other sites

Hi there,

you need to update the below code:

<input type="text" name="status" value="<?php echo $row['status'];?>" />

 

to this in order to have an array of all status extracted from the database

<input type="text" name="status[]" value="<?php echo $row['status'];?>" />

 

 

Then in your script that will handle the update, you can do something like this

 

foreach ($_POST['status'] AS $status){
     // Apply your MYSQL update statement here
}

 

 

 

Link to comment
Share on other sites

Hi,

 

Thnx OOP for the reaction. I changed the $status to $status[] and made following as the exec page. Somehow it takes the last status number in the table and sets every row in the table with that number....

Anyone knows how to solve that???

 

Thnx Ryflex

 

<?php

    require_once('auth.php');
    require_once('config.php');

$global_dbh         = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
                      or die("Could not connect to database");
                      mysql_select_db(DB_DATABASE, $global_dbh)
                      or die("Could not select database");

foreach ($_POST['status'] AS $status)
{
$stat				= $_POST['status'];
$update				= "UPDATE members
                       SET status = '$stat'";
$update_result		= mysql_query($update);
echo "$status<BR>";
}




?>

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.