背景:
我正在通过表单在服务器上传图片....
问题:
但图像路径未在数据库中保存....
form.php的
<form name="save" id="save" method="post" enctype="multipart/form-data" action="save.php" >
<tr>
<td class="jtd">Image:</td>
<td><input type="file" name="product_image" id="product_image"/>
<img src="<?php echo $row['product_image'];?>" /></td>
</tr>
<tr>
<td><input type="button" value="Save" onclick="validatesubproductform();" /></td>
</tr>
</form>
脚本
<script type="text/javascript">
function validatesubproductform()
{
var id='<?php echo $id?>';
var imageflag = 1;
var re = new RegExp("^[^<>%$&*#%@|]*$");
var cproductimage = $("#product_image").val();
var product_imagename=document.getElementById('product_image');
if(cproductimage.length >1) {
var extension = cproductimage.split('.').pop().toUpperCase();
if (product_imagename.files && product_imagename.files[0])
var size = product_imagename.files[0].size;
if(size==0) {imageflag = 0;alert("Image Is corrupted.");}
if(re.test(product_imagename.files[0].name)===false)
{
imageflag=0;
alert('please upload the file without special characters and SPACES');
return false;
}
if (/\s/.test(product_imagename.files[0].name)) {
imageflag=0;
alert('The filename can not contain whitespace. Please rename the file.');
return false;
}
}
if(imageflag == 1) {
$("#savejsonhomescreen").submit();
}
else {
return false;
}
}
</script>
save.php
$uploaddir = '/var/www/html/mobile/phonecasejson/mobilecases/home_screen_img/';
$uploadfile = $uploaddir . basename($_FILES['product_image']['name']);
if(isset($_POST["productKey"]) && $_POST["productKey"]!=''){$updatefield=$updatefield.",product_key='".@$_POST["productKey"]."'";$insertColumn=$insertColumn.",product_key";$insertValue=$insertValue.",'".$_POST["productKey"]."'";}
if(isset($_FILES['product_image']['name']) && $_FILES['product_image']['name']!='')
{
echo $uploadfileUrl='http://139.59.24.243/mobile/phonecasejson/mobilecases/home_screen_img/'.$_FILES['product_image']['name'];
if (move_uploaded_file($_FILES['product_image']['tmp_name'], $uploadfile))
{
$updatefield=$updatefield.",product_image='".$response."'";
$insertColumn=$insertColumn.",product_image";
$insertValue=$insertValue.",'".$response."'";
}
}
if(@$_POST['id']='')
{
echo $sql = "INSERT INTO json_homescreen (".$insertColumn.") VALUES (".$insertValue.")";
$result = $db_handle->executeUpdate($sql);
$_SESSION['msg']="Record Saved Successfully1.";
header("location:jsonhomescreen.php");
die;
}
我正在使用mysqli,如果您需要任何信息,请告诉我....
提前致谢....
感谢您的帮助....我在此处发布问题之前尝试了很多....
答案 0 :(得分:1)
在第12行,您创建一个具有完整路径的变量。如果您希望名为product_image的列包含该路径,则需要在第38和40行使用$ uploadfile变量。如果要使用完整URL而不是文件路径,请使用$ uploadfileUrl变量。
将第40行更改为:
$insertValue=$insertValue.",'".$uploadfileUrl."'";