dongshang1768 2014-02-19 19:31
浏览 43
已采纳

即使SQL表列不为空,PHP表单也在更新SQL表

I am trying to make a PHP form that will only allow the user to update the MySQL Table column photo, if the photo column is blank. Currently, the form will still update the photo column even if there is data other than "blank" data. For example, the photo column contains the data "columbia.jpg" and the user submits the form with the image "Jefferson.jpg" in the first input. The image column's data gets replaced from columbia.jpg to jefferson.jpg when it is not supposed to replace it at all. Instead it should return an error message stating that the user must first delete the old image before adding a new one. The column data should only get replaced when the column data is equal to "blank". (Not the word "blank" but "".)

Here is my full PHP page code:

<?php
if (isset($_GET["id"])) {
    $sn = (int)($_GET["id"]);
?>
<!DOCTYPE html>
<head>
    <title>MySQL file upload example</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
    <form action="<?php $_PHP_SELF ?>" method="post" enctype="multipart/form-data">
        Photo 1: <input type="file" name="photo"><br>
        <input name="add_image" id="add_image" type="submit" value="Upload file">
    </form>
    <p>
        <a href="pdfget.php">See all files</a>
    </p>
</body>
</html>

<?php
if(isset($_POST['add_image']))
{
$dbLink = new mysqli('daom', 'sm', 'aer', 'kabm');

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 
 $pic=($_FILES['photo']['name']); 

$query = "SELECT photo FROM used_trailers WHERE id = $sn";
$result = mysqli_query($dbLink, $query);
$array=mysqli_fetch_assoc($result);

if($query = " "){
//Writes the information to the database 
        $query1 = 
            "UPDATE used_trailers ".
       "SET photo = '$pic' ".
       "WHERE id = $sn" ;
        // Execute the query
        $results = $dbLink->query($query1);

        // Check if it was successfull
        if($results) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded to Photo 1, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your image."; 
 }
} else {
echo '<p>You must first delete the "Photo 1" image, $array, in order to add a new photo. This is to prevent the server from overloading with too many images.</p>';
}
}
}
               echo "$query1";
?>

Thank you for any help. All help is appreciated.

  • 写回答

1条回答 默认 最新

  • donglie7268 2014-02-19 19:36
    关注

    There are some errors in your script. First of all if ($query = " ") will always return true, because you are assigning the variable $query the string " ". To correctly check this, you'd need to use if ($query == " ").

    However, this won't solve your problem as $query is the query - not the result. This should work

    $query = "SELECT photo FROM used_trailers WHERE id = $sn";
    $result = mysqli_query($dbLink, $query);
    $array = mysqli_fetch_assoc($result);
    
    if (empty($array['photo'])){
        //etc.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法