Page 1 of 1

[SOLVED]ok..im confused..

Posted: Mon Jul 10, 2006 4:29 pm
by dull1554
ok so ive been having sme problem with an AIM boit im wirting.....

Code: Select all

$sql_insert = mysql_query("INSERT INTO buddies SET name='" . $checkmsg[1] . "'");
					if($sql_insert)
					{
						$this->send_im($args['user'], "<font face='Verdana' size=1 color='#006699'>" . $checkmsg[1] . " was successfully added to our database, you will be able to check " . $checkmsg[1] . "'s buddyinfo within fifteen minutes.</font>");
					}
			
					else
					{
						$dberrormsgtosend = "<font face='Verdana' size=1 color='#006699'>There is currently a problem with our database, please try again soon.</font>";
						$this->send_im($args['user'], $dberrormsgtosend);
					}
works fine, but this will allow a user to add an existing user....

so heres what i tried

Code: Select all

$isindb = mysql_fetch_row(mysql_query("SELECT * FROM buddies WHERE name='{$checkmsg[1]}'")) or die(mysql_error());				
				if($isindb != 0){$this->send_im($args['user'], "<font face='Verdana' size=1 color='#006699'>That user is allready in the Database!</font>");}	
				elseif($isindb == 0) {
					$sql_insert = mysql_query("INSERT INTO buddies SET name='" . $checkmsg[1] . "'");
					if($sql_insert)
					{
						$this->send_im($args['user'], "<font face='Verdana' size=1 color='#006699'>" . $checkmsg[1] . " was successfully added to our database, you will be able to check " . $checkmsg[1] . "'s buddyinfo within fifteen minutes.</font>");
					}
			
					else
					{
						$dberrormsgtosend = "<font face='Verdana' size=1 color='#006699'>There is currently a problem with our database, please try again soon.</font>";
						$this->send_im($args['user'], $dberrormsgtosend);
					}
				}

so i tried this... if i try to have it add someone thats allready in the database, it tells me that and continues with no problems....

if i try to add someone that isnt in the database it outputs nothing, no errors, and dies.....

this is being run through the php console and before someone asks

Code: Select all

PHP Version: 5.1.4
PHP OS: WINNT
Error Reporting: 2039 (E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR | E_COMPILE_WARNING | E_COMPILE_ERROR | E_CORE_WARNING | E_CORE_ERROR | E_PARSE | E_WARNING | E_ERROR)
Register Globals: Off
Short Tags: Off
Display Errors: On
Loaded Extensions:

   bcmath           calendar         com_dotnet       ctype         
   date             ftp              hash             iconv         
   odbc             pcre             Reflection       session       
   libxml           standard         tokenizer        zlib          
   SimpleXML        dom              SPL              wddx          
   xml              xmlreader        xmlwriter        apache2handler
   curl             gd               gettext          imap          
   mbstring         exif             mime_magic       ming          
   mssql            mysql            mysqli           openssl       
   pdf              PDO              pdo_mssql        pdo_mysql     
   soap             sockets          SQLite           xmlrpc        
   xsl              zip              Zend Optimizer

Posted: Mon Jul 10, 2006 6:42 pm
by RobertGonzalez
Try using a different INSERT query syntax...

Code: Select all

$sql = "INSERT INTO `tablename` (`field1`, `field2`, `field3`) VALUES ('$value1', '$value2', '$value3')";
See if that does anything for you. You may also want to throw some die statements in there for failure of the query. That way you can see if the database is returning an error on you and what the error is.

Posted: Mon Jul 10, 2006 7:16 pm
by bdlang

Code: Select all

$isindb = mysql_fetch_row(mysql_query("SELECT * FROM buddies WHERE name='{$checkmsg[1]}'")) or die(mysql_error());                       
                                if($isindb != 0){$this->send_im($args['user'], "<font face='Verdana' size=1 color='#006699'>That user is allready in the Database!</font>");}   
                                elseif($isindb == 0) {
Your call to mysql_fetch_row() returns (assuming the query works properly) an array, which is not comparible to 0. You must compare the value of count($isindb) to 0.


One of these two methods would be preferable, the first is the most efficient IMHO

Code: Select all

// select a COUNT() of all users that match
$checkUserSQL= "SELECT COUNT(*) FROM buddies WHERE name='{$checkmsg[1]}'";
// mysql_result pulls the count value only
$isindb= mysql_result(mysql_query($checkUserSQL), 0);
// if the count result was 0, user doesn't exist
if ( $isindb == 0 ) {
    // add the user
} else {
    // show user the 'user exists error', have them choose again
}
The other method uses mysql_num_rows() to the same end:

Code: Select all

// select the UserID (or whatever ID column name)
$checkUserSQL= "SELECT UserID FROM buddies WHERE name='{$checkmsg[1]}'";
// mysql_num_rows() grabs the number of records returned, if any
$isindb= mysql_num_rows(mysql_query($checkUserSQL));
// if there were no records returned
if ( $isindb == 0 ) {
    // add the user
} else {
    // show user the 'user exists error', have them choose again
}
The best method is to have your database setup to use a UNIQUE index on the `name` column so that only one unique username will be ever allowed per user. The INSERT literally fails if you try to insert with the same name. The logic depending on this failure determines if the user must choose another username.

BTW, the INSERT INTO table SET column='data' type syntax is fine, just not what everyone's used to.

Posted: Tue Jul 11, 2006 1:39 pm
by dull1554
hmmm thankyou, i made my name row unique. never thought of doing that.