我正在尝试使用图片上传数据库中的图片。它一直给我这个错误:
Array ( [0] => images (1).jpg
[1] => images (1).jpg
[2] => images (1).jpg [3] =>
images (1).jpg )
注意:未定义的偏移量:第82行的C:\ xamppv2 \ htdocs \ fabriek \ invullen.php中的4
致命错误:未捕获异常'PDOException',消息'SQLSTATE [HY093]:参数号无效:绑定变量数与C中的令牌数不匹配':\ xamppv2 \ htdocs \ fabriek \ invullen.php:82 Stack trace:#0 C:\ xamppv2 \ htdocs \ fabriek \ invullen.php(82):在第82行的C:\ xamppv2 \ htdocs \ fabriek \ invullen.php中抛出的PDOStatement->执行(数组)#1 {main}
因此,我们的想法是创建一个管理员可以上传产品的网上商店。现在唯一不起作用的是将多个图片上传到数据库中。我在互联网上搜索了这个错误,但是所有的错误都是用不同的代码编写的,而且他们大多都在谈论“错误。有谁知道这可能是什么问题?
希望任何人都可以提供帮助,
托马斯
代码:
$db_server = "localhost";
$db_username = "root";
$db_password = "";
$db_database = "meubelfabriek";
$conn = new PDO("mysql:host=$db_server;dbname=$db_database", $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit']))
{
$tipe_file1 = $_FILES['image1']['type'];
$tipe_file2 = $_FILES['image2']['type'];
$tipe_file3 = $_FILES['image3']['type'];
$tipe_file4 = $_FILES['image4']['type'];
$prijs = $_POST['prijs'];
$naam = $_POST['fname'];
$beschrijving = $_POST['desc'];
$maat1 = $_POST['maat1'];
$maat2 = $_POST['maat2'];
$maat3 = $_POST['maat3'];
$maat4 = $_POST['maat4'];
if ($tipe_file1 == "image/jpeg" || $tipe_file1 == "image/jpg"){
$aMyUploads = array();
foreach ($_FILES as $aFile) {
if(0 === $aFile['error']){
$newLocation = ''.$aFile['name'];
if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation))){
$aMyUploads[] = $newLocation;
} else {
$aMyUploads[] = '';
}
}
}
print_r($aMyUploads);
$stmt = $conn->prepare("INSERT INTO `producten` (naam, beschrijving, prijs, maat1, maat2, maat3, maat4, image1, image2, image3, image4) VALUES
(:naam, :beschrijving, :prijs, :maat1, :maat2, :maat3, :maat4,'".$aMyUploads[0]."','".$aMyUploads[1]."','".$aMyUploads[2]."','".$aMyUploads[3]."',)");
$stmt->execute(array(":naam"=>$naam, ":beschrijving"=>$beschrijving, ":prijs"=>$prijs, ":maat1"=>$maat1, ":maat2"=>$maat2, ":maat3"=>$maat3, ":maat4"=>$maat4,":image1"=>$aMyUploads[1], ":image2"=>$aMyUploads[2], ":image3"=>$aMyUploads[3], ":image4"=>$aMyUploads[4]));
}else{
echo "<script>alert('Failed to add data! Images must be inputed at last one and filetype JPG/JPEG')</script>
<script>setTimeout(\"self.history.back();\",0000);</script>";
}
}
答案 0 :(得分:0)
您可以通过字符串连接添加一些列,但将值绑定为Parameter。那样不行。因此,对所有列使用参数:
$stmt = $conn->prepare("INSERT INTO `producten` (naam, beschrijving, prijs, maat1, maat2, maat3, maat4, image1, image2, image3, image4) VALUES
(:naam, :beschrijving, :prijs, :maat1, :maat2, :maat3, :maat4,:image1,: image2, :image3, :image4)");
$stmt->execute(array(":naam"=>$naam, ":beschrijving"=>$beschrijving, ":prijs"=>$prijs, ":maat1"=>$maat1, ":maat2"=>$maat2, ":maat3"=>$maat3, ":maat4"=>$maat4,":image1"=>$aMyUploads[1], ":image2"=>$aMyUploads[2], ":image3"=>$aMyUploads[3], ":image4"=>$aMyUploads[4]));