我正在制作一个页面来登记损坏的手机。我做了一个搜索查询来搜索数据库中与日期选择器的日期匹配的数据。这是我的源代码。 (仅与此问题相关,)
<?php
$con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('mobile', $con);
} else {
die('Could not connect: ' . mysql_error());
}
if (array_key_exists('myday', $_POST)) {
$mydate = $_POST['mydate'];
$myday = "SELECT * FROM mobile_rep WHERE sdate='{$mydate}' ";
}
mysql_close($con)
?>
<html>
<head> </head>
<body>
<form action="index.php" method="POST" class="form-inline">
<div class="span12" style="border-radius: 5px; border-style: solid; border-width: 1px; margin-left: 20px;" >
<h5 style="margin-left: 10px; "> <b> Current User List </b></h5>
Select By Date
<input type="date" name="mydate" id="mydate" class="input-medium" />
<input type="submit" class="btn-primary " name="myday" value="Search by date" id='myday' />
<br/> <br/>
<table border="1" width="300" cellspacing="1" cellpadding="1" class="table table-striped" style=" border-radius: 5px; border-style: solid; border-width: 1px; ">
<thead>
<tr>
<th>sdate</th>
<th>model_no</th>
<th>fault</th>
<th>rp_fee</th>
<th>sp_fee</th>
<th>total</th>
</tr>
</thead>
<tbody>
<?php
while ($row1 = mysql_fetch_array($myday)) {
echo "<tr>";
echo "<td>" . $row1['sdate'] . "</td>";
echo "<td>" . $row1['model_no'] . "</td>";
echo "<td>" . $row1['fault'] . "</td>";
echo "<td>" . $row1['rp_fee'] . "</td>";
echo "<td>" . $row1['sp_fee'] . "</td>";
echo "<td>" . $row1['total'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</form>
</body>
</html>
我正在
undefined variable :myday 在 C:\wamp\www\mobile\index.php 第 227 行
警告:mysql_fetch_array() 期望参数 1 为资源,在第 227 行 C:\wamp\www\mobile\index.php 中给出的 null
第 227 行表示 while ($row1 = mysql_fetch_array($myday)) {
我还上传了我的页面的图片。
请帮助我。
最佳答案
当您第一次进入页面时,$myday 变量未设置。如果您发送 POST 代码,则执行 if (array_key_exists('myday', $_POST)) { }
并创建 $myday。
只需在 while 循环之前检查变量是否存在,否则会将 null 值传递给 mysql_fetch_data 函数
if(isset($myday)) { while ($row1 = mysql_fetch_array($myday)) ... }
关于php - 如何使用日期选择器的值选择数据库中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20584513/