I'm getting the following error because my MySQL query is failing, but I have tried and tried and cannot fix this.
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in F:\xampp\htdocs\LMCS Incident Manager\New\edit.php on line 40 Error: Data not found..
PHP code:
$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..");
}
I think the problem is that my second query,
$result = mysql_query("SELECT * FROM `cad` WHERE `id` = $id");
is failing and it stops. I think though it's not able to get the "id" of the table row that I'm trying to retrieve through my MySQL database but I'm not sure.
Table.php code:
<!-- 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 -->
Explanation: This is my table in my table.php file: codebin.org/view/ca0998b1. This is what displays the info. From here, I want to edit a table row when a person clicks on a button in the table that I have made. Once they do that then it brings them to my edit.php file: codebin.org/view/b30b7138. Basically my problem in the edit.php file is that I need to get that certain table row from table.php to edit it from my database.