FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


SQL doesn't add data [SOLVED]





SeriousBug
Well, I'm learning PHP and I wanted to make a registration form. To do this, I made the following code. It works without errors, however when I check the database with phpMyAdmin, the table is empty. Can anyone help me with the code?

The index page sends data with post method. Post method is working.

Code:

<html>
<head>
</head>
<body>
<?php
//Get data sent
$username=$_POST["username"];
$password=$_POST["password"];
//Check to see if empty data sent
if (strlen($username)<=3)
{//Username is too short
die ("<p>Your Username is too short! It must be longer than 3 characters.</p><a href='index.html'>Click here to return to registeration page.</a>");
}
elseif (strlen($password)<=3)
{//Password is too short
die ("<p>Your Password is too short! It must be longer than 3 characters.</p><a href='index.html'>Click here to return to registeration page.</a>");
}
else
{//POST is fine
//connect to database
$con = mysql_connect("localhost","seriousb","mypassword");
//check connection
if (!$con)
{//connection error
die (mysql_error());
}
//connection succesful
//select database
mysql_select_db("seriousb_users");
//add values
mysql_query("INSERT INTO users (username, password, registerdate) VALUES ($username, $password, CURRENT_TIMESTAMP)");
//close connection
mysql_close($con);
//report succesful register
echo ("<h1>Register Succesful!</h1><br /><p>Welcome " . $username . "!</p><a href='index.html'>You can now return to homepage by clicking here.</a>");
}
echo (mysql_error());
?>
</body>
</html>
Peterssidan
Check the return value of mysql_query. If it is FALSE something failed. You can use mysql_error() to get the error message as a string.
SeriousBug
Thanks for suggestion. I found out I had to pass variables as strings. Changing the line from
Code:
mysql_query("INSERT INTO users (username, password, registerdate) VALUES ($username, $password, CURRENT_TIMESTAMP)");
to
Code:
mysql_query("INSERT INTO users (username, password, registerdate) VALUES ('$username', '$password', CURRENT_TIMESTAMP)");
solved the problem.
Fire Boar
Just out of curiosity, what is the result when you enter the following in the password field?

Code:
', CURRENT_TIMESTAMP); DROP TABLE users; --


or possibly

Code:
', CURRENT_TIMESTAMP); UPDATE users SET password=''; --
Related topics
SQL Tutorial
SQL Disk usage?
MySQL DB Backup script - minor problem
An Example to mySQL class
[PHP] phpbb registration - add to other db
Trying to move SQL database
Adding field to message data
MySQL problem. Please HELP!
Changing internal data with scripting.
Running PHP Script without PHP Server?
cannot add data to mysql from PHP form
ORDER BY (SOLVED! Please close!)
I want PHP Code to ......
Database security
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.