如何将图像添加到php mysql数据库?

时间:2014-05-15 08:59:18

标签: php mysql database image

我有这个数据库添加页面,它允许你向数据库添加更多的对象,它工作正常,但我需要条目附加图像,不能让它工作。下面是我当前的工作解决方案( - 图像),我需要添加什么才能将图像上传到条目。变量是芬兰语,因为这是我需要最终产品的语言,所以不要介意它们。

以下是布局图片

添加页面:http://imgur.com/7y9mjoc

显示数据库中所有内容的索引页面:http://imgur.com/jwYbZZT

 <?php
$mysqli = new mysqli(---); // I put in lines to protect my personal info
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$Tyyppi = $_POST["tyyppi"];
$Malli = $_POST["malli"];
$Sarjanumero = $_POST["sarjanum"];
$Paikka = $_POST["paikka"];
$Kuvaus = $_POST["kuvaus"];
$Lainassa = $_POST["laina"];
$Lainaaja = $_POST["lainaaja"];
$Puhelin = $_POST["puhelin"];
$Sposti = $_POST["sposti"];
$Palautus = $_POST["palautus"];

if ($Lainassa==1){
if ($stmt = $mysqli->prepare("INSERT laitteet (tyyppi, malli, sarjanumero, paikka, kuvaus, lainassa, lainaaja, puhelin, sposti, palautus) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
                {
                    $stmt->bind_param("sssssissss", $Tyyppi, $Malli, $Sarjanumero, $Paikka, $Kuvaus, $Lainassa, $Lainaaja, $Puhelin, $Sposti, $Palautus);
                    $stmt->execute();
                    $stmt->close();
                }
                else
                {
                    echo "ERROR: Could not prepare SQL statement.";
                }           
}else{
if ($stmt = $mysqli->prepare("INSERT laitteet (tyyppi, malli, sarjanumero, paikka, kuvaus, lainassa) VALUES (?, ?, ?, ?, ?, ?)"))
                {
                    $stmt->bind_param("sssssi", $Tyyppi, $Malli, $Sarjanumero, $Paikka, $Kuvaus, $Lainassa);
                    $stmt->execute();
                    $stmt->close();
                }
                else
                {
                    echo "ERROR: Could not prepare SQL statement.";
                }   
}
mysqli_close($mysqli);
?>

3 个答案:

答案 0 :(得分:0)

更改您的插入查询。您缺少into

$mysqli->prepare("INSERT laitteet (tyyppi, malli, sarjanumero, paikka, kuvaus, lainassa) VALUES (?, ?, ?, ?, ?, ?)"))

$mysqli->prepare("INSERT into laitteet (tyyppi, malli, sarjanumero, paikka, kuvaus, lainassa) VALUES (?, ?, ?, ?, ?, ?)"))

答案 1 :(得分:0)

INSERT语句的正确语法是:

INSERT INTO tbl_name () VALUES();

这是链接:http://dev.mysql.com/doc/refman/5.6/en/insert.html

答案 2 :(得分:0)

您的查询语法不正确 - 您缺少&#39; INTO&#39; 将if语句替换为当前 - if ($stmt = $mysqli->prepare("INSERT laitteet (tyyppi, malli, sarjanumero, paikka, kuvaus, lainassa) VALUES (?, ?, ?, ?, ?, ?)"));

**After correction**- 
`if ($stmt = $mysqli->prepare("INSERT INTO laitteet (tyyppi, malli, sarjanumero, paikka, kuvaus, lainassa) VALUES (?, ?, ?, ?, ?, ?)"));`