我的代码中有异议,但它给了我错误"注意:未定义的索引"
this is the the screen with error
Index.php
<?php include ('\inc\functions.php');
global $from, $to, $make, $model, $color, $type, $power, $reg_num;
//if(isset($_POST['search'])){
if(isset($_POST["search"])){
$make = $_POST['brand'];
$model = $_POST['model'];
$reg_num = $_POST['reg_num'];
$power = $_POST['horse_powers'];
$color = $_POST['color'];
$type = $_POST['type'];
$from = $_POST['from'];
$from = strtotime($from);
$from = date("Y-m-d", $from);
$to = $_POST['to'];
$to = strtotime($to);
$to = date("Y-m-d", $to);
}
//}
/* else{
$make = null;
$model = null;
$reg_num = null;
$power = null;
$color = null;
} */
?>
<!DOCTYPE html>
<html>
<head>
<title>Rent a car</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<!-- <script type="text/javascript" src="js/jquery.js"></script> -->
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/date.js"></script>
<script>
$(function() {
$( ".datepicker" ).datepicker({
dateFormat: "dd.mm.yy",
showOn: "button",
buttonImage: "images/date.gif",
buttonImageOnly: false,
buttonText: "Select date"
});
});
</script>
</head>
<body>
<div class="container">
<header>
<nav class="main_nav">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="faq.html">FAQ</a></li>
<li><a href="contacts.html">Contacts</a></li>
</ul>
</nav><!-- End nav -->
<br class="clear" />
<aside class="search_class">
<h1>Search car</h1>
<p class="search_about">
<span class="about">About us</span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo.
<span><a href="about.html">more...</a></span>
</p>
</aside><!-- End Aside -->
<br class="clear" />
</header><!-- End header -->
<div class="clear"></div>
<section>
<div class="results">
<div class="post_top">
<h2>Results</h2>
</div>
<div class="post_middle">
<?php
switch ($type) {
case "case1":
echo '<table>';
echo '<th>Make</th><th>Model</th><th>Color</th><th>Reg.Number</th><th>Power</th>';
free_cars_period($from, $to, $servername, $username, $password, $db);
//echo "Your favorite color is red! Case 1";
echo '</table>';
break;
case "case2":
echo '<table>';
echo '<th>Make</th><th>Model</th><th>Color</th><th>Reg.Number</th><th>Power</th>';
reserved_cars_period($from, $to, $servername, $username, $password, $db);
//echo "Your favorite color is blue! Case 2";
echo '</table>';
break;
case "case3":
echo '<table>';
echo '<th>Make</th><th>Model</th><th>Color</th><th>Reg.Number</th><th>Power</th><th>First Name</th><th>Last Name</th>';
reserved_cars_criteria($from, $to, $servername, $username, $password, $db);
//echo "Your favorite color is green! Case 3";
echo '</table>';
break;
default:
echo "Your favorite color is neither red, blue, nor green! Default Case";
}
?>
</div>
<div class="post_bottom"></div>
<div class="clear"></div>
</div><!-- End div "results" -->
<div class="form_wrapper">
<form action="index.php" method="POST">
<ul>
<li class="left">
<label for="make">Make</label>
<?php //Brand select
$sql = "SELECT DISTINCT `brand` FROM `cars` ORDER BY `brand`";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
//output data of each row
echo "<select name='brand'>";
//echo "<option value=>";
echo "<option selected disabled>".'Choose brand'."</option>";
while($row = mysqli_fetch_assoc($result)){
echo "<option value='" . $row['brand'] . "'>" . $row['brand']."</option>";
}
}
echo "</select>";
?>
</li>
<li class="right">
<label for="model">Model</label>
<?php
$sql = "SELECT DISTINCT `model` FROM `cars` ORDER BY `model`";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
//output data of each row
echo "<select name='model'>";
//echo "<option value=>";
echo "<option selected disabled>".'Choose model'."</option>";
while($row = mysqli_fetch_assoc($result)){
echo "<option value='" . $row['model'] . "'>" . $row['model']."</option>";
}
}
echo "</select>";
?>
</li>
<div class="clear"></div>
<li class="left">
<label for="reg_num">Registration Number</label>
<?php
$sql = "SELECT DISTINCT `reg_num` FROM `cars` ORDER BY `reg_num`";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
//output data of each row
echo "<select name='reg_num'>";
//echo "<option value=>";
echo "<option selected disabled>".'Choose number'."</option>";
while($row = mysqli_fetch_assoc($result)){
echo "<option value='" . $row['reg_num'] . "'>" . $row['reg_num']."</option>";
}
}
echo "</select>";
?>
</li>
<li class="right">
<label for="power">Power</label>
<?php
$sql = "SELECT DISTINCT `horse_powers` FROM `cars` ORDER BY `horse_powers`";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
//output data of each row
echo "<select name='horse_powers'>";
//echo "<option value=>";
echo "<option selected disabled>".'Choose power'."</option>";
while($row = mysqli_fetch_assoc($result)){
echo "<option value='" . $row['horse_powers'] . "'>" . $row['horse_powers']."</option>";
}
}
echo "</select>";
?>
</li>
<div class="clear"></div>
<li class="left">
<label for="color">Color</label>
<?php
$sql = "SELECT DISTINCT `color` FROM `cars` ORDER BY `color`";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
//output data of each row
echo "<select name='color'>";
//echo "<option value=>";
echo "<option selected disabled>".'Choose color'."</option>";
while($row = mysqli_fetch_assoc($result)){
echo "<option value='" . $row['color'] . "'>" . $row['color']."</option>";
}
}
echo "</select>";
?>
</li>
<li class="right">
<label for="type">Type</label>
<select name="type" id="type">
<option selected disabled>Choose type</option>
<option value="case1">Свободни коли за период</option>
<option value="case2">Заети коли за период</option>
<option value="case3">Заети коли по критерии</option>
</select>
</li>
<div class="clear"></div>
<li class="left">
<label for="from">From</label>
<input type="text" name="from" id="from" class="datepicker" />
</li>
<li class="right">
<label for="to">To</label>
<input type="text" name="to" id="to" class="datepicker" />
</li>
<div class="clear"></div>
<li class="right">
<input type="submit" name="search" value="Search" id="submit" />
</li>
<div class="clear"></div>
</ul>
</form>
</div><!-- End div "form_wrapper" -->
</section><!-- End section -->
<div class="clear"></div>
<footer class="footer">
<p>All right reserved solutions.</p>
</footer><!-- End footer -->
</div><!-- End container -->
</body>
</html>
的functions.php
<?php
global $servername, $username, $password, $db;
// global $from, $to, $make, $model, $color, $type, $power, $reg_num;
$servername="localhost";
$username="root";
$password="";
$db="naemkola";
global $servername, $username, $password, $db;
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
function connect($servername, $username, $password, $db){
$servername="localhost";
$username="root";
$password="";
$db="naemkola";
$conn = mysqli_connect($servername, $username, $password, $db);
return $conn;
}
global $conn;
$conn = connect($servername, $username, $password, $db);
function select_all_cars(/*mysqli $conn */){
// $conn = connect();
$servername="localhost";
$username="root";
$password="";
$db="naemkola";
$conn = connect($servername, $username, $password, $db);
$sql = "SELECT * FROM `cars`";
$result_qr = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result_qr)){
// echo '<tr><td>'.$row['brand'].'</td><td>'.$row['model'].'</td></tr>';
echo '<tr>';
echo '<td>'.$row['brand'].'</td>';
echo '<td>'.$row['model'].'</td>';
echo '<td>'.$row['color'].'</td>';
echo '<td>'.$row['reg_num'].'</td>';
echo '<td>'.$row['horse_powers'].'</td>';
echo '</tr>';
}
return $result_qr;
}
function free_cars_period($from, $to, $servername, $username, $password, $db){
$conn = connect($servername, $username, $password, $db);
$sql2 = "SELECT `brand`, `model`, `reg_num`, `horse_powers`, `color`
FROM ((`clients_cars` INNER JOIN `cars` ON `clients_cars`.`cars_id` = `cars`.`id`)
LEFT JOIN `clients` ON `clients_cars`.`clients_id` = `clients`.`id`)
WHERE (`rent_date` >= '$from' AND `return_date` <= '$to') AND `clients_id` IS NULL";
$result_qr = mysqli_query($conn, $sql2);
while($row = mysqli_fetch_assoc($result_qr)){
echo '<tr>';
echo '<td>'.$row['brand'].'</td>';
echo '<td>'.$row['model'].'</td>';
echo '<td>'.$row['color'].'</td>';
echo '<td>'.$row['reg_num'].'</td>';
echo '<td>'.$row['horse_powers'].'</td>';
echo '</tr>';
}
return $result_qr;
}
function reserved_cars_period($from, $to, $servername, $username, $password, $db){
$conn = connect($servername, $username, $password, $db);
$sql2 = "SELECT `brand`, `model`, `reg_num`, `horse_powers`, `color`
FROM ((`clients_cars` INNER JOIN `cars` ON `clients_cars`.`cars_id` = `cars`.`id`)
LEFT JOIN `clients` ON `clients_cars`.`clients_id` = `clients`.`id`)
WHERE (`rent_date` >= '$from' AND `return_date` <= '$to') AND `clients_id` IS NOT NULL";
$result_qr = mysqli_query($conn, $sql2);
while($row = mysqli_fetch_assoc($result_qr)){
echo '<tr>';
echo '<td>'.$row['brand'].'</td>';
echo '<td>'.$row['model'].'</td>';
echo '<td>'.$row['color'].'</td>';
echo '<td>'.$row['reg_num'].'</td>';
echo '<td>'.$row['horse_powers'].'</td>';
echo '</tr>';
}
return $result_qr;
}
function reserved_cars_criteria($from, $to, $servername, $username, $password, $db){
$conn = connect($servername, $username, $password, $db);
$sql2 = "SELECT `brand`, `model`, `reg_num`, `horse_powers`, `color`, `first_name`, `last_name`
FROM ((`clients_cars` INNER JOIN `cars` ON `clients_cars`.`cars_id` = `cars`.`id`)
LEFT JOIN `clients` ON `clients_cars`.`clients_id` = `clients`.`id`)
WHERE (`rent_date` >= '$from' AND `return_date` <= '$to') AND `clients_id` IS NOT NULL";
$result_qr = mysqli_query($conn, $sql2);
while($row = mysqli_fetch_assoc($result_qr)){
echo '<tr>';
echo '<td>'.$row['brand'].'</td>';
echo '<td>'.$row['model'].'</td>';
echo '<td>'.$row['color'].'</td>';
echo '<td>'.$row['reg_num'].'</td>';
echo '<td>'.$row['horse_powers'].'</td>';
echo '<td>'.$row['first_name'].'</td>';
echo '<td>'.$row['last_name'].'</td>';
echo '</tr>';
}
return $result_qr;
}
?>
错误行:
Notice: Undefined index: brand in C:\Xampp\htdocs\homework\final_task\index.php on line 7
Notice: Undefined index: model in C:\Xampp\htdocs\homework\final_task\index.php on line 8
Notice: Undefined index: reg_num in C:\Xampp\htdocs\homework\final_task\index.php on line 9
Notice: Undefined index: horse_powers in C:\Xampp\htdocs\homework\final_task\index.php on line 10
Notice: Undefined index: color in C:\Xampp\htdocs\homework\final_task\index.php on line 11
在提交表单之前,我正在检查搜索是否为isset,如果是isset,则必须提交并分配变量,但它会给我错误。 我该如何解决这个错误?提前谢谢