正如标题所说,我无法在MySQL中插入联系表单(使用Bootstrap)(我使用mysqli)。下面是我的PHP脚本:
<?php
$conn = mysqli_connect('localhost', 'root', 'mypassword', 'mydbname');
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if(isset($_POST['submit'])){
$sql = mysqli_query($conn, "INSERT INTO `contacts` (`firstName`, `lastName`, `dayOfBirth`, `phone`, `gender`, `email`) VALUES('".$_POST['firstName']."', '".$_POST['lastName']."', '".$_POST['birthday']."', '".$_POST['phone']."', '', '".$_POST['email']."')");
}
mysqli_close($conn);
?>
以下是我使用bootstrap的联系表格:
<div class="container">
<div class="row">
<h2>Contact Form</h2>
<form class="form-horizontal" method="POST">
<div class="form-group">
<label class="control-label col-xs-2">First Name:</label>
<div class="col-xs-10">
<input type="text" class="form-control" name="firstName" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Last Name:</label>
<div class="col-xs-10">
<input type="text" class="form-control" name="lastName" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">DoB:</label>
<div class="col-sm-6 col-md-2">
<div class="input-group date" id="birthday">
<input type="text" class="form-control" name="birthday">
<span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Phone/Mobile:</label>
<div class="col-xs-10">
<input type="text" class="form-control" name="phone" placeholder="Phone/Mobile">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Gender:</label>
<div class="col-xs-10">
<label class="radio-inline">
<input type="radio" name="gender" value="M">
Male</label>
<label class="radio-inline">
<input type="radio" name="gender" value="F">
Female</label>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Email:</label>
<div class="col-xs-10">
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<button type="submit" class="btn btn-primary" name="submit" value="Submit">Submit</button>
</div>
</div>
</form>
</div>
</div>
似乎$conn
没有问题,我可以无错误地连接到数据库,但查询可能有问题。当我提交时,没有数据插入数据库,但是,它不会显示错误。
此外,如果需要,db中的所有列都被允许NULL
,我还有1个名为id
的列( auto_increment ),并提醒,我想要插入phpmyadmin,而不是mysql。任何人都可以帮我找出问题所在吗?
P / s:我使用wampserver,如果它确实重要。
P / s 2:发现错误!!!
答案 0 :(得分:0)
确保您确实已连接到数据库。然后将action属性添加到引导程序表单中。
<form class="form-horizontal" action="page where insert code happens" method="POST">
答案 1 :(得分:0)
您在查询代码中犯了一些语法错误。试试这个......工作!!
另外,请注意您正在插入MYSQL,它是WAMP或XAMP堆栈的一部分。 PHPMYADMIN是管理数据库的管理部分。
<?php
$conn = mysqli_connect('localhost', 'root', '', 'db_sandbox');
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
if(isset($_POST['submit'])){
$firsname = $_POST['firstName'];
$lastName = $_POST['lastName'];
$birthday = $_POST['birthday'];
$phone = $_POST['phone'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$query = "INSERT INTO contacts (firstName, lastName, dayOfBirth, phone, gender, email) VALUES('".$firsname."', '".$lastName."', ".$birthday.", ".$phone.", '".$gender."', '".$email."')";
$sql = mysqli_query($conn, $query);
}
mysqli_close($conn);
?>
<div class="container">
<div class="row">
<h2>Contact Form</h2>
<form class="form-horizontal" method="POST" action="index.php">
<div class="form-group">
<label class="control-label col-xs-2">First Name:</label>
<div class="col-xs-10">
<input type="text" class="form-control" name="firstName" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Last Name:</label>
<div class="col-xs-10">
<input type="text" class="form-control" name="lastName" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">DoB:</label>
<div class="col-sm-6 col-md-2">
<div class="input-group date" id="birthday">
<input type="text" class="form-control" name="birthday">
<span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Phone/Mobile:</label>
<div class="col-xs-10">
<input type="text" class="form-control" name="phone" placeholder="Phone/Mobile">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Gender:</label>
<div class="col-xs-10">
<label class="radio-inline">
<input type="radio" name="gender" value="M">
Male</label>
<label class="radio-inline">
<input type="radio" name="gender" value="F">
Female</label>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Email:</label>
<div class="col-xs-10">
<input type="email" class="form-control" name="email" placeholder="Email">
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<button type="submit" class="btn btn-primary" name="submit" value="Submit">Submit</button>
</div>
</div>
</form>
</div>
</div>