Data not being added to database

Registration

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1720 Views - Last Post: 29 August 2008 - 07:48 PM Rate Topic: -----

#1 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Data not being added to database

Posted 29 August 2008 - 09:14 AM

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

<?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 August 2008 - 10:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Data not being added to database

#2 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Data not being added to database

Posted 29 August 2008 - 10:50 AM

Your problem's coming from this block

$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

$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:

$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.
Was This Post Helpful? 0
  • +
  • -

#3 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Data not being added to database

Posted 29 August 2008 - 10:58 AM

..... 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
Was This Post Helpful? 0
  • +
  • -

#4 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Data not being added to database

Posted 29 August 2008 - 11:08 AM

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:
// 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?
Was This Post Helpful? 0
  • +
  • -

#5 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Data not being added to database

Posted 29 August 2008 - 11:13 AM

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
Was This Post Helpful? 0
  • +
  • -

#6 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Data not being added to database

Posted 29 August 2008 - 11:24 AM

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

<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>


Was This Post Helpful? 0
  • +
  • -

#7 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Data not being added to database

Posted 29 August 2008 - 11:35 AM

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.
Was This Post Helpful? 0
  • +
  • -

#8 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Data not being added to database

Posted 29 August 2008 - 11:48 AM

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
Was This Post Helpful? 0
  • +
  • -

#9 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Data not being added to database

Posted 29 August 2008 - 11:52 AM

Try the opposite of what I suggested

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



Change that to

$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
Was This Post Helpful? 0
  • +
  • -

#10 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Data not being added to database

Posted 29 August 2008 - 12:02 PM

View Postakozlik, on 29 Aug, 2008 - 11:52 AM, said:

Try the opposite of what I suggested

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



Change that to

$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
Was This Post Helpful? 0
  • +
  • -

#11 Decypher  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 400
  • Joined: 28-June 08

Re: Data not being added to database

Posted 29 August 2008 - 12:42 PM

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...
Was This Post Helpful? 0
  • +
  • -

#12 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: Data not being added to database

Posted 29 August 2008 - 12:52 PM

add "or die(mysql_error());" to the end of the query...
Was This Post Helpful? 0
  • +
  • -

#13 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Data not being added to database

Posted 29 August 2008 - 12:54 PM

View PostDecypher, on 29 Aug, 2008 - 03:42 PM, said:

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...



Instead of using $_SERVER['PHP_SELF'], which you shouldn't be doing anyway, just put in the file name and then try it.
Was This Post Helpful? 0
  • +
  • -

#14 Mcbazzo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 29-August 08

Re: Data not being added to database

Posted 29 August 2008 - 12:57 PM

have yous got msn?
Was This Post Helpful? 0
  • +
  • -

#15 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Data not being added to database

Posted 29 August 2008 - 01:00 PM

No I sure don't.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2