Jump to content

Button to update database incremetally


fxb9500

Recommended Posts

Hi, I am working on a page where people will be able to submit their suggestions to a database and then people can vote them up as good ideas.  I have a column in my database for the popularity of each entry, and I'm trying to create a form that will consist of a hidden object (the entry's popularity + 1) and a button that will submit the form and update the popularity value.  I just cannot for the life of me get it to work properly  :( 

Here's the code, and thanks in advance for your help.

<?php require_once('Connections/Database1.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$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;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
<!--
.Default {
font-family: Tahoma, Geneva, sans-serif;
font-size: 12px;
font-style: normal;
line-height: normal;
font-weight: normal;
font-variant: normal;
color: #000;
}
.Default table {
text-align: center;
}
-->
</style>
</head>

<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>
  <?php } while ($row_dbsuggestions = mysql_fetch_assoc($dbsuggestions)); ?>
</body>
</html>
<?php
mysql_free_result($dbsuggestions);
?>

Link to comment
https://forums.phpfreaks.com/topic/203829-button-to-update-database-incremetally/
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.