am doing a classroom project but am stuck with the registering users script it does send the data to the mysql database ppliz help me out thanks
here are both the html and php codes .

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head> 


<body><form action="reg1.php" method="POST">

<table width="380" height="126" border="1" align="center">

  <tr>

    <td colspan="3"><div align="center">Register to login in the database </div></td>

  </tr>

  <tr>

    <td width="153">Enter user Name </td>

    <td width="181"><input type="text" name="username">&nbsp;</td>

    <td width="24">&nbsp;</td>

  </tr>

  <tr>

    <td>Enter Password </td>

    <td><input type="password" name="pass" /></td>

    <td>&nbsp;</td>

  </tr>

  <tr>

    <td>Renter password </td>

    <td><input type="password" name="pass2" /></td>

    <td>&nbsp;</td>

  </tr>

  <tr>

    <td>&nbsp;</td>

    <td><input type="submit" value="Submit" />&nbsp;</td>

    <td>&nbsp;</td>

  </tr>

</table>

</body>

</html>
<?php 

// Connects to your Database 

mysql_connect("localhost", "root", "") or die(mysql_error()); 

mysql_select_db("kawempe_hc") 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 users 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.');

} 


// 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 users(username, password)

VALUES ('".$_POST['username']."', '".$_POST['pass']."')";

$add_member = mysql_query($insert);}

?>

Recommended Answers

All 6 Replies

I presume you mean it doesn't send to the database?

The only thing I have noticed is that the way in wich you are connecting is not the way in wich I would.

I have seen others connect this way but they were all having problems, my advice would be to stick to the w3schools method.

$con = mysql_connect( "localhost" , "root" , "" ) or die( "MySQL ERROR: " . mysql_error() );
$db = mysql_select_db( "kawempe_hc" , $con ) or die( "MySQL ERROR: " . mysql_error() );

Also as apose to addslashes I would use mysql_real_escape_string this will make sure that the database puts the data in a way that it will be able to accept it.

If the problem persist after these corrections please can you send an SQL export of your table (passwords don't need to be included) not the data just the structure.

I could not notice anything wrong in your script, so, the problem is in your databases, maybe the table you need doesn't exist yet, or you just give wrong names..
Check it out carefully!!!

There's an extra '}' at line 77. Don't know if that's the problem.

You should also make use of the mysql_error() to see what's wrong.

Add this after the insert:

if (mysql_affected_rows($conn) != 1) {
    die(mysql_error());
}

Do you get an error with this code?

if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) {
  die('You did not complete all of the required fields');
}

The single | makes me think it would cause an error because "OR" is "||".

[EDIT]
I tested it on my server and it actually worked. Weird.

commented: Good catch with the bitwise or +2

It's not that weird, actually. | is a bitwise-or, so it create a result with all the bits that are set in !$_POST and !$_POST and !$_POST. If the number this creates is 0, the if won't have any effect. If the number is anything but 0, the if will kick in. (correct me if I'm wrong). But he really should use || to avoid any nasty bugs :P

Good catch, though. Didn't see it when I looked through the code.

It's not that weird, actually. | is a bitwise-or, so it create a result with all the bits that are set in !$_POST and !$_POST and !$_POST. If the number this creates is 0, the if won't have any effect. If the number is anything but 0, the if will kick in. (correct me if I'm wrong). But he really should use || to avoid any nasty bugs :P

Good catch, though. Didn't see it when I looked through the code.

Ok, thanks for that. I didn't know about bitwise operators. Been doing this for 3 years, figured I would of saw that before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.