Page 1 of 1

No error, but no insertion

Posted: Mon Jul 17, 2006 10:53 pm
by Pyro In A Cage
I don't get an error when running this query, but when I look in phpmyadmin, the data that should have been inserted is not there.

Code: Select all

$db = mysql_connect($db_host, $db_user, $db_pass); //Connect to the database
mysql_select_db($db_table_auths,$db); //Select the table
$query = ( 'INSERT INTO'.$db_name.' (code, user, admin) VALUES ("'.$auth_code.'", "'.$_POST['user'].'", "'.$_session['user'].'")' ); //Define the query
$result = mysql_query($sql); //Execute the query
Yes, all of the variables have the right values.

Posted: Mon Jul 17, 2006 11:02 pm
by Burrito
it looks like you need a space after 'INTO' in your sql query.

try echoing out query to make sure it is what you think it is.

also add a die after you mysql_query to ensure that it really isn't throwing an error.

ex:

Code: Select all

mysql_query($query)
  or die(mysql_error());

Posted: Mon Jul 17, 2006 11:03 pm
by Benjamin

Code: Select all

$db_name
Shouldn't that be table name? Echo $query and post it please.

Posted: Mon Jul 17, 2006 11:06 pm
by Burrito
astions wrote:Shouldn't that be table name? Echo $query and post it please.
I thought that as well then noticed this above:

Code: Select all

mysql_select_db($db_table_auths,$db); //Select the table
I think he's just using some odd (as far as nomenclature) var names.

Posted: Mon Jul 17, 2006 11:08 pm
by Benjamin
Yes, it does look like the table and database variables need to be swapped.

Well here it is cleaned up a little bit, still might need to be fixed though..

Code: Select all

$db = mysql_connect($db_host, $db_user, $db_pass); //Connect to the database
mysql_select_db($db_table_auths,$db); //Select the table

$query = ( 'INSERT INTO `' . $db_name . '` (code, user, admin) VALUES ("' . $auth_code . '", "' . mysql_real_escape_string($_POST['user']) . '", "' . mysql_real_escape_string($_session['user']) . '")' ); //Define the query
$result = mysql_query($sql) or die(mysql_error()); //Execute the query

Posted: Mon Jul 17, 2006 11:18 pm
by Pyro In A Cage
Damn you guys are fast...

I echoed query, and there was nothing there. It did give an error: "Query was empty".

Wtf?

Posted: Mon Jul 17, 2006 11:26 pm
by Benjamin

Code: Select all

mysql_query($sql)
Needs to be..

Code: Select all

mysql_query($query)

Posted: Mon Jul 17, 2006 11:31 pm
by Pyro In A Cage
Whoa, how did I not notice that.....


Thank you :)

Posted: Tue Jul 18, 2006 12:05 am
by Charles256
that's my most common mistake,don't feel bad