Jump to content

Setting a form variable?


tomm098

Recommended Posts

Hey everyone. I'm having a slight problem with a Question Answer forum which is stored in a mySQL database. I have two seperate tables, one for Questions, one for Answers. Each Question has a unique ID. When someone answers a question it is assigned that Questions ID. All that is fine.. But I cannot get the answers to appear below the corresponding Questions. The insert record works fine and they have the same ID's but will the answers will not display because they are from different tables I think.

 

The answers have a recordset filter with a form variable. I think if i can set a form variable to the Question ID's they will show up.  But im not sure how to do that?

 

Here is the form

<form id="form6" name="form6" method="POST" action="<?php echo $editFormAction; ?>">
              <p><span class="question"><?php echo $row_QA['Question']; ?></span><br />
              <span class="answer"><?php echo $row_Answer['Answer']; ?></span> </p>
              <p>
                <input name="Answer" type="text" id="Answer" value="add your own answer" />
                <input name="QuestionID" type="text" id="QuestionID" value="<?php echo $row_QA['ID']; ?>"/>
                <input name="User" type="hidden" id="User" value="tomm098" />
                <input type="hidden" name="MM_insert" value="form6" />
                <input type="submit" name="Submit" id="Submit" value="Submit" />
              </p>
            </form>

 

Here is the insert record PHP

<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form6")) {
  $insertSQL = sprintf("INSERT INTO Answers (QuestionID, Answer, `User`) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['QuestionID'], "int"),
                       GetSQLValueString($_POST['Answer'], "text"),
                       GetSQLValueString($_POST['User'], "text"));

  mysql_select_db($database_test, $test);
  $Result1 = mysql_query($insertSQL, $test) or die(mysql_error());
}
?>

 

and here is the recordset for the Answers filter

$colname_Answers = "-1";
if (isset($_POST['QuestionID'])) {
  $colname_Answers = $_POST['QuestionID'];
}
mysql_select_db($database_test, $test);
$query_Answers = sprintf("SELECT Answer, `User` FROM Answers WHERE QuestionID = %s", GetSQLValueString($colname_Answers, "int"));
$Answers = mysql_query($query_Answers, $test) or die(mysql_error());
$row_Answers = mysql_fetch_assoc($Answers);
$totalRows_Answers = mysql_num_rows($Answers);
?>

 

Hopefully I have been clear enough. Thanks a lot

 

 

 

Link to comment
Share on other sites

Is this what you mean

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;
}

 

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.