我注意到從mysqli過程樣式的輸入表單插入數據時,我新與mysqli所以當我從MySQL更改我的代碼到mysqli它有很多錯誤和通知,其中之一就像:未定義的變量:gambar在...(我本地的xampp目錄)在43行。數據和圖像被成功存儲到數據庫,但是當我不插入圖像時,它顯示通知。這是我的代碼:PHP未定義變量MYSQLI程序風格
<?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;
}
?>
任何幫助將是如此有幫助。由於
只要定義$ gamber ='';非常有利於你提取你的$ _POST變量。檢查並告訴發生了什麼? –
@anantkumarsingh再次顯示。 undefined index:gambar in ...... on line 11.我添加如下:$ gambar = $ _POST ['gambar'];先生,對嗎? –
請不要將mysqli_ *函數和mysqli_ *函數結合起來...... – Naruto