我有问题,我从php连接phpmyadmin数据库。 以下代码适用于表单,
<div id="wb_element_instance53" class="wb_element">
<form class="wb_form wb_mob_form" method="post"><input type="hidden" name="wb_form_id" value="18498be5"><textarea name="message" rows="3" cols="20" class="hpc"></textarea>
<table>
<tr>
<th class="wb-stl-normal">Name </th>
<td><input type="hidden" name="wb_input_0" value="Name"><input class="form-control form-field" type="text" value="" name="wb_input_0" required="required"></td>
</tr>
<tr>
<th class="wb-stl-normal">Email </th>
<td><input type="hidden" name="wb_input_1" value="E-mail"><input class="form-control form-field" type="text" value="" name="wb_input_1" required="required"></td>
</tr>
<tr class="area-row">
<th class="wb-stl-normal">Message </th>
<td><input type="hidden" name="wb_input_2" value="Message"><textarea class="form-control form-field form-area-field" rows="3" cols="20" name="wb_input_2" required="required"></textarea></td>
</tr>
<tr class="form-footer">
<td colspan="2"><button type="submit" class="btn btn-default">Submit</button></td>
</tr>
</table>
</form>
<script type="text/javascript">
然后,我尝试使用下面的PHP代码连接phpmyadmin数据库,
<?php
/*
$connect=mysqli_connect('localhost','root','','Contact_db') ;
if(mysqli_connect_errno($connect))
{
echo 'Failed to connect';
}
// create a variable
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";
//Execute the query
mysqli_query($connect,$sql);
?>
但是,上面显示错误:
Notice: Undefined variable: name in this line "$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";"
Notice: Undefined variable: email in in this line "$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";"
Notice: Undefined variable: message in in this line "$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";"
答案 0 :(得分:3)
如果isset()失败怎么办? 修理: 只有在设置了参数时才有$ sql ..
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message']) ){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES ('$name','$email,'$message')";
//Execute the query
mysqli_query($connect,$sql);
}
答案 1 :(得分:3)
问题是$ _POST搜索输入的名称。你的名字是wb_input_0,试试这个:
if (isset($_POST['wb_input_0'])) {
$name = $_POST['wb_input_0'];
}
电子邮件和消息也一样。但是,我不建议将输入命名为
答案 2 :(得分:0)
试试这个:
$email ='';
$name ='' ;
$message ='';
print_r($_POST);//to review is all vars in form.
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
答案 3 :(得分:0)
if (isset($_POST['name'])) {
$name = $_POST['name'];
}else{
$name = '';
}
if (isset($_POST['email'])) {
$email = $_POST['email'];
}else{
$email = '';
}
if (isset($_POST['message'])) {
$message = $_POST['message'];
}else{
$message = '';
}
答案 4 :(得分:0)
帮自己一个忙,准备好你的陈述:
$sql ="INSERT INTO contac_ds ('Name','email','message') VALUES (?,?,?)";
$stmt = mysqli_prepare($connect, $sql);
$name="";
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
$email="";
if (isset($_POST['email'])) {
$email = $_POST['email'];
}
$message="";
if (isset($_POST['message'])) {
$message = $_POST['message'];
}
mysqli_stmt_bind_param($stmt,"sss",$name,$email,$message);
mysqli_stmt_execute($stmt);
请注意,您当前的$_POST
不会包含这些字段,因为您对它们的命名方式不同(两次),因此您还需要修复它。