1 回答
TA贡献1831条经验 获得超9个赞
zapisk提交表单时必须调用函数。
<form method="post">
<input type="submit" name="zapis" id="zapisk" value="Запис" /><br/>
</form>
<?php
function zapisk(){
$servername = "localhost";
$username = "test";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
// Check form submitted and post has zapis value.
if(!empty($_POST['zapis'])){
zapisk();
}
?>
或者您不使用函数并在 if 语句中编写代码:
<form method="post">
<input type="submit" name="zapis" id="zapisk" value="Запис" /><br/>
</form>
<?php
// Check form submitted and post has zapis value.
if(!empty($_POST['zapis'])){
$servername = "localhost";
$username = "test";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报