我正在尝试编写将按钮或输入提交时将数据插入数据库的代码。当前该按钮不起作用,当我进入页面或重新加载页面时,空的数据输入将发送到数据库。
我一直在搜寻和解决堆栈溢出的问题以及可能的解决方案达数小时之久,但是到目前为止,它们都没有起作用。
php:
stristr
HTML:
$servername = "";
$username = "";
$password = "";
$dbname = "";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$ma1 = mysqli_real_escape_string($conn, $_REQUEST['ma1']);
$sql = "INSERT INTO workers (ma1) VALUES ('$ma1')";
if(mysqli_query($conn, $sql)){
echo "Records added successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
mysqli_close($conn);
我希望代码仅在按输入提交时才将数据发送到数据库。将用户重定向到另一个页面不是一种选择,并且不能解决问题。
答案 0 :(得分:-1)
HTML:
<form method="post" action="">
<input name="ma1" type="text">
<input type="submit" value="Submit" name="submit">
</form>
PHP:
$servername = "";
$username = "";
$password = "";
$dbname = "";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(isset($_POST['submit'])) {
$ma1 = mysqli_real_escape_string($conn, $_REQUEST['ma1']);
$sql = "INSERT INTO workers (ma1) VALUES ('$ma1')";
if(mysqli_query($conn, $sql)){
echo "Records added successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
}
mysqli_close($conn);