Jump to content

dynamically updating a db value?


fxb9500

Recommended Posts

Hello,

I am trying to get a page up where users can post a suggestion and other people can vote up it's popularity if they think it's a good idea.  I have a column in the suggestion database called popularity (default of 1) that I am trying to change to it's current value + 1 when someone presses a button.  I can not for the life of me figure out how this is done :(

 

Here's my code:


$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE dbsuggestions SET popularity=%s WHERE `user`=%s",
                       GetSQLValueString($_POST['voteup'], "text"),
                       GetSQLValueString($_POST['voteup'], "text"));

  mysql_select_db($database_Database1, $Database1);
  $Result1 = mysql_query($updateSQL, $Database1) or die(mysql_error());
}

$maxRows_dbsuggestions = 10;
$pageNum_dbsuggestions = 0;
if (isset($_GET['pageNum_dbsuggestions'])) {
  $pageNum_dbsuggestions = $_GET['pageNum_dbsuggestions'];
}
$startRow_dbsuggestions = $pageNum_dbsuggestions * $maxRows_dbsuggestions;

mysql_select_db($database_Database1, $Database1);
$query_dbsuggestions = "SELECT * FROM dbsuggestions";
$query_limit_dbsuggestions = sprintf("%s LIMIT %d, %d", $query_dbsuggestions, $startRow_dbsuggestions, $maxRows_dbsuggestions);
$dbsuggestions = mysql_query($query_limit_dbsuggestions, $Database1) or die(mysql_error());
$row_dbsuggestions = mysql_fetch_assoc($dbsuggestions);

if (isset($_GET['totalRows_dbsuggestions'])) {
  $totalRows_dbsuggestions = $_GET['totalRows_dbsuggestions'];
} else {
  $all_dbsuggestions = mysql_query($query_dbsuggestions);
  $totalRows_dbsuggestions = mysql_num_rows($all_dbsuggestions);
}
$totalPages_dbsuggestions = ceil($totalRows_dbsuggestions/$maxRows_dbsuggestions)-1;
?>

// and here's my actual form...

<body class="Default">

<p>Here are some suggestions for new databases.  If you like an idea, vote for it. </p>
<p> </p>
<?php do { ?>
  <table width="925" border="0">
    <tr>
      <th width="137" height="36" scope="col"><?php echo $row_dbsuggestions['user']; ?></th>
      <th width="144" scope="col"><?php echo $row_dbsuggestions['email']; ?></th>
      <th width="298" scope="col"><?php echo $row_dbsuggestions['suggestion']; ?></th>
      <th width="177" scope="col"><?php echo $row_dbsuggestions['popularity']; ?></th>
      <th width="147" scope="col"><form name="form1" method="POST" action="<?php echo $editFormAction; ?>">
        <input type="hidden" name="voteup" disabled="1" value="<?php echo $row_dbsuggestions['popularity']+1; ?>">
        <input type="hidden" name="MM_update" value="form1">
        <input name="submit" type="submit" class="Default" value="Good Idea">
      </form></th>
    </tr>
  </table>

 

That isn't the whole code... but thanks in advance for your help :)

 

UPDATE:

I realized I hadn't properly specified the primary key for the update, so I did that... and got a response from the server: column "popularity" can not be null

Link to comment
https://forums.phpfreaks.com/topic/203830-dynamically-updating-a-db-value/
Share on other sites

nevermind, I got it.  :)

 

the problem was the first GetSQLValueString here:

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE dbsuggestions SET popularity=%s WHERE `user`=%s",
                       GetSQLValueString($_POST['voteup'], "text"),
                       GetSQLValueString($_POST['voteup'], "text"));

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.