Jump to content


Photo

why so buggy? help pls!


  • Please log in to reply
4 replies to this topic

#1 diskhub

diskhub
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 23 November 2005 - 10:15 AM

Hi, i have a code like this:
<?php
...
$query = "SELECT User_id from gp_users where User_name = '$NAME' and User_email = '$EMAIL'";
$result = mysql_query($query);
 $row2 = @mysql_num_rows($result);
if ($row2 == 0 || !$row2)
   { 
    DO A INSERT
    }
else
{
Do nothing.
}
...
?>

What i want to do is to query the database to see whether there is any entry. And if there is already one, then ignore an insertion. However, i do not know what is wrong. I don't seem to get anything from [!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--]mysql_num_rows($result)[!--colorc--][/span][!--/colorc--] and it always insert data into my database.

please help. Is my syntax for the $query right?

#2 manmadareddy

manmadareddy
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 23 November 2005 - 12:13 PM

<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'dbusername', 'dbpassword')
or die('Could not connect: ' . mysql_error());
mysql_select_db('dbname') or die('Could not select database');
//$message=stripslashes($_POST['txt_message']);
$sql="select username from mytable where username='".$_POST['username']."' and password='".$_POST['pass']."'";
$rs=mysql_query($sql);
$count=mysql_num_rows($rs);
if(!$count)
{
$insertsql="insert into mytable (id,username,password) values(0,'".$_POST['username']."','".$_POST['pass']."')";
mysql_query($insertsql);
header("Location:userhome.php");
exit;
}
else
{
echo "username alerady exists";
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="">
<input type='text' name='username' >
<input type='text' name='pass' >
<INPUT TYPE="submit">
</FORM>
</BODY>
</HTML>


Try this

#3 diskhub

diskhub
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 23 November 2005 - 01:36 PM

i did the same.

$query = "SELECT User_id from gp_users where User_name = '$NAME' and User_email = '$EMAIL'";
$result = mysql_query($query);
$row2 = @mysql_num_rows($result);
if ($row2 == 0 || !$row2)
{
DO A INSERT
}
else
{
Do nothing.

I also have mysql_connect too. just that i didn't mention the script on top.

Is
$query = "SELECT User_id from gp_users where User_name = '$NAME' and User_email = '$EMAIL'";
different from
$query = "SELECT User_id from gp_users where User_name = '.$NAME.' and User_email = '.$EMAIL.'";
?



#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 23 November 2005 - 06:22 PM

echo/print the query. i think $NAME and $EMAIL are null. if those 2 came from a form, make sure you use $_POST['NAME'] or turn register_globals on.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 diskhub

diskhub
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 24 November 2005 - 01:56 AM

[!--quoteo(post=321486:date=Nov 24 2005, 02:22 AM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ Nov 24 2005, 02:22 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
echo/print the query. i think $NAME and $EMAIL are null. if those 2 came from a form, make sure you use $_POST['NAME'] or turn register_globals on.
[/quote]

Hi they are not null.

anyway what's the different between these 3:
[!--coloro:#CC0000--][span style=\"color:#CC0000\"][!--/coloro--]$query = "SELECT User_id from gp_users where User_name = '$NAME' and User_email = '$EMAIL'";

$query = "SELECT User_id from gp_users where User_name = '.$NAME.' and User_email = '.$EMAIL.'";

$query = "SELECT User_id from gp_users where (User_name = '$NAME' and User_email = '$EMAIL')";[!--colorc--][/span][!--/colorc--]

which one you guys use usually?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users