dongyunqin7307 2017-03-17 07:08
浏览 40
已采纳

PHP - 成功更新文件但更新文本失败

I made a simple update form for text and file. This is my code

<?php
require("config.php");
$id = $_GET['id'];

$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
while ($row = $result->fetch_assoc())
{   

?>

<html><head><title>test</title></head>
<body>

<form method="POST" action="" enctype="multipart/form-data">

    ID: <?php echo $id; ?><br>
    <input type="hidden" name="id" value="<?php echo $id; ?>" />

    Contract Title
    <input type="text" name="contract_title" value="<?php echo $row['contract_title']; ?>" /><br>

    Upload File:
    <?php echo $row['filename'] ?>
    <input type="file" name="upload"/><br>  
    <input type="submit" name="edit" value="Submit"/>
</form>
</body>
</html>

<?php
}

if(isset($_POST['edit'])  )
{

if ($_FILES['upload']['size'] != 0 ){

$contract_title = $con->real_escape_string($_POST['contract_title']);

$filename = $con->real_escape_string($_FILES['upload']['name']);
$filedata= $con->real_escape_string(file_get_contents($_FILES['upload']['tmp_name']));
$filetype = $con->real_escape_string($_FILES['upload']['type']);
$filesize = intval($_FILES['upload']['size']);

$query = "UPDATE `contracts` set `filename` = '$filename',`filedata` = '$filedata', `filetype` = '$filetype',`filesize` = '$filesize' WHERE `id` = '$id' " ;

if ($con->query($query) == TRUE) {
echo "<br><br> New record created successfully";
} else {
    echo "Error:<br>" . $con->error;
}

} else {

$contract_title = $con->mysqli_real_escape_string($_POST['contract_title']);

$filename = $con->real_escape_string($_FILES['upload']['name']);
$filetype = $con->real_escape_string($_FILES['upload']['type']);
$filesize = intval($_FILES['upload']['size']);

$query = "UPDATE `contracts` set `filename` = '$filename', `filetype` = '$filetype',`filesize` = '$filesize' WHERE `id` = '$id' " ;

if ($con->query($query) == TRUE) {
echo "<br><br> New record created successfully";
} else {
echo "Error:<br>" . $con->error;
}

}
$con->close(); 
}   

?>

After I clicked submit button, it successfully updated the file upload except the text. I don't want to change from post to get method because that will do the same problem to the upload file process. How do I fix this?

  • 写回答

2条回答 默认 最新

  • dougaojue8185 2017-03-20 00:17
    关注

    All this time after looking for mistakes at the code for hours, this is a simple mistake that I missed!

    $query = "UPDATE `contracts` set `contract_title` = '$contract_title', `filename` = '$filename',`filedata` = '$filedata', `filetype` = '$filetype',`filesize` = '$filesize' WHERE `id` = '$id' " ;
    

    I didn't type the contract_title in the query! my god

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错