TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-04-2009, 03:57 PM   #1 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default if data is already in database

Hi I am trying to work out the following information
Lets say I have the word apple in my database and someone wants to place another apple in the db, I don't what them to be able to.

I don't want to have to use the unique key to do this, however I have already.

What I want to do is in the PHP if the word is in the database it ends the php code, and does not go on.

I was thinking something like this.

Code:
if ($row[name] =="apple'') {
END
}
else
{
continue 
};
russellharrower is offline  
Reply With Quote
Old 08-04-2009, 11:39 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

you can try the mysql_num_rows() function like this:
php Code:
$result = mysql_query('SELECT recordID FROM fruits WHERE name="apple"');
if(mysql_num_rows($result) > 0 {
    //error, apple already exists
}else{
    //congrats! you added an apple to the basket.
}

I should encourage you (me too) to use mysqli objects for security reasons, but I need to learn it myself too.
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
russellharrower (08-05-2009)
Old 08-05-2009, 12:32 AM   #3 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default

Quote:
Originally Posted by tony View Post
you can try the mysql_num_rows() function like this:
php Code:
$result = mysql_query('SELECT recordID FROM fruits WHERE name="apple"');
if(mysql_num_rows($result) > 0 {
    //error, apple already exists
}else{
    //congrats! you added an apple to the basket.
}

I should encourage you (me too) to use mysqli objects for security reasons, but I need to learn it myself too.
Yes you are correct it is a lot better to use mysqli however some servers don't seem to turn them on, due to and if you ask me this is silly...

CPU useage apparently it takes up to much? That was the excuse my server hosting company said. After tell them I leave to go somewhere else they turned it on for my account.
russellharrower is offline  
Reply With Quote
Old 08-05-2009, 12:40 AM   #4 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Baloney, go find yourself a better web host. Any host that doesn't support MySQLi because it is too server intensive must have really bad servers (think old dell sitting in closet)!
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 08-05-2009, 06:24 AM   #5 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default

Hi I am getting this error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/example/public_html/apple.php on line 14

line 14 is where the code you gave me is.

Thanks
russellharrower is offline  
Reply With Quote
Old 08-05-2009, 08:49 AM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

It seems like either your query failed, or you are not passing it the correct variable (can't accurately tell because I have no code to examine i.e. sql)
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
russellharrower (08-05-2009)
Old 08-05-2009, 09:18 AM   #7 (permalink)
The Contributor
 
russellharrower's Avatar
 
Join Date: Jul 2009
Posts: 80
Thanks: 13
russellharrower is on a distinguished road
Default

This is my code

PHP Code:
$filename $_POST['imageq'];

if (!
$bonushsystemconnect)
  {
  die(
'Could not connect: ' mysql_error());
  }

mysql_select_db("bonush_sy6"$bonushsystemconnect);


$imageresult mysql_query('SELECT id FROM isearch WHERE imageurl="$filename"');

if(
mysql_num_rows($imageresult) > 0) {
print 
"Already In DB";
end;
}else{
 
//congrats! you added an apple to the basket.
                                
$ran md5(uniqid(mt_rand(), true));

$size getimagesize($filename);
 
switch (
$size['mime']) {
    case 
"image/gif":
        
$type".gif";
        break;
    case 
"image/jpeg":
        
$type".jpg";
        break;
    case 
"image/png":
        
$type".png";
        break;
    case 
"image/bmp":
        
$type".bmp";
        break;



$test $ran;
$test $test.''.$type;

$fh fopen("$test"'w') or die("can't open file");

if(
$fh==false)
    die(
"unable to create file");


if(!@
copy ($filename,$test))
{
    
$errorserror_get_last();
    echo 
"COPY ERROR: ".$errors['type'];
    echo 
"<br />\n".$errors['message'];
} else {
    echo 
"File copied from remote!";



$sql="INSERT INTO isearch (site, imageurl, oururl, keywords) VALUES ('Peter', '$filename', '$test', 'lol')";

if (!
mysql_query($sql$bonushsystemconnect))
  {
  die(
'Error: ' mysql_error());
  }
echo 
"1 record added";
 }


For some reason the if statement to stop it going on an adding the new row or copying the image, it seems to ether not be checking the DB or there is something wrong with placing if statements in side else statements?

Last edited by codefreek : 08-05-2009 at 01:25 PM. Reason: PHP tags added - please read http://www.talkphp.com/lounge/4563-prettifying-pasted-code-talkphp.html
russellharrower is offline  
Reply With Quote
Old 08-05-2009, 01:14 PM   #8 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I think the error is here:
PHP Code:
$imageresult mysql_query('SELECT id FROM isearch WHERE imageurl="$filename"'); 
Single quotes strings don't parse the contents of a variable, everything is a string for them. try this:

php Code:
$imageresult = mysql_query('SELECT id FROM isearch WHERE imageurl="' . $filename . '"');
or this
php Code:
$imageresult = mysql_query("SELECT id FROM isearch WHERE imageurl='$filename'");

I prefer the first one, easy to read in text editors plus it gains a bit of speed.
tony is offline  
Reply With Quote
Old 08-05-2009, 01:54 PM   #9 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

php Code:
$filename = $_POST['imageq'];
$filename = mysql_real_escape_string($filename);

if (!$bonushsystemconnect)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("bonush_sy6", $bonushsystemconnect);

// I can inject anything i want here
$imageresult = mysql_query('SELECT id FROM isearch WHERE imageurl="'.$filename.'"');

if(mysql_num_rows($imageresult) > 0) {
print "Already In DB";
end;
}else{
 //congrats! you added an apple to the basket.
                               
$ran = md5(uniqid(mt_rand(), true));

$size = getimagesize($filename);
 
switch ($size['mime']) {
    case "image/gif":
        $type= ".gif";
        break;
    case "image/jpeg":
        $type= ".jpg";
        break;
    case "image/png":
        $type= ".png";
        break;
    case "image/bmp":
        $type= ".bmp";
        break;
}


$test = $ran;
$test = $test.''.$type;

$fh = fopen("$test", 'w') or die("can't open file");

if($fh==false)
    die("unable to create file");


if(!@copy ($filename,$test))
{
    $errors= error_get_last();
    echo "COPY ERROR: ".$errors['type'];
    echo "<br />\n".$errors['message'];
} else {
    echo "File copied from remote!";


// AND HERE
$sql='INSERT INTO isearch (site, imageurl, oururl, keywords) VALUES ("Peter", "'.$filename.'", ".$test.'", "lol")';

if (!mysql_query($sql, $bonushsystemconnect))
  {
  die('Error: ' . mysql_error())
  }
echo "
1 record added";
 }
}

Updated your code to include mysql injection prevention and your SQL queries to run without causing errors.

If you ran the code as you posted the SQL fails because it does not properly parse the query. When you are performing SQL queries it is always easier to use single quotes for the string.

@See
;http://us.php.net/manual/en/language....syntax.single
For more information

AND

http://us2.php.net/manual/en/functio...ape-string.php

For information on SQL injection
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 08-05-2009, 01:54 PM   #10 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Originally Posted by tony View Post
I think the error is here:
PHP Code:
$imageresult mysql_query('SELECT id FROM isearch WHERE imageurl="$filename"'); 
Single quotes strings don't parse the contents of a variable, everything is a string for them. try this:

php Code:
$imageresult = mysql_query('SELECT id FROM isearch WHERE imageurl="' . $filename . '"');
or this
php Code:
$imageresult = mysql_query("SELECT id FROM isearch WHERE imageurl='$filename'");

I prefer the first one, easy to read in text editors plus it gains a bit of speed.
Or you could use TalkPHP's favorite function : sprintf (if a website can have favorites )
PHP Code:

$imageresult 
mysql_query(sprintf("SELECT `id` FROM `isearch` WHERE `imageurl` = '%s'"mysql_real_escape_string($filename))); 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 08-05-2009 at 01:56 PM. Reason: added escaping
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Injection and mysql_real_escape_string Durux General 61 01-29-2013 12:20 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Retrving data from database to form knvuppula General 2 09-10-2008 04:33 AM
Cleaning data before entering database question Killswitch General 7 12-24-2007 11:29 PM
Tips: PHP security Village Idiot Tips & Tricks 22 11-23-2007 11:17 PM


All times are GMT. The time now is 08:23 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design