未定义的变量在pdo代码中

时间:2014-12-22 23:13:01

标签: php mysql pdo

我正在进行图片上传,但要上传错误 错误在变量$ novoNome中,此变量无法发送到PDO函数,该函数将在数据库中插入字段
但只发生在一些图像上,有些则没有。

让我们转到代码



if (isset($_POST['newpostimage'])) {

  if(empty($errors) === true){
    
    if(isset($_FILES['image']['name']) && $_FILES["image"]["error"] == 0)
    {

      $arquivo_tmp = $_FILES['image']['tmp_name'];
      $nome = $_FILES['image']['name'];

      $extensao = strrchr($nome, '.');

      $extensao = strtolower($extensao);
      if(strstr('.jpg;.jpeg;.gif;.png', $extensao))
      {
        
        $novoNome = md5(microtime()) . $extensao;

        $destino = 'uploads/postimages/' . $novoNome;

        if( @move_uploaded_file( $arquivo_tmp, $destino  ))
        {

        }
        else
          echo "Error saving the file. Apparently you do not have write permission.<br />";
      }
      else
        echo "You can only send files \"*.jpg;*.jpeg;*.gif;*.png\"<br />";

      }



    $user   = $userid;
    $date  = date('Y/m/d');
    $time   = date('H:i:s');
    $posttype = "image";
    $post  = $_POST['mypost'];
    $linkimg = $novoNome;

    $users->newpostimage($user, $date, $time, $posttype, $post, $linkimg);
    header('Location: /home');
    exit();

  }
}
&#13;
&#13;
&#13;

这是函数newpostimage

&#13;
&#13;
public function newpostimage($user, $date, $time, $posttype, $post, $linkimg){
		
		$query 	= $this->db->prepare("INSERT INTO `post` (`userid`, `date`, `time`, `post`, `posttype`, `image`) VALUES (?, ?, ?, ?, ?, ?)");

		$query->bindValue(1, $user);
		$query->bindValue(2, $date);
		$query->bindValue(3, $time);
		$query->bindValue(4, $post);
		$query->bindValue(5, $posttype);
		$query->bindValue(6, $linkimg);


		try{
			$query->execute();

		}catch(PDOException $e){
			die($e->getMessage());
		}

	}
&#13;
&#13;
&#13;

错误是:

  

注意:未定义的变量:第99行的C:\ xampp \ htdocs \ home.php中的novoNome   SQLSTATE [23000]:完整性约束违规:1048列&#39;图像&#39;不能为空

2 个答案:

答案 0 :(得分:1)

您应该做的第一件事是将newpostimage调用移动到最里面的if子句中。否则,检查没有多大意义,代码总是被执行 - 这也是错误的原因。

if (isset($_POST['newpostimage'])) 
{
    if(empty($errors) === true)
    {
        if(isset($_FILES['image']['name']) && $_FILES["image"]["error"] == 0)
        {

            $arquivo_tmp = $_FILES['image']['tmp_name'];
            $nome = $_FILES['image']['name'];

            $extensao = strrchr($nome, '.');

            $extensao = strtolower($extensao);
            if(strstr('.jpg;.jpeg;.gif;.png', $extensao))
            {
                $novoNome = md5(microtime()) . $extensao;
                $destino = 'uploads/postimages/' . $novoNome;

                if( @move_uploaded_file( $arquivo_tmp, $destino  ))
                {
                    $user   = $userid;
                    $date  = date('Y/m/d');
                    $time   = date('H:i:s');
                    $posttype = "image";
                    $post  = $_POST['mypost'];
                    $linkimg = $novoNome;

                    $users->newpostimage($user, $date, $time, $posttype, $post, $linkimg);
                    header('Location: /home');
                    exit();
                }
                else 
                {
                    echo "Error saving the file. Apparently you do not have write permission.<br />";
                }
            else 
            {
                echo "You can only send files \"*.jpg;*.jpeg;*.gif;*.png\"<br />";
            }
        }
    }
}

答案 1 :(得分:0)

当您上传错误扩展名的图片(if(strstr('.jpg;.jpeg;.gif;.png', $extensao)))时,您的$novoNome变量将是未定义的,因此您在PDO中的'image'参数将为null,我认为您的数据库架构不会t允许该字段为空。