 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
10-16-2008, 07:15 PM
|
#1 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
Small php error - Thank you in advance!
there is no error outputted but i know the problem is in the preg_match part. Some assistance would be great!
Thank you!
Rules for preg:match, and to blur for me to read ;)
PHP Code:
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags [, int $offset ]]] )
The script is small and might be ugly but it is only a practice run ;) so no flame :)
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.";
}
}
if (preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i','',$url)) {
$url="INSERT into upload (link) values('$url')";
mysql_query($url) or die(mysql_error());
print "Link added!";
}
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>";
}
?>
 Code;Freeek,. 
|
|
|
|
10-16-2008, 07:46 PM
|
#2 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
PHP Code:
preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i','',$url)
supposed to be:
PHP Code:
preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i', $url)
I'm not sure if you have to use matches, since you just want 1 match; true or false.
__________________
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
10-16-2008, 08:01 PM
|
#3 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
still the same..
|
|
|
|
10-16-2008, 08:41 PM
|
#4 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
CODE EDIT:
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
{
$check = (preg_match('~^http://[a-z]{2,3}\.youtube\.com/\?v=[\w-]+(?:&feature=related)?$~i','',$url));
if($check = TRUE)
{
$url="INSERT into upload (link) values('$url')";
mysql_query($url) or die(mysql_error());
print "Link added!";
}
}
}
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>";
}
?>
|
|
|
|
10-16-2008, 09:35 PM
|
#5 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 336
Thanks: 8
|
I tried your code and the preg_match is always returning a value, but I am guessing you just want to catch url's that come from youtube? it's a regex problem, I am not an expert in that area, but it would help to know what are you trying to match in the url.
|
|
|
|
10-16-2008, 09:49 PM
|
#6 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Your call to preg_match isn't going to work, you need to follow preg_match(pattern, test_string) for what you're trying to do with it. Also, in the if statement below it, you're assigning TRUE to the $check variable; not comparing its value to TRUE, use the double equals operator ($check == TRUE) (or ($check === 1), or simply ($check)).
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
10-16-2008, 10:12 PM
|
#7 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
If you don't mind me asking, what do you mean by test_string ?..
PS: Thank you for the help everyone!
|
|
|
|
10-16-2008, 10:19 PM
|
#8 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
The string you want to search in with the expression.
__________________
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
10-16-2008, 10:52 PM
|
#9 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
Now when i try to submit anything i get blankpage :S?
|
|
|
|
10-16-2008, 11:11 PM
|
#10 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
because your preg_match is returning 0 or false I guess, either that or the script is failing further up, put an else on the preg_match if statement and see if it shows, this will tell you that there might be a problem with the regex (if the string passed is known to be a valid string)
Also its pointless assigning the result of preg_match to a variable in this case as it will only result in the number of times the pattern matched and because it stops at the first match, this will be either 0 or 1 (or false if it failed in some way). As PHP uses 0 and 1 as true and false the result will only ever be true or false.
This would be a better way of doing it:
PHP Code:
if(preg_match('pattern','string to test')) { // Do something } else { // Do something else }
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
|
The Following User Says Thank You to sketchMedia For This Useful Post:
|
|
10-17-2008, 12:37 AM
|
#11 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
@sketchMedia it works now, no blank page anymore but..
it wont block out none youtube links i can write bla bla and it will accept it..
any ideas?
Thank you..
|
|
|
|
10-17-2008, 01:26 AM
|
#12 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
You don't have an else for if the link is invalid. That would be the reason for your blank page. Try this code:
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/watch\?v=.+$~i', $url)) { $url="INSERT into upload (link) values('$url')"; mysql_query($url) or die(mysql_error()); print "Link added!"; } else { print "Link is invalid"; } } } else { print "<form action='YouTube.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>"; }
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
|
The Following User Says Thank You to Wildhoney For This Useful Post:
|
|
10-17-2008, 01:59 AM
|
#13 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
Thank you, Wildhoney! you're the best!
PS: anyone know how i can block out so that you can not post the same link twice, maybe just do a check from db and see if that exists then print out this already in db ?
any ideas! thank you!
|
|
|
|
10-17-2008, 10:29 AM
|
#14 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
Use 'IGNORE' on the INSERT statement.
sql Code:
INSERT IGNORE INTO `upload` (`link`) VALUES('$url')
Should stop duplicates.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
|
The Following User Says Thank You to sketchMedia For This Useful Post:
|
|
10-17-2008, 02:33 PM
|
#15 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
sketchMedia, it did not work.
|
|
|
|
10-17-2008, 03:00 PM
|
#16 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by sketchMedia
Should stop duplicates.
|
Only if the link column has a unique index on it. The important thing is the index, it's up to the dev if they want to use ignore or go with the error generated when an insert tries to duplicate a unique key.
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
10-17-2008, 03:02 PM
|
#17 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
so if i change it to a unique.. it would work?
would it also make other problems?
|
|
|
|
10-17-2008, 03:04 PM
|
#18 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
PHP Code:
$results = mysql_fetch_array($query);
foreach($results as $result) {
if($result == $url) {
die ('SAMELINK!');
}
}
this is what tanax told me to try..
THANK YOU TANAX! 
|
|
|
|
10-17-2008, 05:23 PM
|
#19 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Whilst that does work, just bear in mind that Salathe's approach is the more professional approach. You would make the link column an index, and set the unique property on it, you would then handle the response from MySQL.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
|
The Following User Says Thank You to Wildhoney For This Useful Post:
|
|
10-17-2008, 05:28 PM
|
#20 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
|
could anyone give an example on how to do that?
and thank you for taking time to help out
i am grateful!
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|