Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Problem updating multiple rows at once
Old 08-07-2010, 04:10 PM Problem updating multiple rows at once
Skilled Talker

Posts: 83
Trades: 0
Hi,

I've created an admin page that lists all piano sheet music that has been uploaded to my server. It displays the ID (autoincrement), Userid (user that uploaded sheet), Artist (name of the band) and Title (name of the song).
On this page, I can either delete the row completely, or click on "Uploaded", which just sets it's uploaded column to 'yes'.
Now I'm working on a button called "Uploaded All". This will set the uploaded column on all of the rows on the page to 'yes'. This way I don't have to click Uploaded for every single row.

I'm not getting any errors when clicking "Uploaded All". When clicking it, it doesn't update the table rows in the database as it should. Take a look at my code:

Tip: The area of the code that I'm currently having troubles with is here

PHP Code:
  $usersids[$i] = $row['id'];
  
$i++;
  }

  echo 
"
  <tr>
    <td align='center' width='10' colspan='6'><a href='uploadedsheets.php?confirm=all'>Uploaded All</a></td>
  </tr>"
;
  
  if (
$confirm=="all")
{
    
$i 0;
    while(
$row mysql_fetch_array($result))
  {
    
mysql_query('UPDATE `upload` SET `uploaded`="yes" WHERE id = ' $usersids[$i]);
    
$i++;
  }
    echo 
"<SCRIPT language='JavaScript'><!--
  window.location='uploadedsheets.php';//-->
</SCRIPT>"
;

Inside the above code after the if statement, I tried echoing out $usersids, and it showed correctly. So the problem must be in the update query or the array.


Full Code:

PHP Code:
<?php
session_start
();

include_once(
'../inc/connect.php');
include_once(
'../inc/admin.php'); 

if(!isset(
$_SESSION['sort_counter']))
{
$_SESSION['sort_counter'] = 1;}

if((
$_SESSION['sort_counter']%2) == 0){ //test even value
  
$sortcount "DESC";
}else{ 
//odd value
  
$sortcount "";
}

$result mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY id"); 

$unconfirmedquery mysql_query("SELECT uploaded FROM upload WHERE uploaded='no'");
$unconfirmedcount mysql_num_rows($unconfirmedquery);

$confirmedquery mysql_query("SELECT uploaded FROM upload WHERE uploaded='yes'");
$confirmedcount mysql_num_rows($confirmedquery);

$sort $_GET['sort'];
$delete $_GET['delete'];
$confirm $_GET['confirm'];

/////////////////////////////////

if ($sort=='id'){ 

    
// $result = mysql_query("SELECT * FROM users ORDER BY id");  
$result mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY id $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1//increment after every run
 
}
if (
$sort=='userid'){ 

    
// $result = mysql_query("SELECT * FROM users ORDER BY username"); 
$result mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY userid $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1//increment after every run
}
if (
$sort=='artist'){ 

    
// $result = mysql_query("SELECT * FROM users ORDER BY email"); 
$result mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY artist $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1//increment after every run
}
if (
$sort=='title'){ 

    
// $result = mysql_query("SELECT * FROM users ORDER BY email"); 
$result mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY title $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1//increment after every run
}
if (
$sort=='file'){ 

    
// $result = mysql_query("SELECT * FROM users ORDER BY email"); 
$result mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY file $sortcount"); 
$_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1//increment after every run
}

/// FIX THIS AREA
if ($confirm=="true" && isset($_GET['id']))
{
    
mysql_query('UPDATE `upload` SET `uploaded`="yes" WHERE id = ' . (int)$_GET['id']);
    echo 
"<SCRIPT language='JavaScript'><!--
  window.location='uploadedsheets.php';//-->
</SCRIPT>"
;
}  
if (
$delete=="true" && isset($_GET['id']))
{
    
mysql_query('DELETE FROM `upload` WHERE id = ' . (int)$_GET['id']);    
    echo 
"<SCRIPT language='JavaScript'><!--
  window.location='uploadedsheets.php';//-->
</SCRIPT>"
;
}  
if (
$delete=="false" && isset($_GET['id']))
{
    echo 
"<SCRIPT language='JavaScript'><!--
  window.location='uploadedsheets.php';//-->
</SCRIPT>"
;
}  

/////////////////////////////////

echo "
<html>
<head>
<title>Uploaded Sheets</title>
<style>
a:link{
text-decoration: none;
color: #519904;
}
a:visited{
text-decoration: none;
color: #519904;
}
a:hover{
text-decoration: none;
color: #4296ce;
}
#headcont{
width: 900px;
height: 20px;
margin-left: auto;
margin-right: auto;
}
#unconfirmed{
    width: 450px;
    text-align: center;
    float: left;
    background-color: #cccccc;
}
#confirmed{
    width: 450px;
    text-align: center;
    float: left;
    background-color: #72A4D2;
}
</style>
</head>
<body>
"
;

include_once(
'../inc/navadmin.php');

echo 
"<div style='font-size: 28px; text-align: center;'>Uploaded Sheets</div>
<div id='headcont'>
<div id='unconfirmed'>Not Uploaded Sheets: "
.$unconfirmedcount."</div>
<div id='confirmed'>Uploaded Sheets: "
.$confirmedcount."</div>
</div><br />
<table border='1' align='center'>
<tr>
<th bgcolor='#cccccc'><a href='uploadedsheets.php?sort=id'>ID</a></th>
<th bgcolor='#cccccc'><a href='uploadedsheets.php?sort=userid'>UserID</a></th>
<th bgcolor='#cccccc'><a href='uploadedsheets.php?sort=artist'>Artist</a></th>
<th bgcolor='#cccccc'><a href='uploadedsheets.php?sort=title'>Title</a></th>
<th bgcolor='#cccccc'><a href='uploadedsheets.php'>Uploaded</a></th>
<th bgcolor='#cccccc'><a href='uploadedsheets.php'>Delete</a></th>
</tr>"
;
echo 
"<script type='text/javascript'>
function show_delete()
{
var r=confirm('Delete?');
if (r==true)
{
// Delete
return true;
}
else
{
// Don't Delete
return false;
}
}
"
;

echo 
"
function show_undelete()
{
var r=confirm('Undelete?');
if (r==true)
{
// Undelete
return true;
}
else
{
// Don't Undelete
return false;
}
}
</script>"
;

$usersids "";
$i 0;

while(
$row mysql_fetch_array($result))
  {
  
  
//
  
$active $row['active'];
  
$color "#ffffff";
  
$deleted "Delete";
  if (
$active=='no'){
  
$color "#f43636";
  
$deleted "Undelete";
  
$active "false";
  
$alert "show_undelete";
  }
  else{
  
$active "true";
  
$alert "show_delete";
  }
  
//
  
  
echo "<tr>";
  echo 
"<td align='center' width='40' bgcolor='$color'>" .$row['id']. "</td>";
  echo 
"<td align='center' width='40'>" .$row['userid']. "</td>";
  echo 
"<td align='center' width='230'>".ucwords($row['artist'])."</td>";
  echo 
"<td align='center' width='230'>".ucwords($row['title'])."</td>";
  echo 
"<td align='center' width='10'><a href='uploadedsheets.php?confirm=true&id=" .$row['id'] . "'>Uploaded</a></td>";
  echo 
"<td align='center' width='10'><a href='uploadedsheets.php?delete=$active&id=" .$row['id']. "' onclick='return $alert()'>$deleted</a></td>";
  echo 
"</tr>";
  
$usersids[$i] = $row['id'];
  
$i++;
  }

  echo 
"
    
$usersids[0]
  <tr>
    <td align='center' width='10' colspan='6'><a href='uploadedsheets.php?confirm=all'>Uploaded All</a></td>
  </tr>"
;
  
  if (
$confirm=="all")
{
    
$i 0;
    while(
$row mysql_fetch_array($result))
  {
    
mysql_query('UPDATE `upload` SET `uploaded`="yes" WHERE id = ' $usersids[$i]);
    
$i++;
  }
    echo 
"<SCRIPT language='JavaScript'><!--
  window.location='uploadedsheets.php';//-->
</SCRIPT>"
;
}  
  
echo 
"</table>";



// Footer
echo "
</body>
</html>
"
;

?>
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-07-2010, 04:25 PM Re: Problem updating multiple rows at once
Average Talker

Posts: 25
Name: Stephen
Location: Arizona
Trades: 0
It looks like you are trying to reuse $result from mysql_query, without reseting the row pointer. Try using mysql_data_seek($result,0) to place it back to the first record.
__________________

Please login or register to view this content. Registration is FREE
and
Please login or register to view this content. Registration is FREE
GetGamesHere is offline
Reply With Quote
View Public Profile Visit GetGamesHere's homepage!
 
Old 08-07-2010, 04:38 PM Re: Problem updating multiple rows at once
Skilled Talker

Posts: 83
Trades: 0
Works! Thanks.
Smudly is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Problem updating multiple rows at once
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.81388 seconds with 12 queries