Jump to content

How do i do a multiple record update


iceangel89

Recommended Posts

here is my paid system, it will kinda help you out with check boxs and what not:

paid.php

<?php
include ("../../style/include/session.php");
include ("../../style/include/cons/head_2.php");
$num = 0;
if($session->isAdmin()){
print '<div class="box"><h2>Select Paid</h2>';
?>

<form action="pay_do.php" method="post">

<table width="87%" border="0">
  <tr>
    <td width="42"><strong>#</strong></td>
    <td width="127"><strong>First Name </strong></td>
    <td width="148"><strong>Last Name </strong></td>
    <td width="203"><strong>Status (username) </strong></td>
  </tr>
<?php 
$paid = "select * from users order by paid";
$q = mysql_query($paid) or die('Error: ' . mysql_error());
while ($rs=mysql_fetch_array($q)) {
$id = $rs['username'];
?>
  <tr>
  <td><?php echo $num+=1; ?></td>
    <td><? echo $rs['first'];?>
</td>
    <td><? echo $rs['last'];?></td>
    <td>
<?php
If($rs['paid'] == 0 ){
echo '<input type="checkbox" name="checkbox[]" value="'.$rs['username'].'" />';
}
elseif($rs['paid'] == 1 ){
      echo "<a href='pay_dele.php?cmd=delete&id=$id'>Paid</a>";
}

echo " (".$rs['username'].")";
} 
?>


</td>
  </tr>
</table>



<br />
<input name="Submit" type="Submit" value="Update"  />
</form>

<?php
}else{
header("Location: ../../index.php");
}
print '</div>';
include ("../../style/include/cons/foot.php");
?>



 

pay_do.php

<?php
include ("../../style/include/session.php");
include ("../../style/include/cons/head_2.php");
print '<div class="box">';
if($session->isAdmin()){
if(isset($_POST['Submit'])){
$boxes = $_POST['checkbox'];
foreach($boxes as &$value){
	if($value!=='') {
             $q = "UPDATE `users` SET `paid`=1 WHERE `username`='$value'";
		echo $q.'<br />';
		mysql_query($q);
	}
}
echo "<h2>Processed</h2>";
echo 'Selected users have been processed<br><a href="paid.php">Select Paid/Non-Paid</a>';
}else{
echo "<h2>Error!</h2>";
echo 'No data to process<br><a href="paid.php">Select Paid/Non-Paid</a>';
}

}else{
header("Location: ../../index.php");
}
print '</div>';
include ("../../style/include/cons/foot.php");
?>

 

pay_dele.php

<?php
include ("../../style/include/session.php");
include ("../../style/include/cons/head_2.php");
if($session->isAdmin()){

if(!isset($cmd)) 
{
   $result = mysql_query("select * from users order by paid"); 
   while($r=mysql_fetch_array($result)) 
   { 

      $id=$r["username"];//take out the id
     
 //make the title a link
      echo "<a href='?cmd=delete&id=$id'>$id - Delete</a>";
      echo "<br>";
    }
}
if($_GET["cmd"]=="delete")
{
print '<div class="box"><h2>'.$id.' marked as not paid</h2>';
$q = "UPDATE `users` SET `paid`=0 WHERE `username`='$id'";
echo $q.'<br />';
mysql_query($q);
print '<br><a href="paid.php">Back to paid</a>';
}
}else{
header("Location: ../../index.php");
}
print '</div>';
include ("../../style/include/cons/foot.php");
?>

 

 

play around with those, they will help

Link to comment
Share on other sites

One of the "easy" ways to do mass updates is:

 

Let's say you have $_POST['checked'] as a multi-dimensional array of things that should be updated.

 

$query = sprintf('UPDATE users SET paid = 1 WHERE userid IN (%s)', join(',', $_POST['checked']));

Link to comment
Share on other sites

Update can affect 0-infinity rows for say you just need to be specific in your where clause.

 

If u are trying to update a series of items in a database that have no mysql correlation to them (meaning like fields you can generate a valid where clause for) then checkboxes are your answer.

 

Simply use the method above.

 

However lets say you want to delete all old photo galleries that have less than 10 pictures in them and more than 50 days since last upload to them.

 

MySQL can handle all that (its just an example I thought of)  you can simple use a switch for all your "targeted" updates like the one I gave to execute the query desired instead of poking around with checkboxes.

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.