我工作过的代码只是将第一个文件名保存在数据库中。我希望能够将它们保存在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 [...]