php - 用图像更新语句

标签 php mysql

我有一个 php 页面,其中包含用于更新记录和图像的表单,我不知道更新语句有什么问题,,, 字段的值被采用,我可以通过 GET 方法在 url 上看到它们...但是当我运行页面并且更新记录信息没有改变并且页面上没有出现任何内容时,因为没有任何字段进行更新我认为我的更新语句有问题,,,这里是代码:

<?php

    // Connect to the database
    require("includes/conn.php");
    // Script Variables

    $target_dir = 'images/';

    $file_given = false;
    $inputs_given = false;
    $id_given = false;

    if(isset($_POST['serialid']) && $_POST['serialid'] != "")
    {
        $serialid = $_POST['serialid'];
        $id_given = true;
    }


        // You only need to catch input from a create or modify action, so start by checking for ALL the REQUIRED inputs
        if(isset($_POST['name']) && $_POST['name'] != "" && isset($_POST['description']) && $_POST['description'] != "" && isset($_POST['price']) && $_POST['price'] != "")
        {
            $name = $_POST['name'];
            $paragraph = $_POST['description'];
            $price = $_POST['price'];

            if(isset($_POST['picture']) && $_POST['picture'] != "")
            {
                $picture = basename($_FILES['picture']['name']);
                $file_given = true; 
            } 

            // Just some verification (not really much, but you can write your own functions and slot them in
            $name_safe = true;
            $description_safe = true;
            $price_safe = true;
            $picture_safe = false;


            if($_FILES["picture"]["type"] == "image/gif" || $_FILES["picture"]["type"] == "image/jpg" || $_FILES["picture"]["type"] == "image/png" || $_FILES["picture"]["type"] == "image/bmp")
                $picture_safe = true;

            if($name_safe && $description_safe && $price_safe && $picture_safe)
                $inputs_given = true;
        }

        if($id_given && $inputs_given)
        {
            // Search for the record and see if it exists
            $get_record = mysql_query("SELECT serial, picture FROM products WHERE serial='$serialid'");
            $record_exists = mysql_num_rows($get_record);

            if($record_exists == 1)
            {
                if($file_given)
                {
                    $update_image = ", picture='$picture'";

                    // Now we need to remove the old image from the file system and upload our new one in it's place

                    $previous_image = mysql_result($get_record,'0','picture');
                    unlink($target_dir . $previous_image);

                    //Now that the previous image has been removed, we need to upload our new image
                    $new_image = $target_dir . $picture ;
                    move_uploaded_file($_FILES['picture']['tmp_name'], $new_image);
                }
                else
                    $update_image = "";

                if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price', " . $update_image . " WHERE serial='$serialid'"))
                    $action_output = "Record successfully modified.";
                else
                    $action_output = "Record modification unsuccessful.";
            }
            else
                $action_output = "The record id you specified does not exist.";
        }

?>
<html>
    <head>
        <title>Manage Records</title>
    </head>

    <body>
        <?php echo $action_output; ?>
    </body>
</html>
<?php
    // Disconnect from the database
?>

这是我点击修改时的url

http://localhost/Shopping/update.php?name=View+Sonic+LCD&description=LCD&price=250&picture=C%3A%5CDocuments+and+Settings%5Ce2565%5CMy+Documents%5CTwasul%5Ctlogo%5Cicon%5Cpic1.jpg&serialid=1

我的修改表单是这样的

<?php

    // Connect to the database
    require("includes/conn.php");
    $id_given = false;
    if(isset($_POST['serialid']) && $_POST['serialid'] != "")
    {
        $serialid = $_POST['serialid'];
        $id_given = true;
    }

    if($id_given)
    {
        $get_record = mysql_query("SELECT * FROM products WHERE serial='$serialid'");
        $record = mysql_fetch_array($get_record);

        $output = '<form method="POST" enctype="multipart/form-data" action="update.php?serialid=' . $record['serialid'] . '&action=modify">

                    <table>
                    <tr>
                    <td>Name:</td>
                    <td><input name="name" type="text"  value="' . $record['name'] . '"/></td>
                    </tr>
                    <tr>
                    <td>Description :</td>
                    <td><textarea name="description" cols="45" rows="5">' . $record['description'] . '</textarea></td>
                    </tr>
                    <tr>
                    <td>Price:</td>
                    <td><input name="price" type="text"  value="' . $record['price'] . '"/></td>
                    </tr>
                    <td colspan="2"><img height="50" width="50" src="../images/' . $record['picture'] . '"/><br/>' . $record['picture'] . '</td>
                    </tr> 
                    <tr>
                    <td>Modify Image:</td>
                    <td><input name="picture" type="file" value="" /></td>
                    </tr>
                    <tr>
                    <td colspan="2"><input type="submit" value="Modify Record"/>
                    </td>
                    </tr>
                    </table>

</form>';

    }
    else
        $output = 'No record id was specified.';
?>
<html>
    <head>
        <title>Modify Record</title>
    </head>

    <body>
        <?php echo $output; ?>
    </body>
</html>
<?php
    // Disconnect from the database
?>

最佳答案

首先,此行中的 WHERE 之前有一个额外的逗号。 :

if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price', " . $update_image . " WHERE serial='$serialid'"))

正确的语法是:

if(mysql_query("UPDATE products SET name='$name', description='$description', price='$price' " . $update_image . " WHERE serial='$serialid'"))

然后你说

I can see them on url through the GET method

但是在您的脚本中您正在使用 $_POST变量来获取值,使用 $_GET相反,或将表单的方法更改为 post
如果你想上传图片,你必须使用 post方法,该文件将在$_FILES中可用变量。
在您的示例中,您通过 URL 传递参数,因此使用 get方法,“图片”只是您电脑中图片的路径,并没有上传到服务器上。

编辑:
添加"<input type='hidden' name='serialid' value='".$record['serialid']."' />""<input type='hidden' name='action' value='modify' />"在你的表单中,而不是将此参数添加到它的操作 URL 中,它应该可以工作

关于php - 用图像更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10879147/

相关文章:

php - PostgreSQL 重复行

php - 从数据库检索数据后如何操作

php - ACL 结构允许默认权限和第二组权限覆盖默认值

php - Android 使用 PHP 添加多个 MYSQL 行

mysql - 组合两个快速索引查询会使结果变慢

php - 可能的字符 base64 url​​ 安全功能

php - 多个账户的捐款总和(变量)

php - 在php中解析xml

mysql - Oracle:带有 IN 子句的参数化查询返回空值

mysql - zabbix 触发来自 2 个字段的 2 个值之间的差异