嗨我试图从数据库中获取所有记录,但是如果还有超过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);
?>
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>
deleteblog.php
$id=$_GET['blog_id'];
$res = "DELETE
FROM blogs
WHERE blog_id=$id";
if($res)
{
echo "successfully deleted";
}
else{
echo "Failure";
}
答案 0 :(得分:0)
从
更改您的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
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$res = "SELECT * FROM blogs ";
$result = mysql_query($res);
while($row = mysql_fetch_array($result)){
$data[] = $row;
}
?>
来自的
和html / 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>
<?php if(!empty($data)){ ?>
<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"; ?>
<?php foreach ($data as $row) { ?>
<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 } ?>
<?php } ?>
</tbody>
</table>
</form>
答案 1 :(得分:0)
请看这里的陈述,
$row = mysql_fetch_array($result);
您只从结果集中获取一行。
<强>解决方案:强>
首先,从 image.php 页面删除此行$row = mysql_fetch_array($result);
,然后在 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_*
函数,从PHP 5.5开始不推荐使用它们,在PHP 7.0中完全删除它们。请改用mysqli
或pdo
。 And this is why you shouldn't use mysql_*
functions。
<强>编辑:强>
这是你应该如何执行DELETE操作。
<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$id=$_GET['blog_id'];
$res = "DELETE FROM blogs WHERE blog_id=$id";
// Execute the query
mysql_query($res);
if(mysql_affected_rows()){
echo "successfully deleted";
}else{
echo "Failure";
}
?>
答案 2 :(得分:0)
不要使用mysql_*
函数,因为这些函数已被弃用。而是使用`mysqli _ *。
以下是您应该尝试的代码
<强> image.php 强>
<?php
$connection = mysqli_connect("localhost", "root", "", "accountant") or die(mysqli_error());
$res = "SELECT * FROM blogs ";
$result= $connection->query($res);
?>
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>
修改强>
要删除记录,请在代码deleteblog.php?id=<?php echo $row['id']; ?>
中传递网址中的ID,然后使用以下代码。
<强> deleteblog.php 强>
<?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
?>
答案 3 :(得分:0)
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"
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>
deleteblog.php
<?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');
?>
试试此代码