I'm doing an image upload but to upload the error.
The error is in the variable $novoNome, this variable can not be sent to the PDO function, which will insert fields in the database
But only happens on some images, some work some do not.
let's go to the code
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();
}
}
This is the function newpostimage
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());
}
}
The error is:
Notice: Undefined variable: novoNome in C:\xampp\htdocs\home.php on line 99 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null