Jump to content

Help my registration code stops processing for no reason!!


Recommended Posts

<?php
session_start();
include("dbconnect.php");
$username = $_POST['UserName'];
if (!$username) {
echo "You Must enter a username<br>";
$problem = "error";
}
$result = mysql_query("SELECT * from registration WHERE username='$username'");
$row = mysql_fetch_array( $result ) or die(mysql_error());
$dbusername = $row['username'];
$numrows = $row['numrows'];
if ($numrows>0) {
echo "Sorry that username is not avaialbe, please go back and try again.<br>";
$problem = "error";
}
$password = $_POST['UserPassword'];
if (!$password) {
echo "You Must enter a password<br>";
$problem = "error";
}
$dob = $_POST['UserDob'];
if (!$dob) {
echo "You Must enter Your date of birth<br>";
$problem = "error";
}
$email = $_POST['UserEmail'];
$mobile = $_POST['UserMobile'];
if (!$mobile) {
echo "You Must enter your mobile number<br>";
$problem = "error";
}
if ($problem=="error") {
echo "Your registration was not sucessful<br>";
} else {
mysql_query("INSERT INTO registration (username, password, age, msid, email) VALUES ('$username', '$password', '$dob', '$mobile',  '$email')");
echo "<p>Registration sucessful, welcome $username!";
}
?>

 

When I run the above code and set the value of $username to nothing - the code prints "You Must enter a username"

 

              When I assign text to $username and the other variables - the page prints nothing...

 

Can anyone see whats wrong???

Try changing the last mysql_query to:

 

mysql_query("INSERT INTO registration (username, password, age, msid, email) VALUES ('$username', '$password', '$dob', '$mobile',  '$email')") or die(mysql_error());

 

I.e. Added or die(mysql_error());

 

So we can make sure the sql is valid and not killing the script

Added or die(mysql_error());

 

it still displays nothing  ???

 

the form code is below

 

 

<form id="Form1" name="Form1" method="post" action="processregistration.php">
<font color="#400040" face="Arial">Select a User Name</font><br>

<input name="UserName" maxlength="30"/><br>
<font color="#400040" face="Arial">Select your own secret Password</font><br>
<input name="UserPassword" maxlength="30" type="password"/><br>
<font color="#400040" face="Arial">Age</font><br>
<input name="UserDob" maxlength="2"/><br>
<font color="#400040" face="Arial">Email Address</font><br>

<input name="UserEmail" maxlength="50"/><br>
<font color="#400040" face="Arial">Mobile Number (International Format)</font><br>
<input name="UserMobile" maxlength="19"/><br>
<font color="#400040" face="Arial">eg: UK = 447123456789</font><br>
<br>
<input name="Submit" type="submit" value="Submit"/><br>
<br>

close your form?

 

</form>

 

also would like to see or die(mysql_error()) after this:

 

$result = mysql_query("SELECT * from registration WHERE username='$username'");

 

also, define your form field types, e.g., <input type='text' name="UserName" maxlength="30"/>

Have added the or die - still no change

 

I changed the code for the form to below

 

<input type='text' name="UserName" maxlength="30"/><br>
<font color="#400040" face="Arial">Select your own secret Password</font><br>
<input type='password' name="UserPassword" maxlength="30"/><br>
<font color="#400040" face="Arial">Age</font><br>
<input type='text' name="UserDob" maxlength="2"/><br>
<font color="#400040" face="Arial">Email Address</font><br>
<input type='text' name="UserEmail" maxlength="30"/><br>
<font color="#400040" face="Arial">Mobile Number (International Format)</font><br>
<input type='text' name="UserMobile" maxlength="19"/><br>
<font color="#400040" face="Arial">eg: UK = 447123456789</font><br>
<br>
<input name="Submit" type="submit" value="Submit"/><br>
</form>
<br>

Also try with mysql_error() on the first query, at the moment its just on the mysql_fetch_array

 

I'm assuming you've selected your database in your connection file?

 

Yes I have used the form to add to the database earlier - it justs stopped working when I added the conditional formatting.

To be honest if it's nothing to do with the first mysql_query then I have no idea.

 

Although I would still bet that it is, try commenting that whole section out and running it again.

 

On a slightly related note, I assume you don't have a column in your db called 'numrows' so

 

$numrows = $row['numrows'];
if ($numrows>0) {

 

is not going to be any good, you will want to use mysql_num_rows, although this is not going to be your problem at the moment

Yes when I comment it out I get the below output

 

You Must enter a password

Your registration was not sucessful

This is the code: -

/*if (!$username) {
echo "You Must enter a username<br>";
$problem = "error";
}
$result = mysql_query("SELECT * from registration WHERE username='$username'") or die(mysql_error());
$row = mysql_fetch_array( $result ) or die(mysql_error());
$dbusername = $row['username'];
$numrows = mysql_num_rows;
if ($numrows>0) {
echo "Sorry that username is not avaialbe, please go back and try again.<br>";
$problem = "error";
}*/

Can anyone see an error in this code??

/* $result = mysql_query("SELECT * from registration WHERE username ='$username'") or die(mysql_error());
$row = mysql_fetch_array( $result ) or die(mysql_error());
$dbusername = $row['username'];
$numrows = mysql_num_rows;
if ($numrows>0) {
echo "Sorry that username is not avaialbe, please go back and try again.<br>";
$problem = "error";
}*/

The only thing I can suggest is that you test each line on its own.

 

I would guess that the error is on this line:

 

$result = mysql_query("SELECT * from registration WHERE username ='$username'") or die(mysql_error());

 

But it is not showing an error because you have error reporting switched off.

 

Are you certain that in your include dbconnect.php file you have selected your database and not just connected to it using

 

mysql_select_db($database_name, $connection_id);

yes the dbconnect.php is as below: -

 

<?php

 

$connection = mysql_connect("localhost",

                            "dbusername",

                            "dbpassword");

mysql_select_db("tablename", $connection);

 

?>

Try debugging it like the code below in a separate file away from the other code, so we can really try to isolate the error

 

//this should force it to show the errors
error_reporting("E_ALL");

$connection = mysql_connect("localhost",
                            "dbusername",
                            "dbpassword");
mysql_select_db("tablename", $connection);

$username = '' //make this equal to a username you know exists so we are sure to get a result

//Use if to test if it returns true
if($result = mysql_query("SELECT * from registration WHERE username ='$username'"))
  {
   echo 'did the mysql_query part<br />';
  } 

if($row = mysql_fetch_array($result))
  {
   echo 'did the mysql_fetch_array part<br />';
  }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.