Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INSERT statement will not work 1

Status
Not open for further replies.

34534534534555

IS-IT--Management
Joined
Jan 8, 2003
Messages
240
Location
GB
Hi

Could someone please shed some light as to why this INSERT statement will not work.
Code:
<?php
	
	session_start();
	
	$isbn = $HTTP_POST_VARS['booktitle'];
	$startprice = $HTTP_POST_VARS['startprice'];
	$buyitnowprice = $HTTP_POST_VARS['buyitnowprice'];
	$bookcondition = $HTTP_POST_VARS['bookcondition'];
	$additionalcomments = $HTTP_POST_VARS['additionalcomments'];
	$username = $_SESSION['name'];
	
	print ($_SESSION['name']);
	print (&quot;<br>$isbn<br>&quot;);
	print (&quot;$startprice<br>&quot;);
	print (&quot;$buyitnowprice<br>&quot;);
	print (&quot;$bookcondition<br>&quot;);
	print (&quot;$additionalcomments<br>&quot;);
	
	mysql_connect(&quot;localhost&quot;, &quot;name&quot;, &quot;pass&quot;) or die (&quot;Could not connect to MySQL Server&quot;);
		
	$db = &quot;db&quot;;
		
	mysql_select_db(&quot;$db&quot;) or die(&quot;Could not open $db: &quot;.mysql_error() );
	
	$sql = &quot;INSERT INTO auction ('sellerusername', 'startprice', 'isbn', 'visible', 'status', 'bookcondition', 'buyitnow')
		VALUES ('$username', '$startprice', '$isbn', 'true', 'open', '$bookcondition', '$buyitnowprice')&quot;;
		

?>

The column names are only a selection (unordered) of those in this table. I also have a column name : auctionid which is the primary key and is set to auto increment, does it or do i need to refer to it in the INSERT?

Help is very much appreciated.


| Feedback is always appreciated as this will help to further our knowledge as well |
 
&quot;INSERT INTO auction (sellerusername, startprice, isbn, visible, status, bookcondition, buyitnow)
VALUES ('$username', '$startprice', '$isbn', 'true', 'open', '$bookcondition', '$buyitnowprice')&quot;;


' is necessary only for the values and not the field names...

Known is handfull, Unknown is worldfull
 
By 'will not work' i mean does not insert a record -at all. Even if i do use mysql_query().

vbkris, even if i do as you suggest it will still not work.

I know that it could work because dreamweaver will do it, but i wanted to do it manually because 1, DW inserts crazy code and 2. i want to envoke transactions and i don't think the DW code will work if i start modifying it.


| Feedback is always appreciated as this will help to further our knowledge as well |
 
I have modified my code to hopefully return an error if there were one:

Code:
<?php
	
	session_start();
	
	$isbn = $HTTP_POST_VARS['booktitle'];
	$startprice = $HTTP_POST_VARS['startprice'];
	$buyitnowprice = $HTTP_POST_VARS['buyitnowprice'];
	$bookcondition = $HTTP_POST_VARS['bookcondition'];
	$additionalcomments = $HTTP_POST_VARS['additionalcomments'];
	$username = $_SESSION['name'];
	
	print ($_SESSION['name']);
	print (&quot;<br>$isbn<br>&quot;);
	print (&quot;$startprice<br>&quot;);
	print (&quot;$buyitnowprice<br>&quot;);
	print (&quot;$bookcondition<br>&quot;);
	print (&quot;$additionalcomments<br>&quot;);
	
	mysql_connect(&quot;localhost&quot;, &quot;usr&quot;, &quot;pass&quot;) or die (&quot;Could not connect to MySQL Server&quot;);
		
	$db = &quot;db&quot;;
		
	mysql_select_db(&quot;$db&quot;) or die(&quot;Could not open $db: &quot;.mysql_error() );
	
	$sql = mysql_query(&quot;INSERT INTO auction (sellerusername, startprice, isbn, visible, status, bookcondition, buyitnow)
		VALUES ('$username', '$startprice', '$isbn', 'true', 'open', '$bookcondition', '$buyitnowprice')&quot; or die(&quot;Invalid query: &quot; .mysql_error()));
		
	
?>

No error codes are reported at all. I just get a printout of the variables (so i can see they are being passed correctly). The table in the database is not affected at all.


| Feedback is always appreciated as this will help to further our knowledge as well |
 
That's because your code is messed up.

Since you haven't closed your parentheses correctly in your invokation of mysql_query(), PHP is assuming that the &quot;or die...&quot; part of the statement is actually part of the query.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
This is the reason why I always recommend storing the query in a variable and then passing mysql_query() that variable.

Try this:
Code:
$query =
&quot;INSERT INTO auction
(
	sellerusername, startprice, isbn, visible, status, bookcondition, buyitnow
)
VALUES
(
	'$username', '$startprice', '$isbn', 'true', 'open', '$bookcondition', '$buyitnowprice'
)&quot;;

$sql = mysql_query($query) or die(&quot;Invalid query: &quot; . mysql_error());

Want the best answers? Ask the best questions: TANSTAAFL!!
 
sleipnir214

Your way of storing the query in a variable makes the code much clearer and easier to read. I will do this from now on.

Thank you very much. It is now working -Great!

Have a *star* on me.


| Feedback is always appreciated as this will help to further our knowledge as well |
 
I've found, too, that when debugging a problem with a query, you can insert a &quot;print $query;&quot; statement where you need it to examine the query.

If you have any single script that performs a lot of queries, remember that you can reuse that single variable $query.

Once mysql_query() is invoked, passing it $query, your script doesn't need the actual query string any more. You're free to put a new query in the variable.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top