Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,175 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,197 people online right now. Registration is fast and FREE... Join Now!




Data not being added to database

2 Pages V  1 2 >  
Reply to this topicStart new topic

Data not being added to database, Registration

Decypher
29 Aug, 2008 - 08:14 AM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

OK it all works apart from the fact the data doesn't enter the database...any ideas?

CODE
<?php
// Connects to your Database
mysql_connect("localhost", "root", "optiona1");
mysql_select_db("operation fortitude") or die(mysql_error());

//This code runs if the form has been submitted
if (isset($_POST['submit'])) {

//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM user WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.<a href="Registration.php">Please try again</a>');
}

// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}

// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}

// now we insert it into the database
$insert = "INSERT INTO user (Username, Password)
VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$add_member = mysql_query($insert);
}
else
{
?>

<center><br><br><br><br>
<form action="<?php echo $_SERVER['registrationcomplete.php']; ?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
</form></center>

<?php
}
?>

</body>
</html>


This post has been edited by Decypher: 29 Aug, 2008 - 09:29 AM
User is offlineProfile CardPM
+Quote Post

akozlik
RE: Data Not Being Added To Database
29 Aug, 2008 - 09:50 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 714



Thanked: 31 times
Dream Kudos: 800
My Contributions
Your problem's coming from this block

php

$insert = "INSERT INTO user (Username, Password) VALUES '".$_POST['username']."', '".$_POST['pass']."')";
$add_member = mysql_query($insert);


You're telling it to put the data into columns 'Username' and 'Password'. However, from the previous query

php

$check = mysql_query("SELECT username FROM user WHERE username = '$usercheck'")


You are using the column name 'username'. If your column names are all lowercase, as they should be, then you need to change your column names in your insert query to use lowercase letters.

Example:

php

$insert = "INSERT INTO user (username, password) VALUES '".$_POST['username']."', '".$_POST['pass']."')";


That should fix your problem, assuming the column names and everything is correct.

Notes

A few notes:

1. Do not put $_POST variables directly into your queries. I know you used addslashes() but you should store your $_POST variables in another variable, and run something like mysql_real_escape_string().

2. Make sure you name your variables things that make sense. Variables like $check and $check2 do not help us at all. For instance, $check2 should be named $numRows or something to that effect, since it holds the number of rows returned from the database. This will make your code clearer for others reading it, like ourselves.

Just some friendly suggestions. Hope this helps.
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Data Not Being Added To Database
29 Aug, 2008 - 09:58 AM
Post #3

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
..... is your password to the DB optional? o.0 if not, that's why...

as for the queries, you NEED to use mysql_real_escape_string, otherwise certain characters can end your queries prematurely
User is offlineProfile CardPM
+Quote Post

Decypher
RE: Data Not Being Added To Database
29 Aug, 2008 - 10:08 AM
Post #4

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

Now I've listened to what you said and sorted out that problem, yet the data still isn't being added.

I feel it has something to do with this:
CODE
// now we insert it into the database
$insert = "INSERT INTO user (Username, Password)
VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
$add_member = mysql_query($insert);
}


Is there any reason why I should have '$add_member'?
also the '(Username, Password)' are correct as they match with my DB and the POSTtags match with the formtags

any ideas what could be wrong?

this may sound noobish, but what are:
mysql_real_escape_string?
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Data Not Being Added To Database
29 Aug, 2008 - 10:13 AM
Post #5

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
no, you can't just add a query into a variable... get rid of the variable, but keep what the variable equals...

also, Queries (for clarity), should be on one line
User is offlineProfile CardPM
+Quote Post

Decypher
RE: Data Not Being Added To Database
29 Aug, 2008 - 10:24 AM
Post #6

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

Still unable to add stuff, cheers anyways.
This is what the code looks like now thou:

CODE
<html>
<head>
<title>Registration</title>
</head>
<body><?php
// Connects to your Database
mysql_connect("localhost", "root", "****");
mysql_select_db("operation fortitude") or die(mysql_error());

//This code runs if the form has been submitted
if (isset($_POST['submit'])) {

//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT Username FROM user WHERE username = '$usercheck'")
or die(mysql_error());
$numrow = mysql_num_rows($check);

//if the name exists it gives an error
if ($numrow != 0) {
die('Sorry, the username '.$_POST['username'].' is already in use.<a href="Registration.php">Please try again</a>');
}

// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}

// here we encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}

// now we insert it into the database
$insert = "INSERT INTO user (Username, Password) VALUES ('".$_POST['username']."', '".$_POST['pass']."')";
mysql_query($insert);
}
else
{
?>

<center><br><br><br><br>
<form action="<?php echo $_SERVER['registrationcomplete.php']; ?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Confirm Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table>
</form></center>

<?php
}
?>

</body>
</html>
</body>
</html>

User is offlineProfile CardPM
+Quote Post

akozlik
RE: Data Not Being Added To Database
29 Aug, 2008 - 10:35 AM
Post #7

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 714



Thanked: 31 times
Dream Kudos: 800
My Contributions
What part are you having trouble with, the SELECT or the INSERT?

Can you give us a copy of your DB schema.

Are you sure the database columns are starting with a capital letter? That should all be working.
User is offlineProfile CardPM
+Quote Post

Decypher
RE: Data Not Being Added To Database
29 Aug, 2008 - 10:48 AM
Post #8

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

Well this is my database

Field Type Collation Attributes Null Default
Username varchar(10) latin1_swedish_ci Yes NULL
Password varchar(32) latin1_swedish_ci Yes NULL


The problem I'm having is the fact, that when i enter the username and password and click submit...it all seems like it's worked but the data doesn't actually go into the database
User is offlineProfile CardPM
+Quote Post

akozlik
RE: Data Not Being Added To Database
29 Aug, 2008 - 10:52 AM
Post #9

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 714



Thanked: 31 times
Dream Kudos: 800
My Contributions
Try the opposite of what I suggested

php

$check = mysql_query("SELECT username FROM user WHERE username = '$usercheck'")


Change that to

php

$check = mysql_query("SELECT Username FROM user WHERE Username = '$usercheck'")


You checked in phpMyAdmin and none of the usernames were going in?

Also, is the form being posted to the correct file for processing?

registrationcomplete.php

User is offlineProfile CardPM
+Quote Post

Decypher
RE: Data Not Being Added To Database
29 Aug, 2008 - 11:02 AM
Post #10

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

QUOTE(akozlik @ 29 Aug, 2008 - 11:52 AM) *

Try the opposite of what I suggested

php

$check = mysql_query("SELECT username FROM user WHERE username = '$usercheck'")


Change that to

php

$check = mysql_query("SELECT Username FROM user WHERE Username = '$usercheck'")


You checked in phpMyAdmin and none of the usernames were going in?

Also, is the form being posted to the correct file for processing?

registrationcomplete.php



It's possibly the bottom being about the correct file for processing...will have a look at it now

User is offlineProfile CardPM
+Quote Post

Decypher
RE: Data Not Being Added To Database
29 Aug, 2008 - 11:42 AM
Post #11

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 44

Ok the problem I think has to do with:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

but I'm unsure what is wrong...
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Data Not Being Added To Database
29 Aug, 2008 - 11:52 AM
Post #12

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
add "or die(mysql_error());" to the end of the query...
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:35AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month