Jump to content

Survey/Poll script - continually adding +1 values to my database


rebabal

Recommended Posts

I'm making a survey/poll that is database driven.  I have everything created and working perfectly so far, except the most important part...getting the information!

I'm using DreamWeaver record sets and insertions to run most of the code.

 

4 radio buttons.  Do they all have to be named the same?  If they are, then DW can't decide which radio to apply to the DB table row.  If they're named differently, like select1, select 2, etc...then when choosing on the actual page, you can select more than one option and it screws up.

 

I just want this to use two tables, questions & answers.  Questions holds the question text, as well as up to 4 options for the users to select from.  Answers holds the Question # (the incrementing ID from the question table, so i can cross reference which question the answers apply to), the answers (I want this to be done in a single entry PER question, not several entries per answer), then the IP of each user so they can't participate more than once.

 

My problem lies when I try to use the update record function in DW.  I have variables set up to add 1 each time an option is selected, and it works...but not really.

 

I can probably explain best with showing a test page:

 

http://palmettofx.dasslehost.com/test1.php?Q=1

& test 2

http://palmettofx.dasslehost.com/test2.php?Q=2

 

Basically, you can select one option over and over and it will continue to count, but when you select other otions it defaults the others back to 0, and only ads to the one you selected.

 

How do i make this update a single row (one per question) each time a user uses the poll?

 

I may be going about this the completely wrong way, and I accept any and all advice.

 

Thanks.

Link to comment
Share on other sites

Heres the 1st test page code

 


<?php require_once('Connections/Survey.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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"] == "test")) {
  $updateSQL = sprintf("UPDATE answers SET `1`=%s, `2`=%s, `3`=%s, `4`=%s WHERE id=%s",
                       GetSQLValueString($_POST['select'], "int"),
                       GetSQLValueString($_POST['select'], "int"),
                       GetSQLValueString($_POST['select'], "int"),
                       GetSQLValueString($_POST['select'], "int"),
                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_Survey, $Survey);
  $Result1 = mysql_query($updateSQL, $Survey) or die(mysql_error());
}

$colname_answers = "-1";
if (isset($_GET['Q'])) {
  $colname_answers = (get_magic_quotes_gpc()) ? $_GET['Q'] : addslashes($_GET['Q']);
}
mysql_select_db($database_Survey, $Survey);
$query_answers = sprintf("SELECT * FROM answers WHERE id = %s", $colname_answers);
$answers = mysql_query($query_answers, $Survey) or die(mysql_error());
$row_answers = mysql_fetch_assoc($answers);
$totalRows_answers = mysql_num_rows($answers);

// Declaring
$opt1 = $row_answers['1'];
$opt2 = $row_answers['2'];
$opt3 = $row_answers['3'];
$opt4 = $row_answers['4'];

// Adding
$add1 = $opt1 + 1;
$add2 = $opt2 + 1;
$add3 = $opt3 + 1;
$add4 = $opt4 + 1;

?>
<form name = "test" action="<?php echo $editFormAction; ?>" method="POST">

This is a test with the radio options name all being the same.<br>
The options are individually selectable, but they are all named the same so any option adds +1 to all fields. Unlike <a href="test2.php">test2.php </a>
<br>
When you just hit select, it erases all entries.
<br>

<table width="300" border="2" cellspacing="0" cellpadding="0">
  <tr>
    <td width="300"colspan="3" align="center">Ye Olde Question</td>
  </tr>
  <tr>
    <td width="100" align="center">Options</td>
    <td width="100" align="center">Before Adding </td>
    <td width="100" align="center">After Adding </td>
  </tr>
  <tr>
    <td><input name="select" type="radio" value="<?php echo $add1; ?>" />
    Option 1 </td>
    <td><?php echo $opt1; ?></td>
    <td><?php echo $add1; ?></td>
  </tr>
  <tr>
    <td><input name="select" type="radio" value="<?php echo $add2; ?>" />
    Option 2 </td>
    <td><?php echo $opt2; ?></td>
    <td><?php echo $add2; ?></td>
  </tr>
  <tr>
    <td><input name="select" type="radio" value="<?php echo $add3; ?>" />
    Option 3 </td>
    <td><?php echo $opt3; ?></td>
    <td><?php echo $add3; ?></td>
  </tr>
  <tr>
    <td><input name="select" type="radio" value="<?php echo $add4; ?>" />
    Option 4 </td>
    <td><?php echo $opt4; ?></td>
    <td><?php echo $add4; ?></td>
  </tr>
  <tr>
    <td align="center"><input name="" type="submit"></td>
    <td><input name="id" type="hidden" value="1"></td>
    <td><input type="hidden" name="MM_update" value="test"></td>
  </tr>
</table>
</form>

<?php
mysql_free_result($answers);
?>

 

Here's the 2nd test page code:

 

<?php require_once('Connections/Survey.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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"] == "test")) {
  $updateSQL = sprintf("UPDATE answers SET `1`=%s, `2`=%s, `3`=%s, `4`=%s WHERE id=%s",
                       GetSQLValueString($_POST['select1'], "int"),
                       GetSQLValueString($_POST['select2'], "int"),
                       GetSQLValueString($_POST['select3'], "int"),
                       GetSQLValueString($_POST['select4'], "int"),
                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_Survey, $Survey);
  $Result1 = mysql_query($updateSQL, $Survey) or die(mysql_error());
}

$colname_questions = "-1";
if (isset($_GET['Q'])) {
  $colname_questions = (get_magic_quotes_gpc()) ? $_GET['Q'] : addslashes($_GET['Q']);
}
mysql_select_db($database_Survey, $Survey);
$query_questions = sprintf("SELECT * FROM answers WHERE id = %s", $colname_questions);
$questions = mysql_query($query_questions, $Survey) or die(mysql_error());
$row_questions = mysql_fetch_assoc($questions);
$totalRows_questions = mysql_num_rows($questions);

// Declaring
$opt1 = $row_questions['1'];
$opt2 = $row_questions['2'];
$opt3 = $row_questions['3'];
$opt4 = $row_questions['4'];

// Adding
$add1 = $opt1 + 1;
$add2 = $opt2 + 1;
$add3 = $opt3 + 1;
$add4 = $opt4 + 1;

?>

<form action="<?php echo $editFormAction; ?>" name = "test" method="POST">

This is a test with the radio options name all being different, select1, select2, select3, select4. <br>
The options are selectable but they are ALL selectable, unlike when they were named the same as in <a href="test1.php">test1.php</a>
<br>
When you just hit select, it erases all entries.
<br>

<table width="300" border="2" cellspacing="0" cellpadding="0">
  <tr>
    <td width="300"colspan="3" align="center">Ye Olde Question</td>
  </tr>
  <tr>
    <td width="100" align="center">Options</td>
    <td width="100" align="center">Before Adding </td>
    <td width="100" align="center">After Adding </td>
  </tr>
  <tr>
    <td><input name="select1" type="radio" value="<?php echo $add1; ?>" />Option 1</td>
    <td><?php echo $opt1; ?></td>
    <td><?php echo $add1; ?></td>
  </tr>
  <tr>
    <td><input name="select2" type="radio" value="<?php echo $add2; ?>" />Option 2</td>
    <td><?php echo $opt2; ?></td>
    <td><?php echo $add2; ?></td>
  </tr>
  <tr>
    <td><input name="select3" type="radio" value="<?php echo $add3; ?>" />Option 3</td>
    <td><?php echo $opt3; ?></td>
    <td><?php echo $add3; ?></td>
  </tr>
  <tr>
    <td><input name="select4" type="radio" value="<?php echo $add4; ?>" />Option 4</td>
    <td><?php echo $opt4; ?></td>
    <td><?php echo $add4; ?></td>
  </tr>
  <tr>
    <td align="center"><input name="" type="submit"></td>
    <td><input name="id" type="hidden" value="2"></td>
    <td><input type="Reset" name="Reset" value="Reset" /></td>
  </tr>
</table>
<input type="hidden" name="MM_update" value="test">
</form>

Notice when you continually select the same option, it works perfect, the numbers add great.  When you add a few to one option then change over to another, it defaults everything BUT the new option to 0 and adds to the new option.
<br /><br />
I know i'm doing something wrong in the update &/or radio buttons.  Any help is appreciated.

<?php
mysql_free_result($questions);
?>

 

There are links posted to show my test pages, in the middle of my OP, here they are again:

 

http://palmettofx.dasslehost.com/test1.php?Q=1

& test 2

http://palmettofx.dasslehost.com/test2.php?Q=2

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.