gpt4 book ai didi

php - 从 HTML 表单向 SQL 数据库插入行时出现问题。我究竟做错了什么?我对此还很陌生

转载 作者:行者123 更新时间:2023-11-29 09:50:04 25 4
gpt4 key购买 nike

这是我的 php 文件。我们的教练让我们这样输入函数。我们最初在一个单独的页面上将该函数调用到 php 文件中,但我认为没有理由以这种方式包含它。

<?php
function insertFlight($flights)
{
$sql = "INSERT INTO `flights` (`FlyingFrom`, `FlyingTo`, `DepartureDate`, `ReturnDate`, `TravelClass`, `Adults`, `Children`) VALUES ('', '', '', '', '', '', NULL)";
$dbh = mysqli_connect("localhost", "fred", "password", "travelexperts");
if (! $dbh)
{
die ("Error: " . mysqli_connect_errno() . " - " . mysqli_connect_error());
}
$stmt = mysqli_prepare($dbh, $sql);
if (! $stmt)
{
die ("Error: " . mysqli_error($dbh));
}
mysqli_stmt_bind_param($stmt, "sssssii", $flights['FlyingFrom'], $flights['FlyingTo'], $flights['DepartureDate'], $flights['ReturnDate'], $flights['TravelClass'], $flights['Adults'], $flights['Children']);
mysqli_stmt_execute($stmt);
if (mysqli_error($dbh))
{
print("Statement has an error: " . mysqli_error());
}
if (mysqli_stmt_affected_rows($stmt))
{
mysqli_close($dbh);
return true;
}
else
{
mysqli_close($dbh);
return false;
}
}
?>

这是我的 html 文件。有一些外部文件我没有包含在内,因为它们运行正常。我真的只是想弄清楚为什么插入命令不起作用。任何帮助将不胜感激。

<!DOCTYPE html>
<html lang="en">

<head>
<title>Flights</title>

<link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="CSS\bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="CSS\flightstyle.css" />
<link href="https://fonts.googleapis.com/css?family=Felipa" rel="stylesheet">
<script src="jquery.js"></script>
<style>
body {
color: #555753;
background: #fff url(http://csszengarden.com/001/blossoms.jpg) no-repeat bottom right;
}

p {
font-family: 'Felipa', cursive;
color: white;
}
h1 {
margin-left: 480px;
color: pink;
}
</style>
</head>

<body>
<script>
$(document).ready(function() {
$('#clock').fadeIn(1500).delay(2500).fadeOut(1500);
$('#picTwo').delay(5000).fadeIn(1500);
});

</script>

<img src="images\fallingPetals.gif" id="clock" style="width:400px;height:300px;" /><br /><br /> <br />
<div id="booking" class="section">
<div class="section-center">
<div class="container">
<div class="row">
<div class="booking-form">
<div class="form-header">
<h1>Book Your Flight</h1>
<p>Embark on Your Journey.</p>
<img src="images\fallingpetalsrevers.gif" id="integrate" style="width:100px;height:70px;" />
</div>
<form method="post" action="tryme.php">

<div class="form-group">
<input class="form-control" name="FlyingFrom" required="required" type="text">
<span class="form-label">Flying from</span>
</div>
<div class="form-group">
<input class="form-control" name="FlyingTo" required="required" type="text">
<span class="form-label">Flying to</span>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input class="form-control" name="DepartureDate" required="required" type="date">
<span class="form-label">Departure Date</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input class="form-control" name="ReturnDate" required="required" type="date">
<span class="form-label">Return Date</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<select class="form-control" name="TravelClass">
<option>Economy class</option>
<option>Business class</option>
<option>First class</option>
</select>
<span class="select-arrow"></span>
<span class="form-label">Travel class</span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<select class="form-control" name="Adults">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<span class="select-arrow"></span>
<span class="form-label">Adults</span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<select class="form-control" name="Children">
<option>0</option>
<option>1</option>
<option>2</option>
</select>
<span class="select-arrow"></span>
<span class="form-label">Children</span>
</div>
</div>
</div>
<div class="form-btn">
<button class="submit-btn">Book</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>


<script>
$('.form-control').each(function () {
floatedLabel($(this));
});

$('.form-control').on('input', function () {
floatedLabel($(this));
});

function floatedLabel(input) {
var $field = input.closest('.form-group');
if (input.val()) {
$field.addClass('input-not-empty');
} else {
$field.removeClass('input-not-empty');
}
}
</script>

</body>

</html>

最佳答案

您的 SQL 语句需要具有语句绑定(bind)函数填充的占位符。下面是简化的 example :

$sql = "INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)";
$stmt = mysqli_prepare($sql);
mysqli_stmt_bind_param('sssd', $code, $language, $official, $percent);
mysqli_stmt_execute();

关于php - 从 HTML 表单向 SQL 数据库插入行时出现问题。我究竟做错了什么?我对此还很陌生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54965748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com