I'm beginner to the php. I've created forms as follows and according to me it has to do the following task.
- view.php - It will display data from DB. In this page if the user clicks on Edit it will go to the updateview.php page.
- In updateview.php page the user will update only three fields i.e, Category, Short Description, Full Description & File Upload . When the user clicks on Update button it will go to the update.php and it will process after that it will return back to the view.php with updated record.
But it is not working. Its not displaying data from DB in view.php and giving warning as follows.
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in view.php on line 81
Please help me to solve this. Thanks.
view.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="CSS/jquery.dataTables.css" type="text/css" />
<script type="text/javascript" src="JS/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="JS/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#example').dataTable();
} );
</script>
<?php session_start(); ?>
<?php
if(isset($_SESSION['example']))
{
echo $_SESSION['example'];
}
else
{
//echo "Session destroyed";
}
?>
</div>
</head>
<body>
<table id="example" class="row-border" cellspacing="0" width="100%">
<thead>
<tr>
<th>SRN</th>
<th>Client</th>
<th>Category</th>
<th>Short Description</th>
<th>Full Description</th>
<th>File Upload</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php while($row = mysqli_fetch_array($selectQ)){ ?>
<tr>
<td><?php echo $row['srn'];?></td>
<td><?php echo $row['client'];?></td>
<td><?php echo $row['category'];?></td>
<td><?php echo $row['sd'];?></td>
<td><?php echo $row['fd'];?></td>
<td><?php echo $row['upload'];?></td>
<td><a href="updateview.php?srn=<?php echo $row['srn']; ?>" target="_blank">Edit</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
dbconn.php
<?php
$mysqli = new mysqli("localhost", "root", "root", "eservice");
/* ESTABLISH CONNECTION */
if (mysqli_connect_errno()) {
echo "Failed to connect to mysql : " . mysqli_connect_error();
exit();
}
?>
updateview.php
<?php
include_once('dbconn.php');
$srn = $_GET['srn'];
$selQ = "Select * from main where srn = '".$srn."'";
$selectQ = mysql_query($selQ);
?>
<?php
while($row = mysql_fetch_array($selectQ)){ ?>
<form action="update.php" method="post" enctype="multipart/form-data" novalidate>
<?php
include_once('dbconn.php');
$srn = $_GET['srn'];
if($stmt = $mysqli->prepare("SELECT srn, client, category, sd, fd,upload FROM main WHERE srn=?")){
$stmt->bind_param("s",$_GET["srn"]);
$stmt->execute();
$stmt->bind_result($srn,$client,$category,$sd,$fd);
$stmt->fetch();
$stmt->close();
}
?>
<div class="item">
<label> <span>SRN</span>
<input name="srn" type="text" id="srn" size="15" readonly="readonly" maxlength="40" value="<?php echo $srn; ?>"/>
</label>
</div>
<div class="item">
<label> <span>Client</span>
<select class="required" name="client"/>
<?php
if($stmt = $mysqli->prepare("SELECT cname FROM client")){
$stmt->execute();
$stmt->bind_result($cname);
while($stmt->fetch()){
?>
<option value="<?php echo $cname; ?>" <?php if($cname==$client){ echo "selected"; } ?>> <?php echo $cname; ?> </option>
<?php
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT OF CLIENT */
?>
</select>
</label>
</div>
<div class="item">
<label> <span>Category</span>
<select class="required" name="category"/>
<?php
if($stmt = $mysqli->prepare("SELECT name FROM category")){
$stmt->execute();
$stmt->bind_result($name);
while($stmt->fetch()){
?>
<option value="<?php echo $name; ?>" <?php if($name==$category){ echo "selected"; } ?>> <?php echo $name; ?> </option>
<?php
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT OF CATEGORY */
?>
</select>
</label>
</div>
<div class="item">
<label> <span>Short Description</span>
<textarea required="required" name='sd'><?php echo $sd; ?></textarea>
</div>
<div class="item">
<label> <span>Full Description</span>
<textarea required="required" name='fd'><?php echo $fd; ?></textarea>
</div>
<div class="item">
<label> <span>Input Attachment</span>
<input type="file" name ="filename" required>
</label>
</div>
<br/><br/>
<div class="item">
<button id='cancel' type='cancel'>Cancel</button>
<button id='send' type='submit'>Update</button>
</div>
</form>
<?php } ?>
update.php
<?php
include('dbconn.php');
$stmt = $mysqli->prepare("UPDATE main SET client=?, category=?, sd=?, fd=? upload=? WHERE srn=?");
$stmt->bind_param('srn', $_POST["client"], $_POST["category"], $_POST["sd"], $_POST["fd"],$_POST["upload"],$_POST["srn"]);
$stmt->execute();
?>