2016-09-17 51 views
4

好吧,我正在製作這個網站(有點像bitly或goo.gl),在那裏你輸入一個鏈接,並縮短它,但有一些額外的好東西。
現在,我只是託管一個本地mysql服務器進行測試,並且它的設置方式,一旦輸入鏈接縮短,隨機生成一個3位數的代碼並將其放入mysql表格以及其鏈接。根據mySQL查詢轉到網址

當您在網站上輸入代碼時,它會轉到mysql表,找到代碼,找到相應的鏈接,理論上應該將該鏈接帶回來,然後將其打開。

這是問題所在。它根本不起作用。我無法弄清楚它是否是html頁面的問題,還是腳本的問題,但我不知道。

我對PHP不太熟悉(語言即時查詢腳本),所以我不知道如何去解決問題。

我希望在這裏有人可以提供一些見解,爲什麼它可能無法正常工作。
我只知道當我點擊「Go」按鈕時,它會打開php代碼而不是運行它,我不知道如何解決它。

因爲我不知道到底是什麼問題,所以這裏是html和php代碼。

HTML: (剝離下來,只是身體爲了方便什麼有趣的其他地方)

<body> 
<center><form action="sql_query.php" method="post"> 
    <input class="enjoy-css" placeholder="" maxlength="3" name="var" type="text" /> 
    <script type="text/javascript" script-name="josefin-sans" src="http://use.edgefonts.net/josefin-sans.js"></script> 
    <input type="submit" class="enjoy-css_1" value="Go" src="sql_query.php" /> 
    <script type="text/javascript" script-name="josefin-sans" src="http://use.edgefonts.net/josefin-sans.js"></script> 
</form></center> 

PHP:

<?php 
$servername = "localhost"; 
$username = "nimbleadmin"; 
$password = "admin"; 
$dbname = "nimble"; 
$var = $_POST['var']; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$result = mysql_query("SELECT id, nimblecode, urlredirect, messageredirect  FROM linker WHERE nimblecode = '" + $var + "'"); 
if (!$result) { 
    echo 'Could not run query: ' . mysql_error(); 
    exit; 
} 
$row = mysql_fetch_row($result); 
echo "id: " . $row["nimblecode"]. " //// URL: " . $row["urlredirect"]. "  //// Message: " . $row["messageredirect"]. "<br>"; 
if ($row["messageredirect"]. == null) { 
    header('Location: ' . $row["urlredirect"]); 
    } else { 
    header('Location: http://nic.x10.mx/message?' . $row["nimblecode"]); 
} 
$conn->close(); 
?> 

任何幫助,不勝感激!我知道這是一個有趣的問題,但我仍在學習!

+1

你安裝了PHP和配置的Apache(或任何你正在使用的網絡服務器)來正確運行PHP文件? –

+0

theyre沒有跑掉一個網絡服務器,而只是通過thr瀏覽器進行本地測試,而正在開發以簡化流程。我如何安裝這些如果在本地運行? – Nic

+1

爲了在本地運行php文件,你需要安裝一個web服務器,像wamp或xampp這樣的東西應該讓你開始,它有apache,php,mysql捆綁在一起。 –

回答

1

你的代碼有語法錯誤,請在php中使用concatenation的dot,並在查詢時使用mysqli對象而不是mysql。

$result = mysqli_query($conn, "SELECT id, nimblecode, urlredirect, messageredirect  FROM linker WHERE nimblecode = '" . $var . "'"); 

全部代碼更改:儲存該文件的名稱爲 「tinyurl.php」

 
    <?php 
    $servername = "localhost"; 
    $username = "nimbleadmin";
$password = "admin";
$dbname = "nimble";
$var = $_GET['var'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query("SELECT id, nimblecode, urlredirect, messageredirect FROM linker WHERE nimblecode = '%s', $var);
if (!$result) {
echo 'Could not run query: ' . mysql_error(); exit;
}
if ($result->num_rows > 0) { $row = $result->fetch_assoc();
echo "id: " . $row["nimblecode"]. " //// URL: " . $row["urlredirect"]. "
//// Message: " . $row["messageredirect"]. "
";
} if ($row["messageredirect"] == "") {
header('Location: ' . $row["urlredirect"]);
} else if ($row["nimblecode"] != "") {
header('Location: http://nic.x10.mx/message ?' . $row["nimblecode"]);
} else { header('Location: http://nic.x10.mx/ '); } $conn->close(); ?>

在.htaccess需要添加此規則:

 
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^([^/]+)/?$ http://nic.x10.mx/tinyurl.php?var= $1 [QSA,NC,L] 

樣品URL條目 http://nic.x10.mx/abc

將重定向到數據庫中的目標URL。

+0

所以我已經實現了這個代碼,並且一切似乎都正常,除非我在php文件頁面上獲取http 500錯誤。任何想法爲什麼現在可能會發生? – Nic

+0

檢查error.log文件,所以它會給你詳細的錯誤。基於此,你可以修復它。 – Senthil