gpt4 book ai didi

php - 在数据库中的单行中插入多个图像名称

转载 作者:行者123 更新时间:2023-12-01 00:42:57 26 4
gpt4 key购买 nike

请 friend 帮我解决这个问题。我已经创建了一个可以将多个图像插入数据库的单行的地方,现在我的代码运行良好但是不可能将所有图像存储在它存储在多行中的单个数据库行中。这是我到目前为止所做的。

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");



$uploads_dir = 'photo/';
foreach ($_FILES["image"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["image"]["tmp_name"][$key];
$name = $_FILES["image"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$sql=mysql_query("INSERT INTO multiimg SET image='$name'");
}
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function addmore(num)
{
if(num==1)
{
document.getElementById('field2').style.display='block';
document.getElementById('ni1').style.display='block';
return false;
}
else if(num==2)
{
document.getElementById('field3').style.display='block';
return false;
}

}
</script>
</head>

<body>
<form enctype="multipart/form-data" name="" action="" method="post">
<div id="field1">Enter One Image :<input type="file" name="image[]" id="img1"/><a href="#" onclick="addmore(1)" id="ni1">addmore...</a></div>
<div id="field2" style="display:none;">Enter Two Image :<input type="file" name="image[]" id="img2"/><a href="#" onclick="addmore(2);">add more...</a></div>
<div id="field3" style="display:none;">Enter Three Image :<input type="file" name="image[]" id="img3"/><a href="#" onclick="addmore(3)" id="ni3">addmore...</a></div>
<div id="field4" style="display:none">Enter Forth Image :<input type="file" name="image[]" id="img4"/><a href="#" onclick="addmore(4)" id="ni4">addmore...</a></div>

<input type="submit" name="submit"/>

</form>

</body>
</html>

enter image description here

最佳答案

试试下面的代码

$images_name ="";
foreach ($_FILES["image"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["image"]["tmp_name"][$key];
$name = $_FILES["image"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$images_name =$images_name.",".$name;
}
}

$sql=mysql_query("INSERT INTO multiimg(image) values('".$images_name."')");

关于php - 在数据库中的单行中插入多个图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com