我尝试使用此代码将我的数据从模式插入到数据库中。
if (isset($_POST['user_tambah'])) {
$mitra = $_POST['mitra_input'];
$username = $_POST['username_input'];
$password = $_POST['password_input'];
$privilege = $_POST['privilege_input'];
$query2 = "INSERT INTO user (name,username, password, privilege) values (? ,?, ?,?)";
$stmt2 = $connection->prepare($query2);
$stmt2->bind_param("sssi", $mitra, $username, $password, $privilege);
$stmt2->execute();
if ($stmt2->affected_rows) {
echo "Deleted ".$stmt->affected_rows." rows";
?>
<div class="alert alert-success" role="alert">Berhasil menambah user</div>
<?php
header("Location: user.php");
} else {
echo "No rows matched the criteria.";
?>
<div class="alert alert-danger" role="alert">Gagal menambah user</div>
<?php
}
$stmt2->close();
}
从此
<div class="modal fade" id="plusModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Tambah User</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" action="" id="insert-user-form" novalidate>
<div class="form-group">
<label for="inputName">Mitra</label>
<input name="mitra_input" type="text" class="form-control" id="inputName" placeholder="Nama perusahaan" required>
</div>
<div class="form-group">
<label for="inputEmail1">Username</label>
<input name="username_input" type="email" class="form-control" id="inputEmail1" aria-describedby="emailHelp" placeholder="Username untuk masuk" required>
</div>
<div class="form-group">
<label for="inputPassword1">Password</label>
<input name="password_input" type="password" class="form-control" id="inputPassword1" placeholder="Password untuk masuk" required>
</div>
<div class="form-group">
<label for="formControlSelect1">Privilege</label>
<select name="privilege_input" class="form-control" id="formControlSelect1">
<option value="2">User</option>
<option value="1">Admin</option>
</select>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<button class="btn btn-primary" type="submit" name="user_tambah" form="insert-user-form">Tambah</button>
</div>
</div>
</div>
</div>
但是如果我单击按钮,它什么也不做。它甚至没有错误。我也尝试了使用模式删除的相同方法,并且效果很好,但是如果尝试这样插入,它将不会单击。如果我的代码有任何问题,请帮忙?
答案 0 :(得分:0)
您尚未在表单范围内提交按钮。 试试这个html标记:
<div class="modal fade" id="plusModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Tambah User</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" action="" id="insert-user-form" novalidate>
<div class="form-group">
<label for="inputName">Mitra</label>
<input name="mitra_input" type="text" class="form-control" id="inputName" placeholder="Nama perusahaan" required>
</div>
<div class="form-group">
<label for="inputEmail1">Username</label>
<input name="username_input" type="email" class="form-control" id="inputEmail1" aria-describedby="emailHelp" placeholder="Username untuk masuk" required>
</div>
<div class="form-group">
<label for="inputPassword1">Password</label>
<input name="password_input" type="password" class="form-control" id="inputPassword1" placeholder="Password untuk masuk" required>
</div>
<div class="form-group">
<label for="formControlSelect1">Privilege</label>
<select name="privilege_input" class="form-control" id="formControlSelect1">
<option value="2">User</option>
<option value="1">Admin</option>
</select>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<button class="btn btn-primary" type="submit" name="user_tambah" form="insert-user-form">Tambah</button>
</div>
</form>
</div>
</div>
</div>
</div>