更新数据库中的图像路径并在div中显示它们

时间:2013-11-20 13:42:11

标签: php image checkbox

这是我上传的图片代码。我实际上可以上传并查看:

$filename = $_FILES['upload_file']['name'].$SN;
$target_path ="uploads/".basename($filename);

if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $target_path))
{
    echo "The file <b>".basename($filename)."</b> "."has been Uploaded.<br>";
    $result = $target_path ;

    echo "Upload: " . $_FILES["upload_file"]["name"] . "<br>";
    echo "Type: " . $_FILES["upload_file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["upload_file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["upload_file"]["tmp_name"]."<br/>";
    echo $result;
    echo "<br/><img src='".$result."'"." alt=\"Image\" width=\"150px\" border=\"1\" />";
    echo "<form method=\"post\" name=\"myform\"><input type=\"checkbox\" name=\"agree_avatar\"  onchange=\"myform.submit()\"></form>";

    if (!isset( $_POST['agree_avatar']) )
    {
        echo "Select As Avatar";
    }

直到这里我可以看到echo "SELECT As Avatar",但是当我选中复选框时,它没有更新数据库,它给我一个There was an error uploading the file please try again!$result是图像路径的错误上传了我想将它保存在数据库字段“avatar”中,以便稍后随时调用它。) P.S(我知道它缺乏安全检查,但之后我会继续研究)

    if(isset($_POST['agree_avatar']))
    {   
        $sql = 'UPDATE register SET avatar="' . $result . '"' . ' WHERE SN="' . $SN . '"' . 'LIMIT 1';
        $run = mysql_query($sql);

        if(!$run)
        {
            echo "DUH!<br/>";
        }
        else
        {
            echo "Avatar selected ";
        }
} // end if from the beggining
else 
{
echo "There was an error uploading the file please try again!";
}

1 个答案:

答案 0 :(得分:0)

我解决了自己的问题 有一件事$ result没有在UPdate查询中获得任何值 我改变了我的脚本,结果我能够在数据库中增加价值 改变后的脚本在

下面
       echo "<form method=\"GET\" name=\"myform\"><input type=\"checkbox\" name=\"agree\"  onchange=\"myform.submit()\"><input type=\"hidden\" name=\"filename\" value=".$_FILES["upload_file"]['name'].$SN." ></form>";

        }

        $filename = $_GET['filename'];
        $target_path ="uploads/". basename( $filename );
        if(isset($_GET['agree']))
        {

        $result = $target_path;
        $sql = 'UPDATE register SET avatar="'.$result.'"'.' WHERE email="'.$user.'"'.' LIMIT 1';
        $run = mysql_query($sql) or  die(mysql_errno());
            if($run)
            {
            echo "Avatar selected" ;

            }
        }