Jump to content

Archived

This topic is now archived and is closed to further replies.

diskhub

why so buggy? help pls!

Recommended Posts

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?

Share this post


Link to post
Share on other sites

<?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

Share this post


Link to post
Share on other sites

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.'";

?

 

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

[!--quoteo(post=321486:date=Nov 24 2005, 02:22 AM:name=ryanlwh)--][div class=\'quotetop\']QUOTE(ryanlwh @ Nov 24 2005, 02:22 AM) 321486[/snapback][/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.

 

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?

Share this post


Link to post
Share on other sites

×

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.