如果添加了用于上传图片和pdf的代码,更新查询将无法正常工作。除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`=?
)
答案 0 :(得分:1)
此问题可能是传递的参数顺序错误,请按以下顺序尝试:
if($statement->execute([$fullname,$email,$contact,$post,$photo_to_db,$pdf_to_db,$pid]))
答案 1 :(得分:0)
您使用错误的顺序将变量传递给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
答案 2 :(得分:0)
$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并检查您的参数,请尝试以下代码