使用PHP类PDO、jquery、,
我的脚本中有一个bug;我试图上传一个图像到mysql,我使用PHPPDO、类和jquery。我确信脚本正在看到图像名称。我首先得到一个未定义变量'img'未声明的错误消息,但我使用isset(函数)修复了该错误消息,但仍然没有插入图像,我得到一个空的警报消息。对不起,我是新来的。这是我的密码使用PHP类PDO、jquery、,,php,jquery,mysql,pdo,Php,Jquery,Mysql,Pdo,我的脚本中有一个bug;我试图上传一个图像到mysql,我使用PHPPDO、类和jquery。我确信脚本正在看到图像名称。我首先得到一个未定义变量'img'未声明的错误消息,但我使用isset(函数)修复了该错误消息,但仍然没有插入图像,我得到一个空的警报消息。对不起,我是新来的。这是我的密码 form.php // html for inputs <form action="" method="post" enctype="multipart/form-data"> <inp
form.php // html for inputs
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name ="myimage" id="image">
<input type="submit" id="imageSend" >
</form>
image.js // the image script
$("#imageSend").click(function(){
var postImg = $("#image").attr("name");// image name
var dataString = 'img='+postImg;
$.ajax({
type:"POST",
url:"postimage.php",
data:dataString,
success: function(html){
alert(html);
},
});
});
postimage.php // script to post image
//class script included here
if(isset($_FILES['img'])){
$image = $_FILES['img'];
$class = new scriptimage();
$class->sendimage($image);
}
class script.php // class script
class scriptimage {
public $dns = "mysql:host=localhost;dbname=inspirationaldb";
public $root ="root";
public $dbpass ="";
public $conn;
//connection string
public function Connection(){
$this->conn = new PDO($this->dns, $this->root, $this->dbpass);
$this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
public function sendimg($uploadname){
$this->Connection();
$image_name = $uploadname['name'];
$image_size = $uploadname['size'];
$image_type = $uploadname['type'];
$image_tmp = $uploadname['tmp_name'];
if($image_size > 1048576){// 1 MB
echo "Large Image";
return false;
}
$path ="post-images/";
$image_exe = $this->GetImgExtntion($image_type);
$imageFolderPath = $path.$image_name.$image_exe;
$imageDBpath = $imageFolderPath;
if(move_uploaded_file($image_tmp, $imageFolderPath)){
$save=$this->conn->prepare("insert into images(image)values(:upload)");
$done=$save->execute(array(":upload" =>$imageDBpath));
if($done){
echo "image saved";
}
else{
echo "image not saved";
}
}
}
form.php//html用于输入
image.js//图像脚本
$(“#图像发送”)。单击(函数(){
var postImg=$(“#image”).attr(“name”);//图像名称
var dataString='img='+postImg;
$.ajax({
类型:“POST”,
url:“positmage.php”,
数据:dataString,
成功:函数(html){
警报(html);
},
});
});
postmage.php//用于发布图像的脚本
//这里包含了类脚本
如果(isset($\u文件['img'])){
$image=$\u文件['img'];
$class=newscriptimage();
$class->sendimage($image);
}
class script.php//class script
类脚本映像{
public$dns=“mysql:host=localhost;dbname=inspirationaldb”;
public$root=“root”;
公共$dbpass=“”;
公共康涅狄格州;
//连接字符串
公共功能连接(){
$this->conn=new PDO($this->dns,$this->root,$this->dbpass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常);
}
公共函数sendimg($uploadname){
$this->Connection();
$image_name=$uploadname['name'];
$image_size=$uploadname['size'];
$image_type=$uploadname['type'];
$image_tmp=$uploadname['tmp_name'];
如果($image_size>1048576){//1 MB
回波“大图像”;
返回false;
}
$path=“post images/”;
$image\u exe=$this->GetImgExtntion($image\u type);
$imageFolderPath=$path.$image\U name.$image\U exe;
$imageDBpath=$imageFolderPath;
if(移动上传的文件($image\u tmp,$imageFolderPath)){
$save=$this->conn->prepare(“插入图像(图像)值(:upload)”);
$done=$save->execute(数组(“:upload”=>$imageDBpath));
如果($完成){
回显“已保存图像”;
}
否则{
回显“图像未保存”;
}
}
}
我做了所有这些,但什么也没有发生,数据库中没有反馈或图像。打开错误报告。我看到的第一个错误是
$save=$this->conn->prepare(“插入图像(“图像”)值(:upload)”)
…试试这个抱歉,我跳过了那部分…在if之后有一个else语句…另外,您正在发布到表单方法,而Ajax方法我真的不想重新加载页面…我应该只发布到表单方法吗?