Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 query not working
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
unknowngiver




PostPosted: Tue May 30, 2006 10:22 am   Post subject: query not working

Hey
whats wrong with this code??
code:


<?

$username = $_GET['user'];
$pass = $_GET['pass'];
$email = $_GET['email'];
$registerpass = $_GET['registerpass'];
$dbusername="zubair";
$dbpassword="";
$database= "z_game";

if (($registerpass == "z3nixrul3s") && ($pass != '') && ($email !='') && ($username!='')) {
mysql_connect(localhost, $dbusername, $dbpassword);

@mysql_select_db($database) or die( "Unable to select database");
$query= "INSERT INTO user (id, username,email,pass) VALUES ('', '$username','$email','$pass')";
mysql_query($query)or die('Error, insert query failed');
}
?>

it gives me the error:

code:

ERROR, Insert Query Failed
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Tue May 30, 2006 10:39 am   Post subject: (No subject)

echo the query instead of executing it and post it... without seeing what your doing it's hard to diagnose.
md




PostPosted: Tue May 30, 2006 10:45 am   Post subject: (No subject)

/me grubles about a lack of edit features...

And also try running the query in say the mysql query browser and see what error it gives you.
wtd




PostPosted: Tue May 30, 2006 11:09 am   Post subject: (No subject)

code:
INSERT INTO user (id, username,email,pass) VALUES ('', '$username','$email','$pass')


If you want the DB to automatically handle the "id" column, then simply do this:

code:
INSERT INTO user (username, email, pass) VALUES ('$username', '$email', '$pass')


Another problem: see how you have variables inside single-quoted strings? Well, in PHP, single-quoted strings don't do string interpolation.
Amailer




PostPosted: Tue May 30, 2006 11:12 am   Post subject: (No subject)

Obviously that means that the id field in your database is set to auto_increasement
rdrake




PostPosted: Tue May 30, 2006 5:04 pm   Post subject: (No subject)

Strangely I found something like the following works better.
code:
INSERT INTO users VALUES ('', 'blah', 'blah');
Of course, the empty set of quotes is for the id field which should be auto incrementing.
Blade




PostPosted: Tue May 30, 2006 9:17 pm   Post subject: (No subject)

yeah, for that you need to have the right info in the same order as the table

so if you have id, username, password in that order in your table, you need to input the id, username, password in that order in your script

but the way he did it, the information doesnt have to be in the exactly same order as the table, you just need each value to corrospond the way you declared it

i dont remember for sure, so somebody correct me if i'm wrong, but when you declare which cells you're going to input data to, i dont think you need to use every cell in the table.

so if you have id, username, email, password, name, phone number but you only want to add an id, username, email and password then you can use

php:
$query= "INSERT INTO user (id, username,email,pass) VALUES ('', '" . $username . "', '" . $email . "' , '" . $pass . "')";


whereas you'd have to say
php:
$query= "INSERT INTO user VALUES ('', '" . $username . "', '" . $email . "' , '" . $pass . "', '', '')";


personally, i think they both work alright Smile
wtd




PostPosted: Tue May 30, 2006 9:37 pm   Post subject: (No subject)

Blade wrote:
i dont remember for sure, so somebody correct me if i'm wrong, but when you declare which cells you're going to input data to, i dont think you need to use every cell in the table.


Indeed, and if you have an auto-incrementing id field, you should let the database handle it.
Sponsor
Sponsor
Sponsor
sponsor
octopi




PostPosted: Tue May 30, 2006 9:44 pm   Post subject: (No subject)

You can put the variable names like the OP did.

code:
$query= "INSERT INTO user (id, username,email,pass) VALUES ('', '$username','$email','$pass')";


Hes using double qoutes not single qoutes. (the single qoutes are part of the sql string)

or if you'd rather you can do
code:
$query= "INSERT INTO user (id, username,email,pass) VALUES ('', '{$username}','{$email}','{$pass}')";


using {} ensures that the variable's name is recognised correctly
say $width=5; and you wanted to print "5px" you could do "{$width}px", whereas if you wrote "$widthpx", it would try to load a different variable

when I do inserts, I make them like my update statements like so:
code:
   
      $sql  = "insert into table_name SET ";
      $sql .= "username='$username', ";
      $sql .= "pass='$pass', ";
      $sql .= "email='$email'";
wtd




PostPosted: Tue May 30, 2006 9:47 pm   Post subject: (No subject)

octopi wrote:
You can put the variable names like the OP did.

code:
$query= "INSERT INTO user (id, username,email,pass) VALUES ('', '$username','$email','$pass')";


Hes using double qoutes not single qoutes. (the single qoutes are part of the sql string)


I knew that!

Thanks for pointing out my mistake.

Wink
Blade




PostPosted: Wed May 31, 2006 8:40 am   Post subject: (No subject)

concatenating like i did works as well Smile
JackTruong




PostPosted: Fri Jun 02, 2006 8:59 pm   Post subject: (No subject)

Maybe it could be:
code:
mysql_connect(localhost, $dbusername, $dbpassword);
localhost should be in quotes, should it not?

Take off the @ when using mysql_select_db and see if the error message changes.
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: