【问题标题】:Why is my data not displaying on the webpage?为什么我的数据没有显示在网页上?
【发布时间】:2021-09-16 04:43:53
【问题描述】:

我正在努力完成这项工作,但我似乎无法做到。 基本上这是我的在线食品订购网站的管理盘面板: 1 菜主页面

这是添加菜的页面: 2 添加菜品页面

当我尝试提交表单时,它不会在数据库中添加数据,也不会在菜主页面中显示菜。

这是我的菜主页面代码:

<?php 
include('top.php');

if(isset($_GET['type']) && $_GET['type']!=='' && isset($_GET['id']) && $_GET['id']>0){
    $type=get_safe_value($_GET['type']);
    $id=get_safe_value($_GET['id']);
    if($type=='active' || $type=='deactive'){
        $status=1;
        if($type=='deactive'){
            $status=0;
        }
        mysqli_query($con,"update dish set status='$status' where id='$id'");
        redirect('dish.php');
    }

}

$sql="select dish.*,category.category from dish,category where dish.category_id=category.id order by dish.id desc";
$res=mysqli_query($con,$sql);

?>
  <div class="card">
            <div class="card-body">
              <h1 class="grid_title">Dish Master</h1>
              <a href="manage_dish.php" class="add_link">Add Dish</a>
              <div class="row grid_box">
                
                <div class="col-12">
                  <div class="table-responsive">
                    <table id="order-listing" class="table">
                      <thead>
                        <tr>
                            <th width="10%">S.No #</th>
                            <th width="15%">Category</th>
                            <th width="25%">Dish</th>
                            <th width="15%">Image</th>
                            <th width="15%">Added On</th>
                            <th width="20%">Actions</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php if(mysqli_num_rows($res)>0){
                        $i=1;
                        while($row=mysqli_fetch_assoc($res)){
                        ?>
                        <tr>
                            <td><?php echo $i?></td>
                            <td><?php echo $row['category']?></td>
                            <td><?php echo $row['dish']?></td>
                            <td><?php echo $row['image']?></td>
                            <td>
                            <?php 
                            $dateStr=strtotime($row['added_on']);
                            echo date('d-m-Y',$dateStr);
                            ?>
                            </td>
                            <td>
                                <a href="manage_dish.php?id=<?php echo $row['id']?>"><label class="badge badge-success hand_cursor">Edit</label></a>&nbsp;
                                <?php
                                if($row['status']==1){
                                ?>
                                <a href="?id=<?php echo $row['id']?>&type=deactive"><label class="badge badge-danger hand_cursor">Active</label></a>
                                <?php
                                }else{
                                ?>
                                <a href="?id=<?php echo $row['id']?>&type=active"><label class="badge badge-info hand_cursor">Deactive</label></a>
                                <?php
                                }
                                
                                ?>
                            </td>
                           
                        </tr>
                        <?php 
                        $i++;
                        } } else { ?>
                        <tr>
                            <td colspan="5">No data found</td>
                        </tr>
                        <?php } ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        
<?php include('footer.php');?>

这是我添加菜肴页面的代码:

<?php 
include('top.php');

$msg="";
$category_id="";
$dish="";
$dish_detail="";
$image="";
$id="";

if(isset($_GET['id']) && $_GET['id']>0){
    $id=get_safe_value($_GET['id']);
    $row=mysqli_fetch_assoc(mysqli_query($con,"select * from dish where id='$id'"));
    $category_id=$row['category_id'];
    $dish=$row['dish'];
    $dish_detail=$row['dish_detail'];
    $image=$row['image'];
}

if(isset($_POST['submit'])){
    $category_id=get_safe_value($_POST['category_id']);
    $dish=get_safe_value($_POST['dish']);
    $dish_detail=get_safe_value($_POST['dish_detail']);
    $added_on=date('Y-m-d h:i:s');
    
    if($id==''){
        $sql="select * from dish where dish='$dish'";
    }else{
        $sql="select * from dish where dish='$dish' and id!='$id'";
    }   
    if(mysqli_num_rows(mysqli_query($con,$sql))>0){
        $msg="Dish already added";
    }else{
        if($id==''){
            mysqli_query($con,"insert into dish(category_id,dish,dish_detail,status,added_on,) values('$category_id','$dish','$dish_detail',1,'$added_on,')");
        }else{
            mysqli_query($con,"update dish set category_id='$category_id', dish='$dish', dish_detail='$dish_detail' where id='$id'");
        }
        
        redirect('dish.php');
    }
}
$res_category=mysqli_query($con,"select * from category where status='1' order by category asc")
?>
<div class="row">
            <h1 class="grid_title ml10 ml15">Dish</h1>
            <div class="col-12 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <form class="forms-sample" method="post">
                    <div class="form-group">
                      <label for="exampleInputName1">Category</label>
                      <select class="form-control" name="category_id">
                          <option value="">Select Category</option required>
                          <?php
                          while($row_category=mysqli_fetch_assoc($res_category)){                           
                                    echo "<option value='".$row_category['id']."'>".$row_category['category']."</option>";  
                          }
                          ?>
                      </select>
                    </div>
                    <div class="form-group">
                      <label for="exampleInputName1">Dish</label>
                      <input type="text" class="form-control" placeholder="Dish" name="dish" value="<?php echo $dish?>" required>
                      <div class="error mt8"><?php echo $msg?></div>
                    </div>
                    <div class="form-group">
                      <label for="exampleInputEmail3" required>Dish Detail</label>
                      <textarea name="dish_detail" class="form-control" placeholder="Dish Details"></textarea>
                    </div>
                    
                    <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                  </form>
                </div>
              </div>
            </div>
            
         </div>
        
<?php include('footer.php');?>

数据不会显示在菜肴母版页中。我该怎么办?

编辑: 以下是相关数据库: dish database category database

【问题讨论】:

  • 您的 SQL 中有错字:insert ... ,status,added_on,) - 结尾的逗号会导致 SQL 失败。请注意,您的 '$added_on,' value 也有额外的评论。你真的应该做一些错误处理/检查来捕捉这个。在 SO 上发布问题之前,请尝试进行一些基本调试 - 例如,回显您的 SQL 并尝试在 MySQL 客户端中运行它会立即向您显示此问题。我投票结束是一个错字。
  • @Don'tPanic 我已经删除了逗号,但它仍然没有保存到数据库中,也没有显示在菜主页面中。他们还有什么问题吗?
  • 您是否尝试过我建议的调试 SQL 的方法?您还尝试了哪些其他调试?如果您查看浏览器的开发工具,并检查网络选项卡,您能看到 POST 发生吗?您期望的所有领域都在那里吗?在 PHP 方面怎么样 - 您是否尝试过记录或回显内容以确保流程按预期进行,并且您使用的变量按预期设置?
  • @Don'tPanic 我使用 php 来回显 select * from category where status='1' order by category asc。然后我将此查询放入 phpmyadmin SQL 选项卡,但它仍然无法正常工作。
  • 警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!

标签: php mysql sql


【解决方案1】:

您的表单缺少操作和方法属性。因此,目前您提交的任何信息都不会发送到任何地方,因此不会记录在您的数据库中。

以下是有关如何设置正确表单的一些信息:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

此外,您的代码容易受到 SQL 注入攻击。我强烈建议您切换到准备好的语句或使用这样的辅助类: https://github.com/colshrapnel/safemysql

【讨论】:

    猜你喜欢
    • 2013-02-25
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多