我注意到从输入表单插入带有mysqli程序样式的数据时,我使用mysqli,所以当我从mysql将代码更改为mysqli时,它有很多错误和注意事项,其中一个像:undefined variable:gambar in .. 。(我的本地xampp目录)在第43行。数据和图像成功存储到数据库,但是当我没有插入图像时,它会显示通知。这是我的代码:
<?php session_start();
include "../config.php";
include "judul_seo.php";
$id = mysql_real_escape_string($_POST['id']);
$judul_event = mysql_real_escape_string($_POST['judul_event']);
$judul_seo = judul_seo($judul_event);
$keywords = mysql_real_escape_string($_POST['keywords']);
$caption = mysql_real_escape_string($_POST['caption']);
$deskripsi = mysql_real_escape_string($_POST['deskripsi']);
if (empty($id))
{
die("Isikan ID!"); //Berhenti dan munculkan pesan jika ID tidak diisi
}
elseif(empty($judul_event))
{
die("Isikan Judul Event!");
}
else
{
if (!empty($_FILES["gambar"]["tmp_name"]))
{
$namafolder = "../images/event/"; //tempat menyimpan file
$jenis_gambar =$_FILES['gambar']['type'];
if($jenis_gambar=="image/jpeg" || $jenis_gambar=="image/jpg" || $jenis_gambar=="image/gif" || $jenis_gambar=="image/png")
{
$gambar = $namafolder . basename($_FILES['gambar']['name']);
if (!move_uploaded_file($_FILES['gambar']['tmp_name'], $gambar))
{
die("Gambar gagal dikirim");
}
}
else
{
die("Jenis gambar yang anda kirim salah. Harus .jpg .gif .png");
}
}
//end if cek file upload
$sql = "INSERT INTO eventbl(id,judul_event,judul_seo,keywords,caption,deskripsi,gambar)
VALUES ('$id','$judul_event','$judul_seo','$keywords','$caption','$deskripsi','$gambar')";
if (mysqli_query($conn, $sql))
{
echo "<script>alert('Insert data berhasil! Klik ok untuk melanjutkan');location.replace('event.php')</script>";
}
else
{
echo "Error updating record: " . mysqli_error($conn);
}
}
?>
judul_seo script:
<?php
function judul_seo($string)
{
$c = array (' ');
$d = array ('-','/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+');
$string = str_replace($d, '', $string);
$string = strtolower(str_replace($c, '-', $string));
return $string;
}
?>
任何帮助都会非常有帮助。感谢
答案 0 :(得分:2)
使用isset
函数检查文件是否已过帐
例如: -
if (isset($_FILES["gambar"]["tmp_name"]) && !empty($_FILES["gambar"]["tmp_name"])){}
希望这会对你有所帮助
答案 1 :(得分:1)
在这行语句之前定义一个空变量,如下所示:
$gambar = '';
if (empty($id))
{
die("Isikan ID!"); //Berhenti dan munculkan pesan jika ID tidak diisi
}