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 10-17-2008, 05:30 PM   #21 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Bug

This is how i am doing it but i get

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\t\index.php on line 22

Warning: Invalid argument supplied for foreach() in C:\wamp\www\t\index.php on line 23

PHP Code:
<?php
error_reporting
(E_ALL E_NOTICE);
include 
"config.php";

if(isset(
$_POST['submit']))
{

    
$url=$_POST['url'];
    
    if(
strlen($url)<1
        {
        print 
"You did not enter a URL.";
    }
    else
{
    
$bringdb "SELECT * FROM upload";
            
            
$results mysql_fetch_array($bringdb);
            foreach(
$results as $result) {
            if(
$result == $url) {
            die (
'no!');
        }
    }
}
        if (
preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/watch\?v=.+$~i'$url))
        {
            
$sql="INSERT INTO `upload` (`link`) VALUES('$url')";
            
mysql_query($sql) or die(mysql_error());
            print 
"Link added!";
    }
        else
        {
            print 
"Link is invalid";    
    }
}


else
{
    print 
"<form action='index.php' method='post'>";
    print 
"URL(include http://):<br>";
    print 
"<input type='text' name='url' size='20'><br>";
    print 
"<input type='submit' name='submit' value='submit'></form>";
}  
?>
codefreek is offline  
Reply With Quote
Old 10-17-2008, 06:31 PM   #22 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

mysql_fetch_array() needs a query handle as a parameter not a string. something like

$bringdb = mysql_query("SELECT * FROM upload");
while($result = mysql_fetch_assoc($bringdb)){
if($result['url'] == $url)
die ('no!');
}

Last edited by tony : 10-17-2008 at 06:34 PM. Reason: submitted before making changes
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
codefreek (10-17-2008)
Old 10-17-2008, 07:14 PM   #23 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

ty you tony, but that got errors to..
codefreek is offline  
Reply With Quote
Old 10-17-2008, 08:51 PM   #24 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I forgot to test my script. I tested now and yeah I get an error with the mysql_fetch_array() and mysql_fetch_assoc() not getting a MySQL handle. So I guess the problem is with mysql_query(), I can't seem to find it though. not much help here.
tony is offline  
Reply With Quote
Old 10-17-2008, 10:46 PM   #25 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

i am getting t_else but cant find :S
codefreek is offline  
Reply With Quote
Old 10-17-2008, 10:48 PM   #26 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

This is how to from inside SQLYog. Simply right click on the desire table, and select "Manage Indexes".

Attached Thumbnails
small-php-error-thank-you-advance-sqlyog.jpg  
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
codefreek (10-17-2008)
Old 10-17-2008, 11:32 PM   #27 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Help

i get this
Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\t\index.php on line 39

and when i fix the bracket for the if thing it gives me same error just on line 63 :S


PHP Code:
<?php
error_reporting
(E_ALL E_NOTICE);
include 
"config.php";

if(isset(
$_POST['submit'])) {

    
$url=$_POST['url'];
   
if(
strlen($url)<1) {
    
            print 
"You did not enter a URL.";
        
            }



}

else

{

    

$videoid 'ML5T1Lm7oO8';
$query mysql_query("SELECT `id` FROM `upload` WHERE `id` = '{$videoid}' LIMIT 1") OR die(mysql_error());

if (
mysql_fetch_array($query))    {
                
                print 
"error";
                
                }

}

else

{
    

if (
preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/watch\?v=.+$~i'$url))     {
    
        
$sql "INSERT INTO `upload` (`link`) VALUES('$url')";
        
mysql_query($sql) or die(mysql_error());
        print 
"Link added!";
                
    
        
}


else
            {
        
                print 
"Link is invalid";
            
            }

}
else


{
    print 
"<form action='index.php' method='post'>";
    print 
"URL(include http://):<br>";
    print 
"<input type='text' name='url' size='20'><br>";
    print 
"<input type='submit' name='submit' value='submit'></form>";
    
}






include 
"watch.php";




?>
Why t_else error?

PS: thank you wildhoney i will look into that..

PS2: ID is primary in my db should it be the link ? or ?..
codefreek is offline  
Reply With Quote
Old 10-17-2008, 11:36 PM   #28 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You should use an editor that points syntax errors out for, such as Zend Studio. By placing all that code into Zend Studio, I can immediately see you have 2 else statements in 1 if statement. Like this:

php Code:
if (true)
{
   
}
else
{
   
}
else
{
   
}

And that's not allowed.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
codefreek (10-17-2008)
Old 10-17-2008, 11:42 PM   #29 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

i use komodo, and it tells u but i deleted and tried and etc.. it wont release the error :P

damn errors :(


This is how i feel right now :P
-.(Fatal error: Maximum brain power is exceeded in C:\brain\).-
-----
codefreek is offline  
Reply With Quote
Old 10-18-2008, 12:19 AM   #30 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Help

PHP Code:
<?php
error_reporting
(E_ALL E_NOTICE);
include 
"config.php";
 
if(isset(
$_POST['submit']))
{
 
    
$url $_POST['url'];
 
    if(
strlen($url)<1)
                {
        print 
"You did not enter a URL.";
    }
    
    else
{
        
                
        
if(
preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i'$url$id))
{
    echo 
$id[1]; // This is the ID.
    
$sql ="INSERT into upload (link) values('$id')";
    
    
mysql_query($url) or die(mysql_error());

    print 
"Link added!";
}

        else
                {
            print 
"Link is invalid or the link is already in are system!";   
        }
    }
}


else
{
    print 
"<form action='index.php' method='post'>";
    print 
"URL(include http://):<br>";
    print 
"<input type='text' name='url' size='20'><br>";
    print 
"<input type='submit' name='submit' value='submit'></form>";







include 
"watch.php";




?>
codefreek is offline  
Reply With Quote
Old 10-18-2008, 03:05 PM   #31 (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:
Only if the link column has a unique index on it.
Yea sorry I had forgotten that.

Anyway you can quickly add a unique to a column with a simple SQL statement:
sql Code:
ALTER TABLE `upload` ADD UNIQUE (`link`)

Your script works now?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 10-18-2008, 03:36 PM   #32 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Bug

i am not going with that just yet.

this is how the script is looking right now.
it wont take in i think it's something with the preg_match maybe
the two vars, don't know,
any ideas would be great.
Thank you!

PHP Code:
<?php
error_reporting
(E_ALL E_NOTICE);
include 
"config.php";
 
if(isset(
$_POST['submit']))    {
 
   
$url $_POST['url'];
 
if(
strlen($url)<1) {
        
        print 
"You did not enter a URL.";
        
        }
    
    else
    
        {
        
                
if(
preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/\?v=([\w-]+)~i'$url$id))    {
    
    echo 
$id[1]; // This is the ID.
    
$sql ="INSERT into upload (link) values(". (int) $id[1].")";
    
    
mysql_query($sql) or die(mysql_error());

    print 
"Link added!";
}

        else
         {
            print 
"Link is invalid or the link is already in are system!";   
        }
    }
}


else
{
    print 
"<form action='index.php' method='post'>";
    print 
"URL(include http://):<br>";
    print 
"<input type='text' name='url' size='20'><br>";
    print 
"<input type='submit' name='submit' value='submit'></form>";







include 
"watch.php";




?>
codefreek is offline  
Reply With Quote
Old 10-18-2008, 03:36 PM   #33 (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

Method of preventing duplicates using IGNORE:
IGNORE will basically stop MySQL returning the error on duplicate entries, therefore you can separate general SQL errors (i.e. syntax errors etc) from duplicate entry errors.

PHP Code:
$sql 'INSERT IGNORE INTO `upload`  (`link`) VALUES (' $id ')';

mysql_query($sql) or die(mysql_error());

if(!
mysql_affected_rows())
{
    echo 
'Link already exists';
}
else
{
    echo 
'Your link was added to the database';

You need a UNIQUE on the `link` column.
__________________
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:
codefreek (10-18-2008)
Old 10-18-2008, 04:51 PM   #34 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Bug Thank you in advance!

I tried that,
sketchMedia what you posted and made the link spot in db unique but when i try to submit something it only prints out echo 'Your link was added to the database';
but noting happens :/


This is how it looks now ?

any ideas,
and thank you everyone for the help!
i learn so much from you, and i will never forget the
kindness that i get from talkphp, specially, wildhoney
and sketchMedia, and tony! great people!
and of course my friend tanax! :D


PHP Code:
<?php
error_reporting
(E_ALL E_NOTICE);
include 
"config.php";
 
if(isset(
$_POST['submit']))    {
 
   
$url $_POST['url'];
 
if(
strlen($url)<1) {
        
        print 
"You did not enter a URL.";
        
        }
    
    else
    
        {
        
                
if(
preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/\?v=([\w-]+)~i'$url))    {
    
 
$sql "INSERT IGNORE INTO `upload` (`link`) VALUES('$url')";

mysql_query($sql) or die(mysql_error());

if(!
mysql_affected_rows())    {
    

    echo 
'Link already exists';
}
}
else
    {
    echo 
'Your link was added to the database';
    }  
  }
}


else
{
    print 
"<form action='index.php' method='post'>";
    print 
"URL(include http://):<br>";
    print 
"<input type='text' name='url' size='20'><br>";
    print 
"<input type='submit' name='submit' value='submit'></form>";


?>
Thank you in advance!


btw, willdhoney i bought ZendStudio ;)
but is there anyways to make the debug function to check for
common newbie mistakes ;)
codefreek is offline  
Reply With Quote
Old 10-18-2008, 04:59 PM   #35 (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

PHP Code:
error_reporting(E_ALL E_NOTICE);
include 
"config.php"
if(isset(
$_POST['submit'])) {

    
$url $_POST['url'];

    if(
strlen($url)<1) {
        print 
"You did not enter a URL.";
    } else {
        
        if(
preg_match('~^http://(?:[a-z]{2,3}\.)?youtube\.com/\?v=([\w-]+)~i'$url)) {

            
$sql "INSERT IGNORE INTO `upload` (`link`) VALUES('$url')";

            
mysql_query($sql) or die(mysql_error());

            if(!
mysql_affected_rows()) {
                echo 
'Link already exists';
            } else {
                echo 
'Your link was added to the database';
            }
            
        } else {
            echo 
'Your link was invalid or malformed';
        }
    }
} else {
    print 
"<form action='index.php' method='post'>";
    print 
"URL(include http://):<br>";
    print 
"<input type='text' name='url' size='20'><br>";
    print 
"<input type='submit' name='submit' value='submit'></form>";

Try that and see what it outputs.
__________________
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:
codefreek (10-18-2008)
Old 10-18-2008, 05:35 PM   #36 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

Your link was invalid or malformed
codefreek is offline  
Reply With Quote
Old 10-18-2008, 05:37 PM   #37 (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

k, now we know that its failing at the regex because preg_match is returing 0.

What its the string you are using to test?
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 10-18-2008, 05:40 PM   #38 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

YouTube - Dr Nitschke and the Sucide Workshop

just a simple one like that :S
codefreek is offline  
Reply With Quote
Old 10-18-2008, 06:24 PM   #39 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

was it, that you meant or?
codefreek is offline  
Reply With Quote
Old 10-18-2008, 06:35 PM   #40 (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 codefreek View Post
was it, that you meant or?
Yea, the regex isn't matching it.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
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


All times are GMT. The time now is 07:09 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