0

Every time I try to create an account using this script, I get the following error: "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\registration.php on line 16 Record successfully inserted"

I am trying to check if the account name already exists.

I have done lots of searching around on this website but I can't seem to get this issue figured out.

Here is my code:

<?php
Include 'connect.php';
If(isset($_REQUEST['submit'])!='')
{
If($_REQUEST['Username']=='' || $_REQUEST['Password']=='')
{
Echo "please fill the empty field.";
}
Else
{
$result = mysql_query("SELECT Username FROM accounts WHERE Username =      ".$_REQUEST['Username']."");
$sql="insert into accounts(Username,Password) values('".$_REQUEST['Username']."',     '".$_REQUEST['Password']."')";
$res=mysql_query($sql);
if(!mysql_num_rows($result) >= 1)
{
Echo "Record successfully inserted";
}
Else
{
Echo "There is some problem in inserting record";
}
}
}
?>
user3362394
  • 3
  • 1
  • 2

1 Answers1

0

You have to use quotes in SQL around strings too. Instead of

$result = mysql_query("SELECT Username FROM accounts 
WHERE Username =      ".$_REQUEST['Username']."");

use

$result = mysql_query("SELECT Username FROM accounts 
WHERE Username = '".$_REQUEST['Username']."'");

And please read something about security concerning SQL queries. And please notice that the mysql extension is obsolet and one should use the new mysqli extension or PDO instead.

Ulrich Thomas Gabor
  • 6,388
  • 4
  • 24
  • 38