2016-04-29 97 views
1

我們正在我們的工作場所(高中)開發幫助臺系統。 我們要在網站上發佈一些用戶手冊直到數據庫。php腳本中的MySQL錯誤

$sql = "INSERT INTO usermanual (title, link, slug) VALUES ('$title', '$link', '$slug')"; 

這是我們的代碼,從形式獲得:

<form action="inc/publish-manual.php" method="post" accept-charset="ISO-8859-1"> 
    <p> 
     <label for="Title">Tittel:</label> 
     <br> 
     <input type="text" name="title" id="title"> 
    </p> 
    <p> 
     <label for="Link">Link til brukerveiledning:</label> 
     <br> 
     <input type="text" name="link" id="link"> 
    </p> 
    <p> 
     <label for="Slug">Slug:</label> 
     <br> 
     <input type="text" name="slug" id="slug"> 
    </p> 
    <input type="submit" value="Publiser"> 
</form> 

當我們運行這一點,我們只得到一個錯誤說:

ERROR: Was not able to execute INSERT INTO usermanual (title, link, slug) VALUES ('test', 'http://gauldal.vgs.no/upload/Gauldal/Bilder/Brukerveiledninger-IKT/Bruke%20Larermentor.pdf', 'test') 

當我們運行SQL在數據庫中工作,但不是來自我們的腳本。 我們一直在尋找這個很長一段時間,現在我們需要一雙新鮮的眼睛來看看錯誤在哪裏。

+3

請發佈'publish-manual.php'內容。 –

+2

寶貴的小費:準備好的陳述... – Naruto

+0

來自Clement Levallois:你可以附上你如何連接到你的數據庫?此外,您在查詢中沒有逃避用戶輸入? – Random

回答

1
<?php 
header('Content-type: text/html; charset=ISO-8859-1'); 

include '../config.php'; 
$link = mysqli_connect($servername, $username, $password, $dbname); 

if ($link === false) { 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 

$title = mysqli_real_escape_string($link, $_POST['title']); 
$link = mysqli_real_escape_string($link, $_POST['link']); 

function slugify($title) 
{ 
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $title); 
    return strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $title)); 
} 
$slug = slugify($title); 

$sql = "INSERT INTO usermanual (title, link, slug) VALUES ('$title', '$link', '$slug')"; 

if (mysqli_query($link, $sql)) { 
    header("location: ../admin.php"); 
} else { 
    echo "ERROR: Was not able to execute $sql " . mysqli_error($link); 
} 
?> 

我建議重命名數據庫鏈接到$連接:

$link = mysqli_connect($servername, $username, $password, $dbname); 

,因爲它看起來令人困惑,你必須在你的代碼2個不同的「鏈接」變量,它可能是這個問題

+0

@diEcho - 它也可以和mysql一起使用 –

+0

使用[prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param'](http: //php.net/manual/en/mysqli-stmt.bind-param.php)添加數據的方法明顯優於手動轉義。 – tadman

+0

@diEcho學會閱讀 –

0
<?php 
header('Content-type: text/html; charset=ISO-8859-1'); 
include '../config.php'; 
$link = mysqli_connect($servername, $username, $password, $dbname); 

if ($link === false) { 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 

$title = mysqli_real_escape_string($link, $_POST['title']); 
$link = mysqli_real_escape_string($link, $_POST['link']); 

function slugify($title) 
{ 
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $title); 
    return strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $title)); 
} 
$slug = slugify($title); 

$sql = "INSERT INTO usermanual (title, link, slug) VALUES ('$title', '$link', '$slug')"; 

if (mysqli_query($link, $sql)) { 
    header("location: ../admin.php"); 
} else { 
    echo "ERROR: Was not able to execute $sql " . mysqli_error($link); 
} 
?> 

這是publish-manual.php文件。 我使用配置文件連接到數據庫。

這是一個系統,我們有許多文件,它適用於一切,但不是這一個。

+0

您能否將此代碼添加到您的問題 – newman

+0

我喜歡你逃避的東西,這是一個很大的改進,但不要這樣做。使用[prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param'](http://php.net/manual/en/mysqli- stmt.bind-param.php)方法添加數據。 – tadman