Fetching records from database and display empty records once deleted in php mysql -
hi iam trying fetch records database displaying 1 record if there more 5 records also.i have tried executing query in database working correctly can me regarding this. if delete records , if there no records in database displaying delete option image option in front end. here code.
image.php
<?php $connection = mysql_connect("localhost", "root", "") or die(mysql_error()); $db = mysql_select_db("accountant", $connection); $res = "select * blogs "; $result=mysql_query($res); $row = mysql_fetch_array($result); ?>
blogimage.php
<form method="post" action="image.php" id="myform"> <table> <thead> <tr> <th scope="col">title</th> <th scope="col">image</th> <th scope="col" style="width: 65px;">modify</th> </tr> </thead> <tbody> <?php include "image.php" ;?> <tr> <td><?php echo $row['blog_title'];?></td> <td><img src="upload/<?php echo $row['image'];?>" height="100" width="100"/></td> <td><a class="buttons delete" href="deleteblog.php" onclick="return confirm('are sure delete');" class="table-icon delete" >delete blog</a></td> </tr> </tbody> </table> </form>
deleteblog.php
$id=$_get['blog_id']; $res = "delete blogs blog_id=$id"; if($res) { echo "successfully deleted"; } else{ echo "failure"; }
look @ statement here,
$row = mysql_fetch_array($result);
you're fetching 1 row result set.
solution:
first of all, delete line, $row = mysql_fetch_array($result);
image.php page , on blogimage.php page, loop through result set display data, this:
// code <?php include "image.php"; while($row = mysql_fetch_array($result)){ ?> <tr> <td><?php echo $row['blog_title']; ?></td> <td><img src="upload/<?php echo $row['image']; ?>" height="100" width="100"/></td> <td><a class="buttons delete" href="deleteblog.php" onclick="return confirm('are sure delete');" class="table-icon delete" >delete blog</a></td> </tr> <?php } ?> // code
sidenote: don't use mysql_*
functions, deprecated of php 5.5 , removed altogether in php 7.0. use mysqli
or pdo
instead. and why shouldn't use mysql_*
functions.
edited:
this how should perform delete operation.
<?php $connection = mysql_connect("localhost", "root", "") or die(mysql_error()); $db = mysql_select_db("accountant", $connection); $id=$_get['blog_id']; $res = "delete blogs blog_id=$id"; // execute query mysql_query($res); if(mysql_affected_rows()){ echo "successfully deleted"; }else{ echo "failure"; } ?>
Comments
Post a Comment