<?php
echo "<h1>Register</h1>";
$submit = filter_input(INPUT_POST,'submit');
$fullname = strip_tags(filter_input(INPUT_POST,'fullname'));
$username = strip_tags(filter_input(INPUT_POST,'username'));
$password = strip_tags(filter_input(INPUT_POST,'password'));
$repeatpassword = strip_tags(filter_input(INPUT_POST,'repeatpassword'));
$date = date("y-m-d");
if($submit)
{
if ($username&&$fullname&&$password&&$repeatpassword)//check for existance.
{
if ($password == $repeatpassword)
{
if(strlen($username) > 25 || strlen($fullname) > 25) // check char length of username and fullname
{
echo "Length of username or password is too long!";
}
else
{
if(strlen($password) > 25 || strlen($password) < 6) //check password length
{
echo "password must be between 6 and 25 characters long!";
}
else
{
$password = md5($password); // encrypt password
$repeatpassword = md5($repeatpassword);
//register user
echo "Success!";
//open database
$connect = mysql_connect('localhost', 'data', 'password');
mysql_select_db('mylogin');
$queryreg = mysql_query("INSERT INTO users('date','name','username','password') VALUES ('$date','$fullname','$username','$password'");
die("You have been registered! <a href ='index.php'>Return to login page!</a>");
}
}
}
else
echo "Your passwords do not match";
}
else
echo ("Please fill in <b>all</b> fileds!");
}
?>
<html>
<p>
<form action = 'register.php' method = 'POST'>
<table>
<tr>
<td>
Your full name:
</td>
<td>
<input type = 'text' name = 'fullname' value ='<?php echo $fullname; ?>' >
</td>
</tr>
<tr>
<td>
Choose a username:
</td>
<td>
<input type = 'text' name = 'username' value ='<?php echo $username; ?>'>
</td>
</tr>
<tr>
<td>
Choose a password:
</td>
<td>
<input type = 'password' name = 'password'>
</td>
</tr>
<tr>
<td>
Repeat your password:
</td>
<td>
<input type = 'password' name = 'repeatpassword'>
</td>
</tr>
</table>
<p>
<input type = 'submit' name = 'submit' value = 'Register'>
</form>
</html>
12 Replies - 545 Views - Last Post: 26 July 2012 - 12:22 PM
#1
INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 08:41 AM
I dont run into any errors, it just simply doesnt enter the data into my database.
Replies To: INSERT INTO is not inserting data into my database
#2
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 08:53 AM
Is it getting that far? So you see "Success!" and then "You have been registered!" printed out on the screen?
Change the queries so they die if there is an error, then you can see if there's a problem:
I'd also suggest using prepared data objects as opposed to those mysql_* functions, as they are safer.
Change the queries so they die if there is an error, then you can see if there's a problem:
$connect = mysql_connect('localhost', 'data', 'password') or die('cannot connect');
mysql_select_db('mylogin') or die('cannot select db');
$queryreg = mysql_query("INSERT INTO users('date','name','username','password') VALUES ('$date','$fullname','$username','$password'") or die('error: ' . mysql_error());
I'd also suggest using prepared data objects as opposed to those mysql_* functions, as they are safer.
This post has been edited by Duckington: 26 July 2012 - 08:54 AM
#3
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 09:04 AM
Some things you're missing that would make your life easier:
- Prepared statements
- Indent style
- Functions — they're your friends.
#4
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 09:14 AM
Done what you suggest Duckington still not luck. I do get the "success, registered printed out.
CTphpnwb, I'll make sure i check them links out. Thanks
CTphpnwb, I'll make sure i check them links out. Thanks
#5
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 09:35 AM
Please, do as CTphpnwb suggested and indent your code. It makes it easier for you, and everyone else, to read.
Here is your code cleaned up:
See how much easier that is to read and follow? If you plan on doing any kind of programming you should definitely be doing this.
You are seeing your success message because on line 34 you echo "Success" before you actually try to insert the data into your database. Instead do as Duckington suggested and check to see if the query is successful before returning a success message.
Here is your code cleaned up:
<?php
echo "<h1>Register</h1>";
$submit = filter_input(INPUT_POST,'submit');
$fullname = strip_tags(filter_input(INPUT_POST,'fullname'));
$username = strip_tags(filter_input(INPUT_POST,'username'));
$password = strip_tags(filter_input(INPUT_POST,'password'));
$repeatpassword = strip_tags(filter_input(INPUT_POST,'repeatpassword'));
$date = date("y-m-d");
if($submit)
{
if ($username&&$fullname&&$password&&$repeatpassword)//check for existance.
{
if ($password == $repeatpassword)
{
if(strlen($username) > 25 || strlen($fullname) > 25) // check char length of username and fullname
{
echo "Length of username or password is too long!";
}
else
{
if(strlen($password) > 25 || strlen($password) < 6) //check password length
{
echo "password must be between 6 and 25 characters long!";
}
else
{
$password = md5($password); // encrypt password
$repeatpassword = md5($repeatpassword);
//register user
echo "Success!";
//open database
$connect = mysql_connect('localhost', 'data', 'password');
mysql_select_db('mylogin');
$queryreg = mysql_query("INSERT INTO users('date','name','username','password') VALUES ('$date','$fullname','$username','$password'");
die("You have been registered! <a href ='index.php'>Return to login page!</a>");
}
}
}
else
echo "Your passwords do not match";
}
else
echo ("Please fill in <b>all</b> fileds!");
}
?>
<html>
<p>
<form action = 'register.php' method = 'POST'>
<table>
<tr>
<td>
Your full name:
</td>
<td>
<input type = 'text' name = 'fullname' value ='<?php echo $fullname; ?>' >
</td>
</tr>
<tr>
<td>
Choose a username:
</td>
<td>
<input type = 'text' name = 'username' value ='<?php echo $username; ?>'>
</td>
</tr>
<tr>
<td>
Choose a password:
</td>
<td>
<input type = 'password' name = 'password'>
</td>
</tr>
<tr>
<td>
Repeat your password:
</td>
<td>
<input type = 'password' name = 'repeatpassword'>
</td>
</tr>
</table>
<p>
<input type = 'submit' name = 'submit' value = 'Register'>
</form>
<p>
</html>
See how much easier that is to read and follow? If you plan on doing any kind of programming you should definitely be doing this.
You are seeing your success message because on line 34 you echo "Success" before you actually try to insert the data into your database. Instead do as Duckington suggested and check to see if the query is successful before returning a success message.
#6
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 09:37 AM
This is what i get after using using mysql_error());
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 ''date','name','username','password') VALUES ('12-07-26','test','test','0b4e7a0e5' at line 1
#7
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 09:46 AM
Hi,
You don't use single quotes around the field names in your INSERT statement. You can use `'s around field but it's only necessary if your field name has a space in it. So your query should be:
What can be helpful is to put the query in a separate variable so you can ouput the actual query if there is a problem. So
Speaking of which, I would highly suggest you read CTphpnwb's link to prepared statements, which is a tutorial on PDO. You should not be using the mysql* database access functions as they are deprecated. If you are just starting to learn PHP you should learn PDO so you are doing things correctly from the start
You don't use single quotes around the field names in your INSERT statement. You can use `'s around field but it's only necessary if your field name has a space in it. So your query should be:
INSERT INTO users(date,name,username,password) VALUES ('$date','$fullname','$username','$password')
What can be helpful is to put the query in a separate variable so you can ouput the actual query if there is a problem. So
$sql = "INSERT INTO users(date,name,username,password) VALUES ('$date','$fullname','$username','$password');
mysql_query($sql);
Speaking of which, I would highly suggest you read CTphpnwb's link to prepared statements, which is a tutorial on PDO. You should not be using the mysql* database access functions as they are deprecated. If you are just starting to learn PHP you should learn PDO so you are doing things correctly from the start
#8
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 09:47 AM
The fields need to be backticked, not quoted.
` not '
You will definitely need to do this as you're using a reserved word as a column name, i.e., date.
` not '
You will definitely need to do this as you're using a reserved word as a column name, i.e., date.
#9
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 09:59 AM
Right didn't think about reserved words, good catch!
#10
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 10:51 AM
Still no luck, ive done what has been suggest, but it still fails to transfer the information to the database
#11
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 11:50 AM
Show what your code looks like now.
#12
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 12:04 PM
#13
Re: INSERT INTO is not inserting data into my database
Posted 26 July 2012 - 12:22 PM
Got it working now, thank you for all the responses.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|