database won't update :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

PHPnewbienumberx
Forum Newbie
Posts: 12
Joined: Wed Jul 19, 2006 6:19 pm

database won't update :(

Post by PHPnewbienumberx »

Code: Select all

<?php require_once('Connections/pparty.php'); ?>
<?php
$maxRows_rsRecent = 20;
$pageNum_rsRecent = 0;
if (isset($_GET['pageNum_rsRecent'])) {
  $pageNum_rsRecent = $_GET['pageNum_rsRecent'];
}
$startRow_rsRecent = $pageNum_rsRecent * $maxRows_rsRecent;

$colname_rsRecent = "1";
if (isset($_GET['id'])) {
  $colname_rsRecent = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_pparty, $pparty);
$query_rsRecent = sprintf("SELECT id, title, links, type, `desc`, rating, ratings, `timestamp` FROM plinks WHERE id = %s", $colname_rsRecent);
$query_limit_rsRecent = sprintf("%s LIMIT %d, %d", $query_rsRecent, $startRow_rsRecent, $maxRows_rsRecent);
$rsRecent = mysql_query($query_limit_rsRecent, $pparty) or die(mysql_error());
$row_rsRecent = mysql_fetch_assoc($rsRecent);

if (isset($_GET['totalRows_rsRecent'])) {
  $totalRows_rsRecent = $_GET['totalRows_rsRecent'];
} else {
  $all_rsRecent = mysql_query($query_rsRecent);
  $totalRows_rsRecent = mysql_num_rows($all_rsRecent);
}
$totalPages_rsRecent = ceil($totalRows_rsRecent/$maxRows_rsRecent)-1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>piranhaparty.com - Where piranhas go to party</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
	background-image: url(bg.gif);
}
.desc {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}
.style2 {
	color: #009900;
	font-weight: bold;
}
-->
</style>

<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
  <tr>
    <td colspan="3"> <img id="piranhaparty_01" src="images/topdetails.gif" width="760" height="149" alt="" /></td>
  </tr>
  <tr>
    <td rowspan="2"> <img src="images/piranhaparty_02.gif" alt="" width="162" height="530" border="0" usemap="#Map" id="piranhaparty_02" /></td>
    <td width="573" height="516" bgcolor="#FFFFFF"><div style="overflow:auto; width:573px; height:516px;">      
      <h1 align="center"><?php echo $row_rsRecent['title']; ?></h1>
      <p><a href="<?php echo $row_rsRecent['links']; ?>" target="_blank"><?php echo $row_rsRecent['links']; ?></a></p>
      <p>Submitted: <?php echo $row_rsRecent['timestamp']; ?><br><?php 
//rating system
if ($mode=="vote") 
{ 
//check cookie 
if(isset($_COOKIE[$cookie])) 
{ 
echo "Sorry, you have already rated."; 
} 
//set cookie 
else 
{ 
$month = 2592000 + time(); 
setcookie(Mysite.$id, Voted, $month); 
//update rating in database 
mysql_query("UPDATE plinks SET rating = rating+$voted, ratings = ratings+1 WHERE id = %s") or die(mysql_error());  
echo "Thanks for rating."; 
} 
}


//output rating 
$current = $row_rsRecent['rating'] / $row_rsRecent['ratings']; 
echo "Current Rating: " . round($current, 1) . "<br>";  
echo "Rate: "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$row_rsRecent['id'].">1</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$row_rsRecent['id'].">2</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$row_rsRecent['id'].">3</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$row_rsRecent['id'].">4</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$row_rsRecent['id'].">5</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=6&id=".$row_rsRecent['id'].">6</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=7&id=".$row_rsRecent['id'].">7</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=8&id=".$row_rsRecent['id'].">8</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=9&id=".$row_rsRecent['id'].">9</a> | "; 
echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=10&id=".$row_rsRecent['id'].">10</a>"; 
?>
      </p>
      <p><span class="style2">Description:</span><br>
        <?php echo $row_rsRecent['desc']; ?></p>
    </div></td>
    <td rowspan="2"> <img id="piranhaparty_04" src="images/piranhaparty_04.gif" width="25" height="530" alt="" /></td>
  </tr>
  <tr>
    <td> <img id="piranhaparty_05" src="images/piranhaparty_05.gif" width="573" height="14" alt="" /></td>
  </tr>
</table>
<map name="Map" id="Map">
  <area shape="rect" coords="20,8,124,24" href="index.php" />
  <area shape="rect" coords="20,123,111,142" href="games.php" />
  <area shape="rect" coords="20,146,98,165" href="misc.php" />
  <area shape="rect" coords="20,188,80,205" href="submit.php" />
  <area shape="rect" coords="20,30,131,48" href="list.php" />
  <area shape="rect" coords="20,53,110,72" href="videos.php" />
  <area shape="rect" coords="20,77,122,96" href="pictures.php" />
  <area shape="rect" coords="20,101,101,119" href="audio.php" />
</map>

</body>
</html>
<?php
mysql_free_result($rsRecent);
?>
i've set up this rating system, but for some reason, when you click to vote, it does not submit to the database
you might want to look around:

Code: Select all

//update rating in database 
mysql_query("UPDATE plinks SET rating = rating+$voted, ratings = ratings+1 WHERE id = %s");
p.s., i'm not getting any errors with error reporting
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I searched the page for 'mysql_error()' and only found it twice, but I know there are more mysql_query()'s than that in there. try 'or die'ing all of them and see what you get.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You never sprintf'ed your %s in your query, so the DB is looking for an id that equals %s. Since there isn't one, the search returns zero rows available for update.
PHPnewbienumberx
Forum Newbie
Posts: 12
Joined: Wed Jul 19, 2006 6:19 pm

Post by PHPnewbienumberx »

Everah wrote:You never sprintf'ed your %s in your query, so the DB is looking for an id that equals %s. Since there isn't one, the search returns zero rows available for update.
ok, i did that, but not it returns "unexpected T_STRING on line 78"

here is line 78 and 79 for reference:

Code: Select all

mysql_query sprintf("UPDATE plinks SET rating = rating+$voted, ratings = ratings+1 WHERE id = %s") or die(mysql_error());  
echo "Thanks for rating.";

EDIT--error goes away when i remove sprintf
Last edited by PHPnewbienumberx on Thu Jul 20, 2006 10:29 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

What are you sprintf'ing? The sprintf() function takes at least two parameters... the format (%s) and the argument (which is missing in your case). Have a look at your other query to see how the argument was passed to it.
PHPnewbienumberx
Forum Newbie
Posts: 12
Joined: Wed Jul 19, 2006 6:19 pm

Post by PHPnewbienumberx »

bah, i screwed the sprintf function and tried a way around, but to no avail, database still won't update :(

Code: Select all

$oldid = $_GET['id'];
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month);
//update rating in database
mysql_query ("UPDATE plinks SET rating = rating+$voted, ratings = ratings+1 WHERE id = $oldid") or die(mysql_error());
echo "Thanks for rating.";
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Fix your code, to something like...

Code: Select all

<?php
$oldid = $_GET['id'];
$month = 2592000 + time();
setcookie('Mysite'.$id, 'Voted', $month);
//update rating in database
mysql_query("UPDATE `plinks` SET `rating` = rating+$voted, ratings = ratings+1 WHERE id = $oldid") or die(mysql_error());
echo "Thanks for rating.";
}
?>
You may also want to run some of this through mysql_affected_rows just to make sure that there are rows being updated.
PHPnewbienumberx
Forum Newbie
Posts: 12
Joined: Wed Jul 19, 2006 6:19 pm

Post by PHPnewbienumberx »

doh, tried that, still won't work :(
Last edited by PHPnewbienumberx on Thu Jul 20, 2006 10:58 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is it deja vu, or is that the same exact post that I just answered? :twisted:
PHPnewbienumberx
Forum Newbie
Posts: 12
Joined: Wed Jul 19, 2006 6:19 pm

Post by PHPnewbienumberx »

Everah wrote:Is it deja vu, or is that the same exact post that I just answered? :twisted:
don't know how that happened!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

PHPnewbienumberx wrote:doh, tried that, still won't work :(
Try running the query manually through phpMyAdmin, substituting real values for the vars to see what is returned by PMA.
PHPnewbienumberx
Forum Newbie
Posts: 12
Joined: Wed Jul 19, 2006 6:19 pm

Post by PHPnewbienumberx »

Everah wrote:
PHPnewbienumberx wrote:doh, tried that, still won't work :(
Try running the query manually through phpMyAdmin, substituting real values for the vars to see what is returned by PMA.
well, the query worked in phpmyadmin
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

That means there is something in your code that is setting the var values to something other than what you expected.
PHPnewbienumberx
Forum Newbie
Posts: 12
Joined: Wed Jul 19, 2006 6:19 pm

Post by PHPnewbienumberx »

Everah wrote:That means there is something in your code that is setting the var values to something other than what you expected.
WOOT! solved it!

Code: Select all

$mode = $_GET['mode'];
if ($mode=="vote")
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Congrats.
Post Reply