Skip to main content
Known Participant
June 21, 2009
Answered

Adding values to insert query [was: Plz help with this code]

  • June 21, 2009
  • 10 replies
  • 1221 views

I have created a comments section in which there is only one field comment here is the code of the form:

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

    <h3>

      <label for="namegd"></label>Comment:</h3>

    <p>

      <label for="comment2"></label>

      <textarea name="comment" id="comment2" cols="60" rows="10" ></textarea>

    </p>

    <p>

      <label for="submit">

      </label>

      <input type="submit" name="submit" id="submit" value="Submit" />

      <label for="reset"></label>

      <input type="reset" name="reset" id="reset" value="Clear" />

    </p>

<input type="hidden" name="MM_insert" value="frmComment" />

</form>

and the insert into code applied to the form is this

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmComment")) {

  $insertSQL = sprintf("INSERT INTO comments (`comment`, ) VALUES (%s)",

                       GetSQLValueString($_POST['comment'], "text"));


  mysql_select_db($database_my_connection, $my_connection);

  $Result1 = mysql_query($insertSQL, $my_connection) or die(mysql_error());

}

But I want the form to insert two more values to the database one will be the commented_by and the other will be post_id where commented_by = ($_SESSION['Username']) and post_id = $row_Recordset1['id'] can some one plz let me know what will be the modified code and ya commented_by is a text field and post_id is an int field.
Plz guys help me Thanks in advance
This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer David_Powers

Adding the extra values to the insert query is easy:

$insertSQL = sprintf("INSERT INTO comments (`comment`, commented_by, post_id) VALUES (%s, %s, %s)",
         GetSQLValueString($_POST['comment'], "text"),
         GetSQLValueString($_SESSION['Username'], "text"),
         GetSQLValueString($row_recordset1['id'], "int"));

You need to make sure that the code for recordset1 comes before the insert query. By default, Dreamweaver places recordset code immediately above the DOCTYPE declaration, and below other server behaviors. So, you need to move the code. Otherwise, this insert query won't work.

10 replies

David_Powers
David_PowersCorrect answer
Inspiring
June 21, 2009

Adding the extra values to the insert query is easy:

$insertSQL = sprintf("INSERT INTO comments (`comment`, commented_by, post_id) VALUES (%s, %s, %s)",
         GetSQLValueString($_POST['comment'], "text"),
         GetSQLValueString($_SESSION['Username'], "text"),
         GetSQLValueString($row_recordset1['id'], "int"));

You need to make sure that the code for recordset1 comes before the insert query. By default, Dreamweaver places recordset code immediately above the DOCTYPE declaration, and below other server behaviors. So, you need to move the code. Otherwise, this insert query won't work.

adi3199Author
Known Participant
June 21, 2009

Hey it isn't working the problem is that when ever is use the above code and preview it in the browser I just get a blank page but the address in the address bar is correct

David_Powers
Inspiring
June 21, 2009

If you're getting a blank page, it means there's an error somewhere in the PHP code, but you have display_errors turned off. Change your php.ini to display errors, and restart Apache. Then you should see an error message that helps identify where the problem is.

David_Powers
Inspiring
June 21, 2009

Posting three separate threads about the same issue is a sure way to make yourself unpopular in any web forum, particularly when you start using garish colours, bold font, and unhelpful subject lines. I have locked your duplicate threads.

adi3199Author
Known Participant
June 21, 2009

Sorry for that but I was not sure how to put the whole situation in front of the reader so I tried to explain in different ways in all the different threads and by the way can u help me with this plz sir