当我单击表单按钮时,它成功调用insert函数和存储在变量中的所有值并传递给PHP文档。图像已成功上载到数据库中。但图像没有移动到文件夹。任何人都可以帮我解决这个问题。
HTML代码
<form style="border: thin solid black;">
<p>Enter Mobile Name:<input type="text" placeholder="Mobile Name" id="name"></p>
<p>Enter Mobile Price:<input type="text" id="price" placeholder="Price"></p>
<p>Select The Image:<input type="file" id="image"></p>
<p>Enter Mobile Brand:<input type="text" id="brand" placeholder="Nokia,Q mobile,Samsung,Iphone" style="width: 200px"></p>
<p>Select Category:<select id="category"> <option value="Mobile">Mobile</option> <option value="Tablet">Tablet</option></select></p>
<p><input id="b" type="button" onClick="Insert();" value="Upload"></p>
<h5 id="message"></h5>
</form>
Ajax代码
function Insert()
{
var image= document.getElementById("image").value;
var brand= document.getElementById("brand").value;
var price= document.getElementById("price").value;
var name= document.getElementById("name").value;
var category=document.getElementById("category").value;
if (image == "" && brand == "" && name == "" && price == "" )
{
document.getElementById("message").innerHTML="Please Fill The Fields";
return;
}
else if (image == "" || brand == "" || name == "" || price == "" )
{
document.getElementById("message").innerHTML="Please Fill The Form Correctly.";
return;
}
else
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
document.getElementById("message").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","insertdata.php?name="+name+"&price="+price+"&image="+image+"&brand="+brand+"&category="+category,true);
xmlhttp.send();
}
}
PHP代码
<?php
include("Sql_Server.php");
$error="";
$message="";
$images="";
$category="";
$brand="".$_GET['brand']."/".basename($_GET['image']);
$latestadded="Latest_Added/".basename($_GET['image']);
$images=base64_encode(basename($_GET['image']));
$imageFileType = pathinfo($latestadded,PATHINFO_EXTENSION);
echo $images."<br>";
echo $brand."<br>";
echo $_GET['image'];
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $imageFileType != "jfif" )
{
$message="Sorry, only jpg, jpeg, png ,jfif, gif files are allowed.";
}
elseif($_GET['category']=='Mobile')
{
$query="Insert Into mobiles(Image,Mobile_Name,Price,Brand)Values('".$images."','".$_GET['name']."','".$_GET['price']."','".$_GET['brand']."')";
$query2="Insert Into latest_mobiles(Image,Mobile_Name,Price,Brand)Values('".$images."','".$_GET['name']."','".$_GET['price']."','".$_GET['brand']."')";
$insert=mysqli_query($conn,$query);
$insert2=mysqli_query($conn,$query2);
if($insert && $insert2)
{
move_uploaded_file($_GET['image'],$brand);
echo $message="Submitted";
}
else
{
echo $error="Not Submitted";
}
}
else
{
$query="Insert Into tablets(Image,Tablet_Name,Price,Brand)Values('".$images."','".$_GET['name']."','".$_GET['price']."','".$_GET['brand']."')";
$query2="Insert Into latest_mobiles(Image,Mobile_Name,Price,Brand)Values('".$images."','".$_GET['name']."','".$_GET['price']."','".$_GET['brand']."')";
$insert=mysqli_query($conn,$query);
$insert2=mysqli_query($conn,$query2);
if($insert && $insert2)
{
move_uploaded_file($_GET['image'],$latestadded);
copy($latestadded,$brand);
echo $message="Submitted";
}
else
{
echo $error="Not Submitted";
}
}
?>
答案 0 :(得分:0)
首先,您的表单需要是“multipart / form-data
其次你应该通过HTTP POST而不是GET发送文件
第三,您上传的tmp文件名应为全局$ _FILES
php数组。像这样的东西
$_FILES[file input name here]['tmp_name']
这是将文件上载到服务器上的临时位置的名称。