如果添加的代码上传图像和PDF更新查询不工作。我对INSERT和UPDATE除了prepare
和execute
相同的代码,插入查询工作很细,更新查询没有。我也曾在形成一个包括enctype="multipart/form-data"
,这样我可以从$ _FILES获得的数据也是如此。此外,我已经使用$_FILES['photo']['tmp_name']
和$_FILES['pdf']['tmp_name']
为了使用功能move_uploaded_file
move_uploaded_file($_FILES['photo']['tmp_name'], 'destination');
和move_uploaded_file($_FILES['pdf']['tmp_name'], 'destination');
移动它们
function edit_profile($pid)
{
if($_SERVER['REQUEST_METHOD']=='POST')
{
echo "<pre>";
print_r($_POST);
print_r($_FILES);
echo "</pre>";
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$sel_post = $_POST['sel_post'];
$txt_post = $_POST['post'];
$post = "";
if(empty($txt_post))
{
$post = $sel_post;
}
else
{
$post = $this->add_new_post($txt_post);
}
if(empty($fullname) || empty($contact))
{
array_push($this->errors, MEND_FIELD_ERROR);
return;
}
if(!empty($_FILES['photo']['name']))
{
$photo = $_FILES['photo'];
$allowed_ext = array('png','jpg', 'pdf','jpeg', 'bmp', 'gif');
$allowed_size = 20000000;
$tmp_photo = $photo['tmp_name'];
$photo_size = $photo['size'];
$photo_error = $photo['error'];
$photo_ext = explode('.',$photo['name']);
$photo_ext = strtolower(end($photo_ext));
if(in_array($photo_ext,$allowed_ext))
{
if($photo_size <= $allowed_size)
{
$photo_new_name = time()."_".uniqid('',true).'.'.$photo_ext;
$upload_destination = './cdn/uploads/profile/'.$photo_new_name;
if(move_uploaded_file($tmp_photo,$upload_destination))
{
$photo_to_db = $photo_new_name;
}
else
{
array_push($this->errors, STORAGE_ERROR);
return;
}
}
else
{
array_push($this->errors, $document_name.' : '.FILE_SIZE_ERROR);
return;
}
}
else
{
array_push($this->errors, $photo_ext.' : '.FILE_EXT_ERROR);
return;
}
}
if(!empty($_FILES['pdf']['name']))
{
$pdf = $_FILES['pdf'];
$allowed_pdf_ext = array('pdf');
$allowed_pdf_size = 20000000;
$tmp_pdf = $pdf['tmp_name'];
$pdf_size = $pdf['size'];
$pdf_error = $pdf['error'];
$pdf_ext = explode('.',$pdf['name']);
$pdf_ext = strtolower(end($pdf_ext));
if(in_array($pdf_ext,$allowed_pdf_ext))
{
if($photo_size <= $allowed_pdf_size)
{
$pdf_new_name = time()."_".uniqid('',true).'.'.$pdf_ext;
$upload_pdf_destination = './cdn/uploads/profile_pdf/'.$pdf_new_name;
if(move_uploaded_file($tmp_pdf,$upload_pdf_destination))
{
$pdf_to_db = $pdf_new_name;
}
else
{
array_push($this->errors, STORAGE_ERROR);
return;
}
}
else
{
array_push($this->errors, $document_name.' : '.FILE_SIZE_ERROR);
return;
}
}
else
{
array_push($this->errors, $photo_ext.' : '.FILE_EXT_ERROR);
return;
}
}
$statement = $this->db->prepare("UPDATE `profiles` SET `fullname`=?,`email`=?,`contact`=?,`post`=?, `photo`=?, `pdf`=? WHERE `pid`=?");
if($statement->execute([$fullname,$email,$contact,$post,$pid, $pdf_to_db, $photo_to_db]))
{
ExitThis::send_to(URL.'profile/view_profile?id='.$pid);
}
else
{
array_push($this->errors, DATABASE_ERROR);
return;
}
}
}
仿佛更新查询工作正常,但数据仍保留在更新之前同上面的代码将返回我的view_profile页 - 没有变化。
编辑:执行它获得此之前调试$statement
后:
PDOStatement Object
(
[queryString] => UPDATE `profiles` SET `fullname`=?,`email`=?,`contact`=?,`post`=?, `photo`=?, `pdf`=? WHERE `pid`=?
)
这个问题可能是参数传递,按照顺序尝试错误的序列:
if($statement->execute([$fullname,$email,$contact,$post,$photo_to_db,$pdf_to_db,$pid]))
您通过使用错误的顺序变量execute
方法。
...db->prepare("UPDATE `profiles` SET `fullname`=?,`email`=?,`contact`=?,`post`=?, `photo`=?, `pdf`=? WHERE `pid`=?");
而接下来你调用执行与这些变量:
...->execute([$fullname,$email,$contact,$post,$pid, $pdf_to_db, $photo_to_db]))
去年3应该像$photo_to_db, $pdf_to_db, $pid
。您传递错误的PID,所以这就是为什么你没有看到更新的结果。
您也可以使用命名参数:http://php.net/manual/en/pdostatement.execute.php#example-1072
$statement = $this->db->prepare("UPDATE `profiles` SET
`fullname`=?,`email`=?,`contact`=?,`post`=?, `photo`=?, `pdf`=? WHERE
`pid`=?");
if($statement->execute([$fullname,$email,$contact,$post,$photo_to_db
$pdf_to_db,$pid]))
{
ExitThis::send_to(URL.'profile/view_profile?id='.$pid);
}
else
{
array_push($this->errors, DATABASE_ERROR);
return;
}
删除您的更新语句$ PID,并检查您的参数,请试试这个代码