我收到以下错误,因为我的MySQL查询失败,但我已经尝试过并且无法解决此问题。
警告:mysql_fetch_array()期望参数1是资源, 在F:\ xampp \ htdocs \ LMCS事件管理器\ New \ edit.php中给出的布尔值 第40行错误:未找到数据..
PHP代码:
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cad", $con);
$query = "SELECT id FROM cad";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$row['id'];
}
$id = $row['id'];
$result = mysql_query("SELECT * FROM `cad` WHERE `id` = $id");
$row = mysql_fetch_array($result);
if (!$result)
{
die("Error: Data not found..");
}
我认为问题在于我的第二个查询,
$result = mysql_query("SELECT * FROM `cad` WHERE `id` = $id");
失败并停止。我想尽管它无法获得我想通过我的MySQL数据库检索的表行的“id”,但我不确定。
Table.php代码:
<!-- Table -->
<form action="index.php" method="get" id="dispatch">
<table>
<thead>
<tr>
<th>Incident #</th>
<th>Town</th>
<th>Location</th>
<th>Incident Type</th>
<th>Time/Date</th>
<th>Admin</th>
<th>Edit Entry</th>
</tr>
</thead>
<tbody>
<?php
if( isset($_POST['town']) )
{
$town = $_POST['town'];
}
if( isset($_POST['location']) )
{
$location = $_POST['location'];
}
if( isset($_POST['incident_type']) )
{
$incident_type= $_POST['incident_type'];
}
if( isset($_POST['time_date']) )
{
$time_date= $_POST['time_date'];
}
if( isset($_POST['admin']) )
{
$admin = $_POST['admin'];
}
if( isset($_POST['id']) )
{
$id = $_POST['id'];
}
$db = mysqli_connect('localhost','root','') or die("Database error");
mysqli_select_db($db, 'cad');
$result= mysqli_query($db, "SELECT * FROM `cad` ORDER BY `time_date` DESC LIMIT 20");
while($row = mysqli_fetch_array($result))
{
$town = $row['town'];
$location = $row['location'];
$incident_type = $row['incident_type'];
$time_date = $row['time_date'];
$admin = $row['admin'];
$id = $row['id'];
/*
date_default_timezone_set('America/Chicago');
$timestamp = strtotime(date("Y-m-d H:i:s")) + 3600;
$time = date('Y-m-d H:i:s', $timestamp);
if ($time_date >= $timestamp)
echo "<tr class=\"tr-black\">";
else
echo "<tr class=\"tr-red\> */
echo "<tr>
<td class=\"id-center\">
".$id."
</td>
<td >
".$town."
</td>
<td>
".$location."
</td>
<td>
".$incident_type."
</td>
<td>
".$time_date."
</td>
<td >
".$admin."
</td>
<td>
<a id=\"edit-left\" href=\"edit.php?id=$id\" onclick=\"return confirm('Are you sure you want to edit this incident?');\" name=\"edit\" value=\"$id\" class=\"btn btn-primary btn-default center-1\"><span class=\"glyphicon glyphicon-edit\"></span></a>
<a id=\"edit-right\" href=\"delete.php?id=$id\" onclick=\"return confirm('Are you sure you want to delete this incident?');\" name=\"delete\" value=\"$id\" class=\"btn btn-primary btn-default center-1\"><span class=\"glyphicon glyphicon-trash\"></span></a>
</td>
</tr>";
}
mysqli_close($db);
?>
</tbody>
</table>
</form>
<!-- End -->
说明:这是我table.php文件中的表:codebin.org/view/ca0998b1。这是显示信息的内容。从这里开始,当一个人点击我所创建的表格中的按钮时,我想编辑一个表格行。一旦他们这样做,那么它将它们带到我的edit.php文件:codebin.org/view/b30b7138。基本上我在edit.php文件中的问题是我需要从table.php获取某个表行来从我的数据库中编辑它。
答案 0 :(得分:1)
问题是这段代码:
while($row = mysql_fetch_array($result))
{
$row['id'];
}
$id = $row['id'];
在while
循环内,你没有对$row['id']
做任何事情。当mysql_fetch_array()
返回false
时,循环结束,在最后一次迭代中,它将其分配给$row
。所以在循环完成后,你正在做:
$id = false['id'];
这没有意义,并将$id
设置为NULL
。然后,当您尝试执行第二个查询时,它正在执行:
$result = mysql_query("SELECT * FROM `cad` WHERE `id` = ");
这是无效的SQL,因此您收到错误。
请参阅@ Volkerk关于如何正确编码所有内容的答案。
答案 1 :(得分:0)
此:
$id = $row['id'];
永远不会有用,因为您的$row
变量仅在while()
循环中可用。你想要做的是:
while($row=mysql_fetch_array($result)) {
$id = $row['id'];
}
但我不明白为什么你这样做......你正在取得id然后尝试获取数据......?为什么不在一个查询中获取所有内容...?
答案 2 :(得分:0)
问题是这一行:
$row = mysql_fetch_array($result);
http://php.net/manual/en/function.mysql-fetch-array.php
你没有传递正确的参数,你只给它一个布尔值
答案 3 :(得分:0)
唯一的方法(我能想到)你的脚本让一些感觉是你只想选择“最后”记录。
如果是这样,您可能会对ORDER BY和LIMIT
<?php
// you really should change the default settings, root with no password isn't such a good idea
$con = mysql_connect("localhost","root","");
if (!$con) {
// crude error handling
// you might want to improve it
// see https://www.owasp.org/index.php/Information_Leakage
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cad", $con)
or die('Could not select database'); // again crude, but anyway
// SELECT only the record with the highest id
// SELECT * is frowned upon - it's working, yet ... just write out the field names...
$query='
SELECT
id, foo, bar, baz
FROM
cad
ORDER BY
id DESC
LIMIT
1
';
$result=mysql_query($query)
or die(mysql_error()); //crude
while( false!=($row=mysql_fetch_array($result)) )
{
echo '<td>', htmlspecialchars($row['bar']), '</td>';
}