目前,我试图在我的网站上创建“新闻模块”,首先向mysql数据库添加新闻,但它不起作用 - 点击按钮提交后没有任何反应 - 数据库中没有新数据。
<?php
$servername = "localhost";
$username = "u296093122_admin";
$password = ".";
$dbname = "u296093122_datab";
if(isset($add_n)){
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("->Pripojenie neúspešné: " . $conn->connect_error);
}
$sql = "INSERT INTO Akcie(nadpis, obsah, timestamp)VALUES('$nadpis', '$obsah', NOW())";
$result = $conn->query($sql);
if(!$result){
echo('Error ' . $mysql_error());
exit();
}else{
mysql_close($conn);
echo('Success!');
}
}else{
?>
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Nadpis:</td>
<td><input name="nadpis" type="text" id="nadpis"></td>
</tr>
<tr>
<td>Obsah novinky:</td>
<td><textarea name="obsah" id="obsah"></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="hiddenField" type="hidden" value="add_n">
<input name="add" type="submit" id="add" value="Submit">
</td>
</tr>
</table>
</form>
<? } ?>
答案 0 :(得分:0)
您应该使用isset($_POST['hiddenField'])
代替isset($add_n)
。最好的方式使用
if (isset($_POST['hiddenField']) && $_POST['hiddenField'] == 'add_n')