Notice: Undefined index: tombol in C:\xampp\htdocs\pw-liana\submitcomment.php on line 2
Notice: Undefined index: nama in C:\xampp\htdocs\pw-liana\submitcomment.php on line 25
与nama(第25行),电子邮件(第25行),网站(第25行),komentar(第25行),art_id(第26行),art_url(第26行)的通知相同。 但最后的通知是
Notice: Undefined variable: art_url in C:\xampp\htdocs\pw-liana\submitcomment.php on line 31
我的PHP代码
<?php
if ($_POST["tombol"]=="Kirim")
{
$nama=$_POST["nama"];
$email=$_POST["email"];
$website=$_POST["website"];
$komentar=$_POST["komentar"];
$art_id=$_POST["art_id"];
$art_url=$_POST["art_url"];
if(empty($nama))
$_POST["nama"]='anonymous';
if(empty($komentar)){
echo "<meta http-equiv='refresh' content='2; url=$art_url'>";
die("komentar harus diisi");}
}
//connect database
$con=mysql_connect("localhost", "root", "");
if(!$con)
die("Tidak dapat melakukan koneksi ke server MySQL");
//Menampilkan data
mysql_select_db("db_web", $con);
$sql="INSERT INTO comment (nama, email, website, komentar, art_id, art_url,
date)
VALUES
('$_POST[nama]','$_POST[email]','$_POST[website]', '$_POST[komentar]',
'$_POST[art_id]', '$_POST[art_url]', NOW())";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "<meta http-equiv='refresh' content='0; url=$art_url'>";
//Memutuskan koneksi
mysql_close($con);
?>
我的HTML代码
<div id="publishcomment">
<?php include("publishcomment.php"); getcomment("1");
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?></div>
<div>
<form name="submitcomment" method="post" action="submitcomment.php">Nama:<br>
<input name="nama" type="text"><br>
Email(optional):<br><input name="email" type="email"><br>
Website(optional):<br><input name="website" type="text"><br>
Komentar:<br><textarea name="komentar" rows="6" cols="50"></textarea><br>
<input name="art_id" value="1" type="hidden">
<input name="art_url" value="" type="hidden"><br>
<input name="tombol" value="Kirim" type="submit">
<input value="Reset" type="Reset">
</form></div>
</article>
</div>
点击提交按钮
时收到错误答案 0 :(得分:0)
问题是,“全局”数组$ _POST没有tombol索引。在第一步中,您应该检查索引是否已设置。然后检查该值是否是您预期的值。
另外我会使用mysqli而不是mysql。 MySQL(不含i)已被弃用。
我已更新您的代码
编辑:代码未经过测试!
此外,我会尝试在PHP中完成所有操作,而无需一直进行重定向。如果不可能使用PHP函数头('Location:URL');代替
EDIT2 :这是一个更新的代码。试试这个吧。仍未经测试,只是Dreamweaver检查语法错误。
<?php
if (isset($_POST["tombol"]))
{
if($_POST["tombol"] == "Kirim")
{
$nama = (isset($_POST["nama"])) ? $_POST["nama"] : 'anonymous';
$email = (isset($_POST["email"])) ? $_POST["email"] : '' ;
$website = (isset($_POST["website"])) ? $_POST["website"] : '' ;
$komentar = (isset($_POST["komentar"])) ? $_POST["komentar"] : '' ;
$art_id = (isset($_POST["art_id"])) ? $_POST["art_id"] : '' ;
$art_url = (isset($_POST["art_url"])) ? $_POST["art_url"] : '' ;
if(empty($komentar))
{
echo '<div class="error">Comment is empty</div>';
}
else
{
//connect database
$con = mysqli_connect("localhost", "root", "", "db_web");
if(!$con)
{
echo '<div class="error">SQL connection Error</div>';
}
else
{
//Menampilkan data
$sql = 'INSERT INTO comment (nama, email, website, komentar, art_id, art_url, date)
VALUES
("'.mysqli_real_escape_string($con,$nama).'","'.mysqli_real_escape_string($con,$email).'","'.mysqli_real_escape_string($con,$website).'", "'.mysqli_real_escape_string($con,$komentar).'", '.$art_id.', "'.mysqli_real_escape_string($con,$art_url).'", NOW())';
if (!mysqli_query($con,$sql))
{
echo '<div class="error">Error: ' . mysqli_error($con).'</div>';
}
//Memutuskan koneksi
mysqli_close($con);
}
}
}
else
{
echo '<div class="error">The comment is not "Kirim"</div>';
}
}
?>
<div id="publishcomment">
<?php include("publishcomment.php"); getcomment("1");
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
</div>
<div>
<form name="submitcomment" method="post" action="<?php echo $actual_link; ?>">Nama:<br>
<input name="nama" type="text"><br>
Email(optional):<br><input name="email" type="email"><br>
Website(optional):<br><input name="website" type="text"><br>
Komentar:<br><textarea name="komentar" rows="6" cols="50"></textarea><br>
<input name="art_id" value="1" type="hidden">
<input name="art_url" value="" type="hidden"><br>
<input name="tombol" value="Kirim" type="submit">
<input value="Reset" type="Reset">
</form>
</div>
</div>