从数据库中获取记录并在php mysql中删除后显示空记录

从数据库中获取记录并在php mysql中删除后显示空记录,php,mysql,Php,Mysql,您好,我正在尝试从数据库中获取所有记录,但如果有5条以上的记录,则只显示一条记录。我已尝试在数据库中执行查询,它工作正常。有人能帮我解决这个问题吗。 若我删除了记录,若数据库中并没有记录,它会在前端显示删除选项和图像选项。 这是我的密码 image.php <?php $connection = mysql_connect("localhost", "root", "") or die(mysql_error()); $db = mysql_select_db("accou

您好,我正在尝试从数据库中获取所有记录,但如果有5条以上的记录,则只显示一条记录。我已尝试在数据库中执行查询,它工作正常。有人能帮我解决这个问题吗。 若我删除了记录,若数据库中并没有记录,它会在前端显示删除选项和图像选项。 这是我的密码

image.php

<?php
    $connection = mysql_connect("localhost", "root", "") or die(mysql_error());
    $db = mysql_select_db("accountant", $connection);
    $res = "SELECT *  FROM blogs ";
    $result=mysql_query($res);
    $row = mysql_fetch_array($result);
?>

从更改php代码


看看这句话

$row = mysql_fetch_array($result);
您仅从结果集中获取一行

解决方案:

首先,删除这一行,
$row=mysql\u fetch\u array($result)image.php页面,然后在blogimage.php页面上,循环遍历结果集以显示所有数据,如下所示:

// your 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 you sure to delete');" class="table-icon delete" >Delete Blog</a></td>
        </tr>
        <?php
    }
?>

// your code

不要使用
mysql.*
函数,因为这些函数已弃用。改为使用“mysqli”

下面是您应该尝试的代码

image.php

<?php
$connection = mysqli_connect("localhost", "root", "", "accountant") or die(mysqli_error());
$res = "SELECT *  FROM blogs ";
$result= $connection->query($res);
?>
<?php
    $connection = mysqli_connect("localhost", "root", "", "accountant") or die(mysqli_error());
    $deletequery = " DELETE FROM blogs WHERE id=".intval($_GET['id']);//your delete query
    $result= $connection->query($res);//execute query
    ?>

你在blogimage.php上的代码应该是这样的

<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" ;
            while($row =  $connection->fetch_assoc($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?id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete" >Delete Blog</a></td>
                </tr>
                <?php
            }
        ?>   

        </tbody>
        </table>

标题
形象
修改
“height=“100”width=“100”/
编辑

要删除记录,请在URL中传递id,如代码
deleteblog.php?id=
中所示,然后使用以下代码

deleteblog.php

<?php
$connection = mysqli_connect("localhost", "root", "", "accountant") or die(mysqli_error());
$res = "SELECT *  FROM blogs ";
$result= $connection->query($res);
?>
<?php
    $connection = mysqli_connect("localhost", "root", "", "accountant") or die(mysqli_error());
    $deletequery = " DELETE FROM blogs WHERE id=".intval($_GET['id']);//your delete query
    $result= $connection->query($res);//execute query
    ?>

Image.php

<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$res = "SELECT *  FROM 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 you sure to delete');" class="table-icon delete" >Delete Blog</a></td>
            </tr>
        </tbody>
    </table>
</form>
<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"
 while($row =  $connection->fetch_assoc($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?blog_id=<?php echo $row['blog_id']; ?>" onclick="return confirm('Are you sure to delete');" class="table-icon delete" >Delete Blog</a></td>
            </tr>
          <?php } ?>
        </tbody>
    </table>
</form>

标题
形象
修改
“height=“100”width=“100”/
deleteblog.php

$id=$_GET['blog_id'];
$res = "DELETE 
FROM blogs 
WHERE blog_id=$id";
if($res)
{
echo "successfully deleted";
}
else{
echo "Failure";
}
<?php
$id=$_GET['blog_id'];
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$res = "delete  FROM blogs where blog_id=$id ";
$result=mysql_query($res);
header('Location: blogimage.php');
?>


请尝试此代码

如果有记录,则该代码不起作用;如果有记录,则该代码不显示一条记录;如果数据库中没有记录,则该代码将不显示任何数据。如果数据库中没有记录,你想要什么?如果也有记录,它不显示记录如果没有记录,它工作正常,但是如果数据库中有记录,它应该显示数据,但是它不工作,它在myside工作得很好。我已经执行了代码。您的image.php文件中可能存在问题。它正在工作,但如果我删除该记录,则表明它已成功删除,但在删除之前它并未删除该记录。如何传递删除该记录的id如何传递要删除的idrecord@Nagu将图像id附加到
href
属性值,像这样:
。。。href=“deleteblog.php?id=”
。在deleteblog.php页面上,使用
$id=$\u get['id']获取id并删除该特定图像。如我所给出的。。。。这里blog_id是我的db表id,deleteblog.php是我给出的….$id=$\u GET['blog_id'];。。。。但在执行时,它会将错误作为未定义的索引blog_id,并将消息显示为已成功删除,但事实并非如此deleting@Nagu你做错了,应该是,
在deleteblog.php页面上,使用
$id=$\u GET['blog\u id']捕捉它它显示为已成功删除但未删除记录它显示为已成功删除但未删除记录始终发布OP尝试出错的内容,而只是插入代码