将文件上传到服务器,但文件名不保存到数据库

时间:2012-12-26 04:49:46

标签: php mysql

问题在于:文件名未保存到数据库中。文件正在上传到服务器,但文件名根本不会保存。文件上传成功后我可以回显文件名但是它确实想要将文件名保存到数据库中。我确信这是一个简单的解决方案,我只是缺少一些东西(我希望)。

提前致谢。

(p.s。是的,我知道我应该使用mysqli)

HTML:

<form action="" name="loa" method="post" enctype="multipart/form-data">
    <input type="hidden" name="size" value="350000">
    <input type="file" name="loa"> 
    <input type="submit" name="loasub" value="Upload Letter of     Authorization">
</form>

PHP:

<?php
    if (!empty($_POST['loasub'])) {
        $target = "loa/";
        $target = $target . basename( $_FILES['loa']['name']);
        $theloa = ($_FILES['loa']['name']);
        mysql_connect("localhost", "user", "pass") or die(mysql_error()) ;
        mysql_select_db("mydb") or die(mysql_error()) ;

        //Writes the information to the database
        mysql_query("UPDATE customers SET loa='$theloa' WHERE id='30'") ;
        if(move_uploaded_file($_FILES['loa']['tmp_name'], $target))
        {
            echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
            echo $theloa;
        }
        else {
            echo "Sorry, there was a problem uploading your file.";
        }
    }
?>

4 个答案:

答案 0 :(得分:1)

尝试像这样运行qurety-(从id = '30'删除单引号)

mysql_query("UPDATE customers SET loa='$theloa' WHERE id=30") ;

答案 1 :(得分:1)

 mysql_query("UPDATE customers SET loa='$theloa' WHERE id='30'") ;

运行或死(mysql_error());

 mysql_query("UPDATE customers SET loa='$theloa' WHERE id='30'") or die(mysql_error()) ;

可能在查询中存在错误,即使语法错误也可能是错误的字段名称。除此之外,只要变量$ theloa被填充就好了。 mysql_error会判断查询是否正在执行?

答案 2 :(得分:1)

需要考虑的几件事情:

  1. mysql_*函数已弃用。请改用mysqliPDO
  2. 如果变量$theloa中包含单引号,您的查询可能会中断。使用预准备语句来防止此类错误并防止(警报声和红灯闪烁) SQL注入。
  3. 考虑做一个var_dump($_FILES['loa']['name'])并将其粘贴到您的问题中以便更清晰。

    尝试使用以下修改过的mysqli版本的文件(您需要在PHP安装中启用mysqli扩展名)。它应该(理想情况下)起作用。

    <?php
        if (!empty($_POST['loasub'])) {
    
            $theloa = ($_FILES['loa']['name']);
            $target = "loa/";
            $target = $target . basename($theloa);
    
            $mysqli = new mysqli("localhost", "user", "password", "mydb");
            if (mysqli_connect_errno()) {
                die(mysqli_connect_error());
            }
    
    
            //Writes the information to the database
            if ($preparedStatement = $mysqli->prepare("UPDATE customers SET loa=? WHERE id=30")) {
                $preparedStatement->bind_param("s", $theloa);
                $executionResult = $preparedStatement->execute();
                if (!$executionResult) {
                    die("Query execution failed!");
                }
                //$preparedStatement->bind_result($sqlOutput); // bind mysql output to an output variable
                //$preparedStatement->fetch(); // fetch mysql output
                //var_dump($sqlOutput); // dump the sql output
    
                $preparedStatement->close();
            }
            $mysqli->close();
    
            if(move_uploaded_file($_FILES['loa']['tmp_name'], $target))
            {
                echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
                echo $theloa;
            }
            else {
                echo "Sorry, there was a problem uploading your file.";
            }
        }
    ?>
    

答案 3 :(得分:0)

php值不能成引号 - (从$ loa中删除单引号)

试试这个:

mysql_query("UPDATE customers SET loa=$theloa WHERE id='30'") ;

调试

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

$sql = mysql_query("UPDATE customers SET loa=$theloa WHERE id='30'") ;

mysql_select_db('customers ');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
?>