Jump to content

INSERT QUERY


datoshway

Recommended Posts

I need to insert a query but with the ID # from another table.  Here is the query I am using to select which works fine.

 

// get reviews
		$strQuery			= sprintf("SELECT * FROM messages WHERE intItemID = %d", 
										intval($intProductID));
		$queryGetReviews	= db_query($strQuery);

 

However my insert doesn't seem to be picking up the %d. 

//query to insert data into table
$sql = "
	INSERT INTO messages
	SET
	firstname = '$name',
	message = '$message' WHERE intItemID=%d"
	;
$result = mysql_query($sql);
if(!$result)
{
	echo "Failed to insert record";
}
else
{
	echo "Record inserted successfully";
}
}

 

Please help!

 

Link to comment
Share on other sites

I'm not very good at PHP but I am pretty sure the error is that you are not setting a variable for %d

 

$sql = sprintf("
	INSERT INTO messages
	SET
	firstname = '$name',
	message = '$message' WHERE intItemID=%d", $record)
	;

 

Should be:

 

$sql = sprintf("
	INSERT INTO messages
	SET
	firstname = '$name',
	message = '$message' WHERE intItemID=%d", $record);

Link to comment
Share on other sites

1. Do you have a REALLY good reason for using SELECT *?

2. What is %d ? it's not a number, so should be wraped in quotes regardless.  It also includes a wildcard for use with the LIKE statement which, obviously, is different from [=]. Could you tell us what %d is reffering to exactly.

Link to comment
Share on other sites

The select is not the issue, my select statement is fine and working, my insert is just not inserting the ID number.  THE %d is the ID number for the product page we are on, that is what i'm looking to store, this ID number is stored in a separate database. For some reason I can't insert that number into table messages, even though it queried it for the select.

 

 

Link to comment
Share on other sites

My database looks like this.

 

id | message | firstname | intItemID

 

I'm displaying the messages that intItemID = %d which is the ID of a item number from a items table.  So it's displaying properly,  however there is a form to insert while on this page which is working fine but not inserting the ID number (intItemID) for the product page we are on. 

 

Link to comment
Share on other sites

Here is everything i'm dealing with .

if(!empty($_POST) && isset($_POST))
{
//make variables safe to insert
  $name = mysql_real_escape_string($_POST['name']);
  $intItemID = mysql_real_escape_string($_POST['intItemID']);
  $message = mysql_real_escape_string($_POST['message']);

//query to insert data into table
$sql = "
	INSERT INTO messages
	SET
	firstname = '$name',
	intItemID = '%d',
	message = '$message'"
	;
$result = mysql_query($sql);
if(!$result)
{
	echo "Failed to insert record";
}
else
{
	echo "Record inserted successfully";
}
}
?>

<?php
if (db_num_rows($queryGetReviews) > 0) {
while ($objRow= db_fetch_object($queryGetReviews)) { //open while loop
                        $message = strip_tags(stripslashes($objRow->message));
                        $firstname = stripslashes($objRow->firstname);
   ?>
   <br />
  <strong style="font-size:18px; color:#42919a;"><?php  echo $firstname;?><br /></strong>
<strong style="font-size:16px; color:#ccc;"><?php  echo $message;?><br /></strong><br />
            <img src="../../line_horz2.png" width="766" height="2" /><br />
<?php 
} //close while loop

} else { //close IF and add else statement
//throw error message
echo "error has occured or no rows returned<br/>".mysql_error();
}?>

 

And my controller

<?php

session_start();

db_connect();
db_select();
processLogin();

$intProductID		= intval(getVariable("id"));

if (is_numeric($intProductID)) {
	$strQuery				= sprintf(	"SELECT strName, txtDescription, txtComments, dblPrice, dblVolume, intMOQ, intManufacturerID, intTimesPurchased, boolCustom, strThumbFilename, txtKeywords, boolActive, dtEntered FROM tblItems WHERE intID = %d", 
										intval($intProductID));
	$queryGetProduct		= db_query($strQuery);

	if (db_num_rows($queryGetProduct) > 0) {
		$arrParameters	= array();
		$arrRow			= db_fetch_assoc($queryGetProduct);
		$arrRow			= importVariables($arrRow);

		extract($arrRow);

		db_free_result($queryGetProduct);




		// get reviews
		$strQuery			= sprintf("SELECT * FROM messages WHERE intItemID = %d", 
										intval($intProductID));
		$queryGetReviews	= db_query($strQuery);


	} else {
		header("Location: " . STR_RELATIVE_PATH);
	}
}

?>

 

 

 

Link to comment
Share on other sites

The %d syntax is a sprintf() format specifier. It gets replaced with the correct parameter from the sprintf() function call, which you are not using.

 

Since you aren't using sprintf() to build your query string, why not just put the $intItemID variable into the $sql = ""; statement, the same as you are doing with the $name and $message variables?

 

 

Link to comment
Share on other sites

I tried that but it still records it as NULL.

 

//make variables safe to insert
  $name = mysql_real_escape_string($_POST['name']);
  $intItemID = mysql_real_escape_string($_POST['intItemID']);
  $message = mysql_real_escape_string($_POST['message']);

//query to insert data into table
$sql = "
	INSERT INTO messages
	SET
	firstname = '$name',
	intItemID = '$intItemID',
	message = '$message'"
	;
$result = mysql_query($sql);
if(!$result)
{
	echo "Failed to insert record";
}
else
{
	echo "Record inserted successfully";
}
}
?>

 

Anyone want to make some quick money?  PM me.

Link to comment
Share on other sites

Nothing you have posted shows the form that is submitting the data, are you sure $_POST['intItemID'] contains what you expect?

 

Your current code would be inserting a value if there is one. From my signature -

Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.
Link to comment
Share on other sites

Still not recording intItemID... This is now what we have..

if(!empty($_POST) && isset($_POST))
{
//make variables safe to insert
  $name = mysql_real_escape_string($_POST['name']);
  $intItemID = mysql_real_escape_string($_POST['intItemID']);
  $message = mysql_real_escape_string($_POST['message']);

//query to insert data into table
$sql = "INSERT INTO 
messages (firstname, intItemID, message) 
VALUES ('$name', '%d', '$message')";
$result = mysql_query($sql);
if(!$result)
{
	echo "Failed to insert record";
}
else
{
	echo "Record inserted successfully";
}
}

Link to comment
Share on other sites

Let's see exactly what the variable holds before going too much further. Add the indicated line and see what it returns.

if(!empty($_POST) && isset($_POST))
{
//make variables safe to insert
  $name = mysql_real_escape_string($_POST['name']);
  $intItemID = mysql_real_escape_string($_POST['intItemID']);
  $message = mysql_real_escape_string($_POST['message']);

echo "<br>POST value: {$_POST['intItemID']}<br>\$intItemID value: $intItemID<br>";  // <---- Add this line

//query to insert data into table
$sql = "INSERT INTO. . .

 

EDIT: closed BBCode tag properly . . .

Link to comment
Share on other sites

Here is the form

  <form id="myForm" action="data.php" method="post">
    <fieldset>
      <div>
        <label>Name*</label>
        <input type="input" name="name" class="reviews"/>
      </div>
      <div>
        <label>Review*</label>
        <textarea rows="15" cols="15" name="message" class="reviews"></textarea>
      </div>
      <div>
        <input type="submit" value="Submit" />
      </div>
    </fieldset>
  </form>

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.