2016-04-29 94 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