• PHP Help General Help
  • [RESOLVED] Notice: Undefined variable: pesan in C:\xampp\htdocs\slkweb\guestbook_save.php on lin

hallo all...
i have get error when i try to save data:

Notice: Undefined variable: pesan in C:\xampp\htdocs\slkweb\guestbook_save.php on line 30

The code for the page is as follows:

<?php
$txtnama = isset($_POST['txtnama']);
$txtbagian = isset($_POST['txtbagian']);
$txtemail = isset($_POST['txtemail']);
$txtsaran = isset($_POST['txtsaran']);

if (trim($txtnama) =="") {
	$pesan[] = " Data Nama masih kosong";
	}
if (trim($txtbagian) =="") {
	$pesan[] = " Data Bagian masih kosong";
	}
if (trim($txtemail) =="") {
	$pesan[] = " Data e-mail masih kosong";
	}
if (trim($txtsaran) =="") {
	$pesan[] = " Data Kritik/saran masih kosong";
	}

if (! count($pesan)==0){
	$urut_pesan =0;
	echo " <b> Error input data : </b><br>";
	foreach ($pesan as $indeks=>$pesan_tampil){
		$urut_pesan++;
		echo"<font color='#ff0000'>";
		echo"$urut_pesan. $pesan_tampil <br>";
		echo"</font>";
		}
	}
else {
	include_once"library\inc.koneksi.php";
	$tgl_isi=date('y-m-d');
	$sql = "INSERT INTO tguestbook(id_gbook,nama,bagian,email,komentar,tanggal)
			VALUES ('','$txtnama','$txtbagian','$txtemail','$txtsaran','$tgl_isi')";

mysql_query($sql, $koneksi)
	or die ("gagal menyimpan buku tamu".mysql_error());

echo"<br><br><br>";
echo" pesan telah berhasil disimpan";
echo"<a href=''index.php?page=gbview'>Tampilkan</a>";
exit;
}
?>

does anybody have solution for this error??
thanks for advance..

    Initialize $pesan as an empty array right after you perform all of your isset() checks.

    $txtsaran = isset($_POST['txtsaran']);
    $pesan = array();  // <--- Here
    if (trim($txtnama) =="") {
    

      Another issue with your script is this section:

      $txtnama = isset($_POST['txtnama']); 
      $txtbagian = isset($_POST['txtbagian']); 
      $txtemail = isset($_POST['txtemail']); 
      $txtsaran = isset($_POST['txtsaran']); 

      Note that the variables $txtnama, etc. are all going to be boolean values, e.g. either TRUE or FALSE - whatever [man]isset/man returns. The way you use those variables, however, suggests that you actually want to store the user's input in those variables instead.

        @: thanks for your solution, i add initial line and error message was fixed. @: yes, i've got that issue, after i save the data from user input, when data displayed, content of data only '1' whatever user input. can you give me some solution to fix it? thanks

          10 months later
          Write a Reply...