php - 来自数据库的信息应该显示在网站上,但没有

标签 php html css mysql

我一直在关注有关在网站上显示数据库信息的在线教程,但是当我尝试使用 localhost/app_location 加载它时,它只显示标题栏。我不知道为什么它不起作用。有什么建议 ?

这是我关注的教程' https://www.youtube.com/watch?v=oxZj82kh4FA&t=195s '

这是我对代码的看法:

进程.php

<?php
$mysqli = new mysqli('localhost', 'root', '', 'testing');

if (mysqli_connect_errno()) {
  echo json_encode(array('mysqli' => 'Failed to connect to MySQL: ' . mysqli_connect_error()));
  exit;
}
$page = isset($_GET['p'])? $_GET['p'] : '';
if($page=='view'){
  $result = $mysql->query("SELECT * FROM tabledit WHERE deleted != '1'");
  while($row = $result->fetch_assoc()){
    ?>

    <tr>
      <td><?php echo $row['bookingId']; ?></td>
      <td><?php echo $row['description']; ?></td>
      <td><?php echo $row['bookingStart']; ?></td>
      <td><?php echo $row['bookingEnd']; ?></td>
    </tr>
    <?php
  })
}else {


header('Content-Type: application/json');

$input = filter_input_array(INPUT_POST);


if ($input['action'] == 'edit') {
    $mysqli->query("UPDATE tabledit SET description='" . $input['description'] . "', bookingStart='" . $input['bookingStart'] . "'
      , bookingEnd='" . $input['bookingEnd'] . "' WHERE bookingId='" . $input['bookingId'] . "'");
} else if ($input['action'] == 'delete') {
    $mysqli->query("UPDATE tabledit SET deleted=1 WHERE bookingId='" . $input['bookingId'] . "'");
} else if ($input['action'] == 'restore') {
    $mysqli->query("UPDATE tabledit SET deleted=0 WHERE bookingId='" . $input['bookingId'] . "'");
}

mysqli_close($mysqli);

echo json_encode($input);
}
?>

index.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewpoint" content="width=device-width, initial-scale=1">
    <title>Table</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" >
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.csst"/>
</head>
<body onload="viewData()">

<div class="container" style="margin-top:35px">
  <h4>Table Quick Edit</h4>
  <table id="tabledit" class="table table-bordered table-striped">
    <thead>
      <tr>
        <th>BookingId</th>
        <th>Description</th>
        <th>BookingStart</th>
        <th>BookingEnd</th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>

<script src="jquery-3.4.1.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/jquery.tabledit.js"></script>
<script>
  function viewData(){
    $.ajax({
      url: 'process.php?p=view',
      method:'GET'
    }).done(function(data){
      $('tbody').html(data)
      tableData()
    })
  }
  function tableData(){
    $('#tabledit').Tabledit({
        url: 'process.php',
        eventType: 'dblclick',
        editButton: true,
        deleteButton: true,
        hideIndentifier: false,
        buttons: {
          edit: {
            class: 'btn btn-sm btn-warning',
            html: '<span class="glyphicon glyphicon-pencil"></span> Edit',
            action: 'edit'
          },
          delete: {
            class: 'btn btn-sm btn-danger',
            html: '<span class="glyphicon glyphicon-trash"></span> Trash',
            action: 'delete'
          },
          save: {
            class: 'btn btn-sm btn-success',
            html: 'Save'
          },
          restore: {
            class: 'btn btn-sm btn-warning',
            html: 'Restore',
            action: 'restore'
          },
          confirm: {
            class: 'btn btn-sm btn-default',
            html: 'Confirm'
          }
        },
        columns: {
            identifier: [0, 'bookingId'],
            editable: [[1, 'description'], [2, 'bookingStart'], [3, 'bookingEnd']]
      },
      onSuccess: function(data, textStatus, jqXHR) {
        viewData
      },
      onFail: function(jqXHR, textStatus, errorThrown) {
        console.log('onFail(jqXHR, textStatus, errorThrown)');
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
      },
      onAjax: function(action, serialize) {
        console.log('onAjax(action, serialize)');
        console.log(action);
        console.log(serialize);
      }
    });
  }
</script>
</body>
</html>

我仔细检查了每一行。我每次都运行 xampp。它连接到数据库,但没有显示任何内容。

最佳答案

你正在混合使用 mysql 和 mysqli

$result = $mysql->query("SELECT * FROM tabledit WHERE deleted != '1'");

应该是

$result = $mysqli->query("SELECT * FROM tabledit WHERE deleted != '1'");

关于php - 来自数据库的信息应该显示在网站上,但没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927229/

相关文章:

html - 使用 Bootstrap 水平平铺 Div 标签并在点击时最大化

PHP 使用来自 SQL Server 2012 的 MSSQL_Query 调用存储过程返回无效值

php - 将数组中值的位置存储到数据库表列

php - pfsockopen 线程安全?

html - 如何将图像和搜索栏对齐在一行中?

html - 使一个 child 溢出可见溢出 :hidden (or something else)

javascript - 在javascript中单击按钮更改文本并替换按钮/文本?

php - 使用 jquery 删除列表项 <li>?

javascript - div标签对齐问题

css - Vuejs.vue 文件 : dynamic less import