Forum Moderators: coopster

Message Too Old, No Replies

Sending form elements to SQL

         

toggo

3:04 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



I'm trying to insert the values from a simple form to a sql table but keep returning the error "Failed query of..."

Can anyone shed any light on this? I've only just started with php and sql and it's got me stumped.

$dbuser="root"; //database username
$dbpass=""; //password for the database
$dbname="asset"; //the name of the database
$chandle = mysql_connect("localhost", $dbuser, $dbpass) or die("Connection Failure to Database");
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found. " . $dbuser);
$mainsection="asset_register"; //The name of the table where web links are stored
$query1="insert into " . $mainsection . " (unitid,make,model,location,user,indreplace,estrelace,purchasedate,oskey,machinekey,officekey,notes) values (\"" . $unitid . "\",\"" . $make . "\",\"" . $model . "\",\"" . $location . "\",\"" . $user . "\",\"" . $indreplace . "\",\"" . $estreplace . "\"," . $purchasedate . "\"," . $oskey . "\"," . $machinekey . "\"," . $officekey . "\"," . $notes .")";
mysql_db_query($dbname, $query1) or die("Failed Query of " . $query1);

phparion

3:15 pm on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



this query is not reabable easily please remove those thousands double quotes and make our life easy to read that what you want to do exactly,,,

some tips:

execute your this query on command line and see whether it works or not then use in php page..

second and very handy tip is that DISPLAY YOUR QUERY on page and see that how does your query look like, because often we make some small mistake in php syntax that turns into an illegal query for mysql,

third tip,
make sure that u use quote for string values to be sent to table and no quotes for integer values..

if still having problems then please write the query without those double quotes so that i can understand and answer you..

toggo

3:29 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Sorry about that, I have now simplified the query for better reading and removed the double quotes, I'm not sure how I execute sql at the command line however I'm looking into it now.

$dbuser="root"; //database username
$dbpass=""; //password for the database
$dbname="asset"; //the name of the database
$chandle = mysql_connect("localhost", $dbuser, $dbpass) or die("Connection Failure to Database");
mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found. " . $dbuser);
$mainsection="asset_register"; //The name of the table where web links are stored
$query1="insert into " . $mainsection . " (unitid,make,model,location) values ('$unitid','$make','$model','$location')";
mysql_db_query($dbname, $query1) or die("Failed Query of " . $query1);

sonjay

11:12 pm on Nov 21, 2005 (gmt 0)

10+ Year Member



Where are the values for $unitid, $make, $model and $location coming from? Have you verified that you do indeed have values assigned for those variables?

What do you get if you do this right before executing the query:
echo $query1;

Do you have error reporting turned on?

Are there any non-null required values in asset_register for which you're not specifying a value?

Do the datatypes of your variables match the required datatypes in your db?

Anyango

6:04 am on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, Listen.

This is not a good way to debug an SQL statement

mysql_db_query($dbname, $query1) or die("Failed Query of " . $query1);

you should use

mysql_query($query1) or die("Error: ".mysql_error()."<br>SQL: ".$query1);

then look for the error mysql returns

toggo

10:43 am on Nov 22, 2005 (gmt 0)

10+ Year Member



Thanks for your help guys I managed to sort it when I cleaned the code up somewhat. I was over complicating things and wasn't use the mysql_error(). Turns out it was down to a simple spelling mistake. I should have been more thorough with my data checking.

:-)

sonjay

1:31 pm on Nov 22, 2005 (gmt 0)

10+ Year Member



Dern, it felt like there was something wrong with that "or die()" part, but I missed that mysql_error() was missing!