【问题标题】:Insert NULL in mysql (blob) using PHP使用 PHP 在 mysql (blob) 中插入 NULL
【发布时间】:2014-05-05 18:54:25
【问题描述】:

我得到了下一个代码

if ($_FILES['intrebare_img']['size'] > 0) {
      $tmpName = $_FILES['intrebare_img']['tmp_name']; 
      $intrebare_img=addslashes(file_get_contents($tmpName));
}
else 
    $intrebare_img  = NULL;
if ($_FILES['opt1_img']['size'] > 0) {
      $tmpName = $_FILES['opt1_img']['tmp_name']; 
      $opt1_img  = addslashes(file_get_contents($tmpName));
}
else 
    $opt1_img = NULL;
if ($_FILES['opt2_img']['size'] > 0) {
      $tmpName = $_FILES['opt2_img']['tmp_name']; 
      $opt2_img  =  addslashes(file_get_contents($tmpName));
}
else
    $opt2_img = NULL;

if ($_FILES['opt3_img']['size'] > 0) {
      $tmpName = $_FILES['opt3_img']['tmp_name']; 
      $opt3_img  =  addslashes(file_get_contents($tmpName));
}
else 
    $opt3_img = NULL;
 $query = "INSERT INTO intrebari (intrebare_txt, intrebare_img, opt1_txt, opt1_img, opt2_txt, opt2_img, opt3_txt, opt3_img, raspuns_corect) 
                VALUES ('{$intrebare_txt}','{$intrebare_img}','{$opt1_txt}','{$opt1_img}','{$opt2_txt}','{$opt2_img}','{$opt3_txt}','{$opt3_img}','{$rsp_corect}')";
 mysql_query($query) or die("Error, query failed"); 

当执行它在数据库中插入值时,唯一的问题是即使没有选择文件,它也会插入 [BLOB - 0 B] 而不是 NULL。 我认为我的问题是由围绕 NULL 值的“”产生的,但无法解决。

提前致谢!

【问题讨论】:

  • 这可以通过准备好的语句和参数化查询轻松解决。那时你就不需要引号了。

标签: php mysql insert null blob


【解决方案1】:

明智的做法是使用 PDO 或类似的东西以及准备好的语句。让 PDO 完成繁重的工作,并将您的精力集中在解决其他问题上。它还可以保护您免受 SQL 注入漏洞的影响,您的代码似乎充满了这些漏洞。

如果你想坚持使用贬值的 mysql_ 函数,你需要检查每个变量并从 NULL 值中删除引号。大致如下:

if ( $opt3_img !== NULL ) {
    $opt3_img = "'".$opt3_img."'";  
}
$query = sprintf('INSERT INTO intrebari SET opt3_img = %s', $opt3_img);

您当然还希望将该功能放入一个函数中,以避免为每个变量重复该代码..

【讨论】:

  • 你的答案戴夫!我知道代码充满了漏洞,我会在它运行时添加mysql_real_escape_string。可悲的是,这是一个学校项目,我们只学到了 mysql_ 函数。
【解决方案2】:

最后,经过一番研究,我重写了代码,使其使用 PDO。 结果如下:

        $dbh = new PDO('mysql:dbname=dbname;host=127.0.0.1', user, password);
        if ($stmt = $dbh->prepare ("INSERT INTO intrebari (intrebare_txt, intrebare_img, opt1_txt, opt1_img, opt2_txt, opt2_img, opt3_txt, opt3_img, raspuns_corect) 
                                                            VALUES (:intrebare_txt, :intrebare_img, :opt1_txt, :opt1_img, :opt2_txt, :opt2_img, :opt3_txt, :opt3_img, :rsp_corect)")) 
        {
            $stmt -> bindParam(':intrebare_txt', $_POST['intrebare_txt']);
            $stmt -> bindParam(':opt1_txt', $_POST['opt1_txt']);
            $stmt -> bindParam(':opt2_txt', $_POST['opt2_txt']);
            $stmt -> bindParam(':opt3_txt', $_POST['opt3_txt']);
            if ( isset ($_POST['rsp_corect'])) 
                $stmt -> bindParam(':rsp_corect',$_POST['rsp_corect']);
            else 
                echo '<script type="text/javascript">alert("Nu a fost selectat un raspuns valid!");window.location=\'intrebari.php\';</script>';
            if ($_FILES['intrebare_img']['size'] > 0) {
                  $tmpName = $_FILES['intrebare_img']['tmp_name']; 
                  $stmt -> bindParam(':intrebare_img', file_get_contents($tmpName));
            }
            else 
                $stmt -> bindValue(':intrebare_img', NULL);
            if ($_FILES['opt1_img']['size'] > 0) {
                  $tmpName = $_FILES['opt1_img']['tmp_name']; 
                  $stmt -> bindParam(':opt1_img', file_get_contents($tmpName));
            }
            else 
                $stmt -> bindValue(':opt1_img', NULL);
            if ($_FILES['opt2_img']['size'] > 0) {
                  $tmpName = $_FILES['opt2_img']['tmp_name']; 
                  $stmt -> bindParam(':opt2_img', file_get_contents($tmpName));
            }
            else
                $stmt -> bindValue(':opt2_img', NULL);

            if ($_FILES['opt3_img']['size'] > 0) {
                  $tmpName = $_FILES['opt3_img']['tmp_name']; 
                  $stmt -> bindParam(':opt3_img', file_get_contents($tmpName));
            }
            else 
            $stmt -> bindValue(':opt3_img', NULL);
            $stmt -> execute();
            // close connection
            $stmt->closeCursor();
            $stmt = null; 
            $dbh = null;
            sleep(60);

            }
            else echo "STM FAILED !!";
}

由于本网站上另一个问题的误导性答案,我首先尝试使用 mysqli 连接,显然这是失败的! 对于其他查看代码的人,我必须提到的另一件事是在 ` userpassword 是定义的常量!

【讨论】:

    猜你喜欢
    • 2013-11-21
    • 2011-10-26
    • 2019-09-25
    • 1970-01-01
    • 2012-10-28
    • 2017-12-07
    • 2012-11-08
    • 1970-01-01
    • 2019-07-06
    相关资源
    最近更新 更多