有人可以帮我这个表格吗? 我有一个包含3个INT列的表(danni,lenni,anders)。我希望这个表单在每次使用时都插入一个新行,其中包含我在字段中写入的数字。它确实插入了一个新行,但无论我输入什么数字,它只在表中插入'0'。
<?php
// Connection
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$danni = mysqli_real_escape_string($link, $_REQUEST['danni']);
$lenni = mysqli_real_escape_string($link, $_REQUEST['lenni']);
$anders = mysqli_real_escape_string($link, $_REQUEST['anders']);
// attempt insert query execution
$sql = "INSERT INTO ligabs3 (danni, lenni, anders) VALUES ('$danni', '$lenni', '$anders')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Records Form</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>
<label for="danni">Danni:</label>
<input type="text" name="danni" id="danni">
</p>
<p>
<label for="lenni">Lenni:</label>
<input type="text" name="lenni" id="lenni">
</p>
<p>
<label for="anders">Anders:</label>
<input type="text" name="anders" id="anders">
</p>
<input type="submit" value="Add Records">
</form>
</body>
</html>
答案 0 :(得分:0)
there are so many errors in your code. when you refresh page code will execute and insert in database. not after submit form. everytime on page refresh it will insert.
name is compulsory for submit button.
try this.
<?php
if($_POST['addrecord']){
// Connection
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$danni = mysqli_real_escape_string($link, $_REQUEST['danni']);
$lenni = mysqli_real_escape_string($link, $_REQUEST['lenni']);
$anders = mysqli_real_escape_string($link, $_REQUEST['anders']);
// attempt insert query execution
$sql = "INSERT INTO ligabs3 (danni, lenni, anders) VALUES ('$danni', '$lenni', '$anders')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Records Form</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>
<label for="danni">Danni:</label>
<input type="text" name="danni" id="danni">
</p>
<p>
<label for="lenni">Lenni:</label>
<input type="text" name="lenni" id="lenni">
</p>
<p>
<label for="anders">Anders:</label>
<input type="text" name="anders" id="anders">
</p>
<input type="submit" name="addrecord" value="Add Records">
</form>
</body>
</html>
答案 1 :(得分:0)
试试这段代码......
<?php
if($_POST['addNewRecords']){
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$danni = mysqli_real_escape_string($link, $_POST['danni']);
$lenni = mysqli_real_escape_string($link, $_POST['lenni']);
$anders = mysqli_real_escape_string($link, $_POST['anders']);
$sql = "INSERT INTO ligabs3 (danni, lenni, anders) VALUES ('$danni', '$lenni', '$anders')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Records Form</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<p>
<label for="danni">Danni:</label>
<input type="text" name="danni" id="danni">
</p>
<p>
<label for="lenni">Lenni:</label>
<input type="text" name="lenni" id="lenni">
</p>
<p>
<label for="anders">Anders:</label>
<input type="text" name="anders" id="anders">
</p>
<input type="submit" name="addNewRecords" value="Add Records">
</form>
</body>
</html>