0

这是问题:文件名没有保存到数据库中。文件正在上传到服务器就好了,但文件名根本不会保存。文件成功上传后,我可以回显文件名,但无论出于何种原因,它确实希望将文件名保存到数据库中。我确信这是一个简单的解决方法,我只是遗漏了一些东西(我希望如此)。

提前致谢。

(ps是的,我知道我应该使用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

4 回答 4

1

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

mysql_query("UPDATE customers SET loa='$theloa' WHERE id=30") ;
于 2012-12-26T04:54:21.630 回答
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 会告诉查询是否正在执行?

于 2012-12-26T05:23:01.543 回答
1

有几点需要考虑:

  1. mysql_*功能已弃用。使用mysqliPDO代替。
  2. $theloa如果变量中包含单引号,您的查询可能会中断。使用准备好的语句来防止此类错误并防止(警报声和红灯闪烁)SQL 注入。

考虑做 avar_dump($_FILES['loa']['name'])并将其与您的问题一起粘贴以更清晰。

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

<?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.";
        }
    }
?>
于 2012-12-26T05:28:28.927 回答
0

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

试试这个 :

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

调试

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

$sql = mysql_query("更新客户 SET loa=$theloa WHERE id='30'") ;

mysql_select_db('客户');
$retval = mysql_query($sql, $conn);
如果(!$retval )
{
  die('无法更新数据:' . mysql_error());
}
echo "数据更新成功\n";
mysql_close($conn);
?>
于 2012-12-26T05:17:05.447 回答