我正在尝试将数据从表单上传到MySQL数据库,但它无法正常工作。
它只是停留在那里并且什么也不做,它不会插入到数据库中。
请查看下面的代码。
<?php
if(isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = "INSERT INTO users (";
$query .= " first_name, last_name, username, password";
$query .= ") VALUES (";
$query .= " '{$first_name}', '{$last_name}', '{$username}', '{$password}'";
$query .= ")";
$result = mysqli_query($connection, $query);
if($result) {
echo "It worked";
} else {
echo "It did not work";
}
}
?>
这是我的HTML
<div class="col-md-8 col-xs-12">
<div class="white-box">
<form class="form-horizontal form-material" method="post">
<div class="form-group">
<label class="col-md-12">First Name</label>
<div class="col-md-12">
<input type="text" placeholder="John" name="first_name" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Last Name</label>
<div class="col-md-12">
<input type="text" placeholder="Doe" name="last_name" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Username</label>
<div class="col-md-12">
<input type="text" placeholder="Username" name="username" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<label class="col-md-12">Password</label>
<div class="col-md-12">
<input type="password" placeholder="Password" name="password" class="form-control form-control-line"> </div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button class="btn btn-success">Create User</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
由于您正在检查isset($ _ POST [&#34; submit&#34;]),您需要在html表单中添加类型为submit的输入标记。
替换
<button class="btn btn-success">Create User</button>
带
<input class="btn btn-success" type="submit" name="submit" value="Create User">
答案 1 :(得分:0)
<?php
if (isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$username = $_POST['username'];
$password = ($_POST['password']);
$query=mysqli_query($conn,"INSERT INTO users(first_name, last_name, username, password) VALUES ('$first_name','$last_name','$username','$password')");
if(!$query){
die('could not enter data:'. mysqli_error($conn));
}
else{
echo "Entered data successfully";
}
}
?>
答案 2 :(得分:0)
<?php
if(isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$username = $_POST['username'];
$password = md5($_POST['password']);
$query = "INSERT INTO users (first_name, last_name, username, password) VALUES ('$first_name','$last_name','$username','$password')";
$result = mysqli_query($connection, $query);
if($result) {
echo "It worked";
} else {
echo "It did not work";
}
}
?>
答案 3 :(得分:0)
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
First Name
<input type="text" name="first_name" >
Last Name
<input type="text" name="last_name" class="form-control form-control-line">
Username
<input type="text" placeholder="Username" name="username" >
Password
<input type="password" placeholder="Password" name="password" >
<input type="submit" name="update" value="create user">
</form>
</body>
</html>
<?php
$host = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($host,$username,$password);
mysqli_select_db($conn,'db'); //database name is "db"
if (!$conn){
die("could not create connection");
}
else
echo"conection made <br>";
if (isset($_POST['update'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$username = $_POST['username'];
$password = ($_POST['password']);
$query=mysqli_query($conn,"INSERT INTO users(first_name, last_name, username, password) VALUES ('$first_name','$last_name','$username','$password')");
if(!$query){
die('could not enter data:'. mysqli_error($conn));
}
else{
echo "Entered data successfully";
}
}
?>