我正在尝试在mysql数据库中创建一个订阅条目。每次我提交表单时,都会创建一个新条目,而电子邮件地址或输入值不会出现在数据库中。 请找出问题所在。
<?php
include 'dbh.php';
$subs=$_POST['$sub_email']:
$sql="INSERT INTO subscribe(sub) VALUES('$subs')";
$result=mysqli_query($conect,$sql);
header("Location:index.html");
?>
<form class="form" method="post" action="subscribe.php" style="margin-top: 20px">
<div class="input-group">
<div class="input-group-addon">@</div>
<input type="email" class="form-control" name="subs_email" id="inlineFormInputGroup" placeholder="Your Email">
</div>
<button type="submit" class="btn btn-success btn-lg" style="margin-top:5px " >Submit</button>
</form>
答案 0 :(得分:1)
请注意,您的HTML输入具有属性name="subs_email"
因此,您的$_POST
数组索引应该是subs_email
而不是$subs_email
$subs=$_POST['subs_email']:
答案 1 :(得分:0)
如果电子邮件ID不存在,请尝试以下代码插入:
<?php
include 'dbh.php';
if(isset($_POST['subs_email'])) {
$subs = mysqli_real_escape_string($conect, $_POST['subs_email']); //escape variables for security
// insert if email id not exist
$result = mysqli_query($conect,"SELECT `sub` FROM `subscribe` WHERE `sub` = '".$subs."' LIMIT 1");
if (mysqli_num_rows($result) <= 0) { {
$sql = "INSERT INTO subscribe (sub) VALUES('".$subs."')";
$result = mysqli_query($conect,$sql);
}
header("Location:index.html");
}
?>
<form class="form" method="post" action="subscribe.php" style="margin-top: 20px">
<div class="input-group">
<div class="input-group-addon">@</div>
<input type="email" class="form-control" name="subs_email" id="inlineFormInputGroup" placeholder="Your Email">
</div>
<button type="submit" class="btn btn-success btn-lg" style="margin-top:5px " >Submit</button>
</form> **strong text**