简体   繁体   English

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

[英]Upload file to server, but file name does not save to database

Here is the issue: The file name is not being saved into the database. 这是问题所在:文件名未保存到数据库中。 Files are being uploaded to the server just fine, yet the file name will not save at all. 文件可以很好地上传到服务器,但是文件名根本不会保存。 I can echo the file name once the file is uploaded successfully but it does want to save the filename to the database for whatever reason. 文件成功上传后,我可以回显文件名,但是无论出于何种原因,它都确实希望将文件名保存到数据库中。 I'm sure this is an easy fix and I'm just missing something (I hope). 我敢肯定,这是一个简单的解决方法,并且我只是想念一些东西(我希望如此)。

Thanks in advance. 提前致谢。

(ps yes, I know I should be using mysqli) (ps是的,我知道我应该使用mysqli)

HTML: 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:

<?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.";
        }
    }
?>

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

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

run or die(mysql_error()); 运行或死亡(mysql_error());

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

maybe there is an error in the query, even if not syntax error it could be wrong field names. 也许查询中有错误,即使不是语法错误也可能是错误的字段名。 Other than that it seems fine as long as the variable $theloa is populated. 除此之外,只要填充了$ theloa变量,这似乎还不错。 mysql_error will tell if query is even being executed? mysql_error会告诉您查询是否正在执行?

A couple of things to consider: 需要考虑的几件事:

  1. mysql_* functions are deprecated. mysql_*函数已弃用。 Use mysqli or PDO instead. 改用mysqliPDO
  2. Your query can break if the variable $theloa has a single quote in it. 如果变量$theloa中包含单引号,则查询可能会中断。 Use prepared statements to prevent such errors and prevent (siren sounds and red lights flashing) SQL injection. 使用准备好的语句来防止此类错误并防止(警笛声和红灯闪烁) SQL注入。

Consider doing a var_dump($_FILES['loa']['name']) and pasting it in with your question for more clarity. 考虑做一个var_dump($_FILES['loa']['name'])并将其粘贴到您的问题中以更清楚。

Try using the following modified mysqli version of your file (you need to have the mysqli extension enabled in your PHP installation). 尝试使用文件的以下修改的mysqli版本(您需要在PHP安装中启用mysqli扩展名)。 It should (ideally) work. 它应该(理想地)工作。

<?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.";
        }
    }
?>

php values cannot be into quotes- (remove single quote from $loa) php值不能用引号引起来((从$ loa中删除单引号)

Try this : 尝试这个 :

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

Debugging 调试

$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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM