Multiple Database entries

Hi there, if anyone could lend me some help that would be great, basically i have a user signup form with recaptcha, that allows the user to sign up. This is all working fine, but now i have came round to using the server to check if the user exists but even with the new code I’ve coded it doesn’t work and still adds the user even if the username is taken, it then proceeds to add the user to a mailing list if a box was ticked, this all works fine also, its just the multiple user problem which I’ve tried to address, but can’t get the code to do what it’s supposed to.

Any ideas?

Here is my code:
[php]<?php
require_once(‘recaptchalib.php’);
$privatekey = “**********”;
$resp = recaptcha_check_answer ($privatekey,
$_SERVER[“REMOTE_ADDR”],
$_POST[“recaptcha_challenge_field”],
$_POST[“recaptcha_response_field”]);

if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die (“The reCAPTCHA wasn’t entered correctly. Go back and try it again.” .
"(reCAPTCHA said: " . $resp->error . “)”);
} else {
if (!isset( $_POST[‘user’])){

}
$con=mysqli_connect(“","","”, “*****”);
// Check connection
$sql = “SELECT COUNT(*) FROM users WHERE username = ‘{$_POST[‘user’]}’”;
$sql_result = mysql_query($sql);
}if(mysql_num_rows($sql_result) > 0 ) { //check if there is already an entry for that username
die (“Already taken”);
exit ();
} else {
//insert to table
mysqli_real_escape_string();
$email = $_POST[‘email’];
$user = $_POST[‘user’];
$signed = date(“Y-m-d”);
$password = $_POST[‘pass’];
$password = md5($password);
$dob = implode(’-’, $_POST[‘date’]);
$query = “INSERT INTO users (email, username, signedup, DOB, password) VALUES (’{$email}’, ‘{$user}’, ‘{$signed}’, ‘{$dob}’, ‘{$password}’)”;
mysqli_query($con, $query);
mysqli_close($con);
}
if (!isset( $_POST[‘mail’])){

header( ‘Location: *********l’ ) ;

}
$con2=mysqli_connect(“","","", "***”);
mysqli_real_escape_string();
$email = $_POST[‘email’];
$query2 = “INSERT INTO emails (emailad) VALUES (’{$email}’)”;
mysqli_query($con2, $query2);
mysqli_close($con2);
header( ‘Location: **********’ ) ;

?>[/php]

You got your opening and closing brackets out of whack…

Look at line 14,15, and 16

on 14 you open the bracket { then on 16 you close the bracket } and on 15 you do nothing.

That’s where i was just moving code around to try and get it to work, even when this is fixed ill still get the same result.

Can you post the “All Fixed” code?

Haha, well i certainly wouldn’t call it all fixed, i merely just removed a header but okay :slight_smile:

[php]require_once(‘recaptchalib.php’);
$privatekey = “***”;
$resp = recaptcha_check_answer ($privatekey,
$_SERVER[“REMOTE_ADDR”],
$_POST[“recaptcha_challenge_field”],
$_POST[“recaptcha_response_field”]);

if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die (“The reCAPTCHA wasn’t entered correctly. Go back and try it again.” .
“(reCAPTCHA said: " . $resp->error . “)”);
} else
if (!isset( $_POST[‘user’])){
header( 'Location: ’ ) ;
}
$con=mysqli_connect("
","”,“", "”);
// Check connection
$sql = “SELECT COUNT(*) FROM users WHERE username = ‘{$_POST[‘user’]}’”;
$sql_result = mysql_query($sql);
if(mysql_num_rows($sql_result) > 0 ) { //check if there is already an entry for that username
die (“Already taken”);
exit ();
} else {
//insert to table
mysqli_real_escape_string();
$email = $_POST[‘email’];
$user = $_POST[‘user’];
$signed = date(“Y-m-d”);
$password = $_POST[‘pass’];
$password = md5($password);
$dob = implode(’-’, $_POST[‘date’]);
$query = “INSERT INTO users (email, username, signedup, DOB, password) VALUES (’{$email}’, ‘{$user}’, ‘{$signed}’, ‘{$dob}’, ‘{$password}’)”;
mysqli_query($con, $query);
mysqli_close($con);
}
if (!isset( $_POST[‘mail’])){

header( ‘Location: ******’ ) ;

}
$con2=mysqli_connect("***","**","", "");
mysqli_real_escape_string();
$email = $_POST[‘email’];
$query2 = “INSERT INTO emails (emailad) VALUES (’{$email}’)”;
mysqli_query($con2, $query2);
mysqli_close($con2);
header( ‘Location: *****’ ) ;

?>[/php]

Lets take a look at what you are doing…

[php] $sql = “SELECT COUNT(*) FROM users WHERE username = ‘{$_POST[‘user’]}’”;
$sql_result = mysql_query($sql);
if(mysql_num_rows($sql_result) > 0 ) { //check if there is already an entry for that username
die (“Already taken”);[/php]

COUNT(*) will always return 1 row…

Therefore every user name will be taken…

You should change it to…

[php]$sql = “SELECT 1 FROM users WHERE username = ‘{$_POST[‘user’]}’”;[/php]

Now you will only get a return value of 1 only if the user exists in the database, else the query will return nothing.

Is this your problem? I have no clue, I just stopped at the first thing I noticed that was wrong.

If the above doesn’t solve your issue, please post the HTML too :stuck_out_tongue:

I’ve tried adding in multiple accounts again using thee same username, it still redirects to account created page instead of stopping on the 23rd line. Any ideas?

Can you post the latest “All Fixed” Code with the HTML?

Of course my good sir.

HTML/POST page:

ive removed the date settings because it’s adding 150+ lines to the code

[php]

Username:

Password:

Email:

Date of birth:

- Year - ///REMOVED LINES - Month - ///REMOVED LINES

  <select name="date[day]" id="date[day]">
    <option> - Day - </option>
   ///REMOVED LINES
  </select>
  </span></p>
<p class="imagealign">&nbsp;</p>
<p class="imagealign">So we know your human:</p>
<span class="imagealign" onfocus="MM_validateForm('user','','R','textfield2','','R','cpass','','R','email','','R');return document.MM_returnValue">
<?php
      require_once('recaptchalib.php');
      $publickey = "***"; // you got this from the signup page
      echo recaptcha_get_html($publickey);
    ?>
</span>
<p>
  <span class="imagealign">      Recieve updates via email 
    <input name="mail" type="checkbox" id="mail" value="mail" checked="checked" />
  </span></p>
<p><span class="imagealign">To sign up you must agree to our terms of use
    <input name="mail2" type="checkbox" id="mail2" value="a" required />
</span></p>
<p class="imagealign">
  <input type="submit" name="submit" id="submit" value="Register"  />
</p>

<---------- Register your details with us on the left

Homepage

Or

Sign in

***

<?php [/php]

after

[php] $sql = “SELECT 1 FROM users WHERE username = ‘{$_POST[‘user’]}’”;[/php]

I’m assuming you changed it…

Add

[php]echo $sql;
exit;[/php]

What does the output show?

Ah sorry, i forgot to post the PHP;
Added it in and got this result:

SELECT 1 FROM users WHERE username = ‘Chris’

And when you run that in the database directly, you get 1 row back?

With no ‘Chris’ Row:
MySQL returned an empty result set (i.e. zero rows).

When i sign up and perform the query:

Showing rows 0 - 0 ( 1 total, Query took 0.0005 sec)

So it’s working like it should… What’s the issue?

Trouble is, when i go to sign up again, it creates a duplicate account, i’m trying to make it so it returns an error when the server detects that the username already exists

With no 'Chris' Row: MySQL returned an empty result set (i.e. zero rows).

When i sign up and perform the query:

Showing rows 0 - 0 ( 1 total, Query took 0.0005 sec)

So first time it’s 0, Second time it shows what?

Showing row 0 contains a user named Chris i believe, if it’s possible to have a row 0?

Sponsor our Newsletter | Privacy Policy | Terms of Service