TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   MySQL & Databases (http://www.talkphp.com/mysql-databases/)
-   -   MySQL INSERT INTO (http://www.talkphp.com/mysql-databases/2953-mysql-insert-into.html)

Bravo81 06-12-2008 10:07 PM

MySQL INSERT INTO
 
Hi all,

First of im new anf it took me ages to fine the dam register page!

Ok my problem, I am trying to do a registration page.

All is going well untill it goes to put the data into the Database.

Here my code:

Code:

mysql_query("INSERT INTO `users` (username, password, email, activecode, activated, tut, status)
VALUES ($reg_username, $randompassword, $email, $code, 0, 0, Alive)") or die ('Error Registering');

It isnt working, it dies.

The fields I am inserting into arent the only fields in the Table.
Will this stop it? Just inserting into the defined fields and leaving the rest blank would be great letting them default.

If not I will do it the hard way.

Thanks for any posts, help and advice.

Dean.

delayedinsanity 06-12-2008 10:28 PM

Have you checked mysql_error() to see what it says?

PHP Code:

if (mysql_affected_rows() != 1) die ('Query failed: '.mysql_error()); 


Bravo81 06-13-2008 07:28 AM

Thanks for the reply DI.

I put that line in and got this error:

Quote:

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@hotmail.co.uk, 5jagr8ap, 0, 0, Alive)' at line 2
I take it thats the email address thats messing up?

Will that be because I am only putting data into a few fields of the Table, not the whole of the table?

I still dont understand that, I will have another look in 10 mintues.

Thanks,

Dean.

delayedinsanity 06-13-2008 02:55 PM

It could be as simple as putting quotes around your values. Try;

PHP Code:

$q "INSERT INTO `users` (username, password, email, activecode, activated, tut, status) ".
     
"VALUES ('$reg_username', '$randompassword', '$email', '$code', 0, 0, 'Alive')";

mysql_query($q); 

-m

xenon 06-13-2008 05:03 PM

You get that error because you're sending strings without quotes to the database. That works only if the destination field is of type number (any - double, int, what ever) and the value you're trying to insert/change is a number, aswell.

Bravo81 06-13-2008 05:16 PM

Ok, now I get:

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''username', 'password', 'activated', 'online', 'email', 'status', 'tut', 'active' at line 1

Im confused.

The code is:
Code:

mysql_query("INSERT INTO `users` ('username', 'password', 'activated', 'online', 'email', 'status', 'tut', 'activecode')
VALUES ('$reg_username', '$randompassword', '0', '0', '$email', 'Alive', '0', '$code')");
if (mysql_affected_rows() != 1) die ('Query failed: '.mysql_error());

I have removed all other columns from the table.

The code is inserting into the only columns in the table in order.

Thanks for the responses,

Dean.

delayedinsanity 06-13-2008 05:34 PM

Try removing the quotes from your column names, but keep them around your values, and see if that does the trick.
-m

Kalle 06-19-2008 10:47 PM

Or try use the ` quotes insted of single quote signs


All times are GMT. The time now is 10:51 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0