用于网址缩短的PHP代码无法正常工作,页面无法重定向

时间:2016-09-18 20:28:16

标签: php sql mysqli

我正在使用此代码创建一个urlshortner,如果来自美国的用户访问该链接,他将被重定向到某个页面,如果来自任何其他国家/地区的用户访问该链接,他将被重定向到其他页面。它没有给我任何错误,但也没有给我工作。

<?php
$con = mysqli_connect("localhost","root","");
if (!$con){
   die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con, "shortenedurls"); //Replace with your MySQL DB Name

$de= mysqli_real_escape_string($con, $_GET["decode"]);

$sql = 'select * from short_urls where shorturl="$de"';
$urlid = mysqli_query($con,'SELECT id FROM short_urls WHERE shorturl="$de"');

$result=mysqli_query($con, 'SELECT * FROM redirect WHERE urlid="$urlid" LIKE "%US%"') or die(mysqli_error($con));
if(mysqli_num_rows($result) == 0 ) {
    $result1 = mysqli_query($con, 'SELECT * FROM short_urls WHERE shorturl="$de"');
    while($row = mysqli_fetch_array( $result1)) {
        $res=$row['urlinput'];
        header("location:".$res);
    }
} else {
    while($row = mysqli_fetch_array( $result)) {
        $res=$row['urlinput1'];
        header("location:".$res);
    }
}
?>

1 个答案:

答案 0 :(得分:0)

id

无法正确解析,因为它也在单引号内;他们优先考虑。交换单引号和双引号,该问题已修复。

例如

"$de" 

此查询在语法上也是不正确的

$urlid = mysqli_query($con,"SELECT id FROM short_urls 
                            WHERE shorturl='$de'");

应该是

$result=mysqli_query($con, "SELECT * FROM redirect 
                            WHERE urlid='$urlid' LIKE '%US%'")