我工作过的代码,除了,它只将第一个文件名保存在数据库中。我希望能够将它们保存在img_path下,用逗号或 |--->
img1.jpg, img2.jpg, img3.jpg
或
img123.jpg|img456.jpg|img789.jpg
形式:
<input type="file" name="img[]" multiple="" />
这是完整的代码
<?php
$con = mysql_connect( 'localhost', '', '') or die('Could not connect to mysql server.' );
mysql_select_db('shop', $con) or die('Could not select database.');
$product=mysql_real_escape_string($_POST['product']);
$description=mysql_real_escape_string($_POST['description']);
$category=mysql_real_escape_string($_POST['category']);
$color=mysql_real_escape_string($_POST['color']);
$sizes=mysql_real_escape_string($_POST['sizes']);
$style=mysql_real_escape_string($_POST['style']);
$material=mysql_real_escape_string($_POST['material']);
$stock=mysql_real_escape_string($_POST['stock']);
$ws_price=mysql_real_escape_string($_POST['ws_price']);
$rt_price=mysql_real_escape_string($_POST['rt_price']);
$sp_code=mysql_real_escape_string($_POST['sp_code']);
foreach ($_FILES["img"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name =$_FILES["img"]["tmp_name"][$key];
//$tmp_name = implode(",", $_FILES["img"]["tmp_name"][$key]);
$name = $_FILES["img"]["name"][$key];
$rand = rand(0000,9999);
$px = $rand . $name;
$px = preg_replace('/'s+/','_', $px);
$px = str_replace('&', 'and', $px);
$target = 'img/'.$category.'/';
if (is_dir($target) == false) {
mkdir($target, 0755);
echo "Directory Created</br>";
}
$u = $target . $px;
move_uploaded_file($tmp_name, $u);
echo "". $px.", ";
$a = array($px);
$x = implode("|", $a);
}
}
$sql = "INSERT INTO items (
product, description, category, color, sizes, style, material, stock, ws_price, rt_price, sp_code, img_path
)VALUES(
'$product', '$description', '$category','$color','$sizes','$style','$material','$stock','$ws_price','$rt_price','$sp_code','$x'
)";
$result = mysql_query($sql);
if($result){ echo("<br>Product Added!"); }
else{ echo("<br>Failed! Please try again.");}
mysql_close($con);
?>
我用过这个
$x = implode(',', $px)
$sql = "INSERT INTO items (img_path)VALUES('$x');";
而这个
$sql = "INSERT INTO items (img_path)VALUES('". implode(',', $px)."')";
它总是会给我一个错误。
Warning: implode(): Invalid arguments passed in [...]
这是我在这里提出一些建议的解决方案。
我没有使用内爆将它们保存在同一个表中的img_path中,而是创建了一个新的 img 来保存图像路径并使用产品 (id) 表中的 pid (product_id) 链接它们。
数据库
CREATE TABLE IF NOT EXISTS `img` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`img_path` varchar(255) NOT NULL,
`pid` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
这是编辑后的代码(当前工作...直到我再次打破它XD):
<?php
$con=mysqli_connect("localhost","user","password","db");
if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
$product=mysql_real_escape_string($_POST['product']);
$description=mysql_real_escape_string($_POST['description']);
$category=mysql_real_escape_string($_POST['category']);
$color=mysql_real_escape_string($_POST['color']);
$sizes=mysql_real_escape_string($_POST['sizes']);
$style=mysql_real_escape_string($_POST['style']);
$material=mysql_real_escape_string($_POST['material']);
$stock=mysql_real_escape_string($_POST['stock']);
$ws_price=mysql_real_escape_string($_POST['ws_price']);
$rt_price=mysql_real_escape_string($_POST['rt_price']);
$sp_code=mysql_real_escape_string($_POST['sp_code']);
$sql = "INSERT INTO products (product, description, category, color, sizes, style, material, stock, ws_price, rt_price, sp_code)
VALUES('$product', '$description', '$category','$color','$sizes','$style','$material','$stock','$ws_price','$rt_price','$sp_code')";
if (!mysqli_query($con,$sql)){
die('Error: ' . mysqli_error($con));
} echo "<p>Product Added! </p>";
foreach ($_FILES["img"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name =$_FILES["img"]["tmp_name"][$key];
$name = $_FILES["img"]["name"][$key];
$rand = rand(0000,9999);
$px = $rand . $name;
$px = preg_replace('/'s+/','_', $px);
$px = str_replace('&', 'and', $px);
$px = str_replace("'", '-', $px);
$target = 'img/'.$category.'/';
if (is_dir($target) == false) {
mkdir($target, 0755);
echo "Directory Created</br>";
}
$u = $target . $px;
move_uploaded_file($tmp_name, $u);
echo "". $px."</br>";
mysqli_query($con,"INSERT INTO img (img_path, pid) VALUES ('$px', (SELECT MAX(id) FROM products))");
}
}
mysqli_close($con);
?>