遇到PHP SQL数据库编辑和删除问题

时间:2017-08-07 15:26:02

标签: php sql database edit

以下是列出数据库工作正常的页面

<?php

$db_host = 'localhost'; 
$db_user = 'yeospace_nm0102'; 
$db_pass = '*********'; 
$db_name = 'yeospace_nm0102'; 

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
    die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
}

$sql = 'SELECT * FROM AddEntry';
$query = mysqli_query($conn, $sql);
if (!$query) {
    die ('SQL Error: ' . mysqli_error($conn));
}
$result = mysqli_query($conn, "SELECT * FROM AddEntry ORDER BY id DESC"); 
?>
<html>
<head>
    <title>List Of Reviews</title>
</head>
<body>
    <h1>List of Reviews</h1>
    <table class="data-table">
        <thead>
            <tr>
                <th>NO</th>
                <th>Author</th>
                <th>Title</th>
                <th>Date</th>
                <th>Age</th>
                <th>Review</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $no     = 1;
        $total  = 0;
        while ($row = mysqli_fetch_array($query))
        {
            echo "<tr>";
            echo "<td>".$no."</td>";
            echo "<td>".$row['author']."</td>";
            echo "<td>".$row['title']."</td>"; 
            echo "<td>".date('F d, Y', strtotime($row['date']))."</td>";  
            echo "<td>".$row['age']."</td>";  
            echo "<td>".$row['review']."</td>";     
            echo "<td><a href=\"update.php?id=$row[id]\">Edit</a> | <a href=\"delete.php?id=$row[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";    

            $no++;
        }
?>
        </tbody>

    </table>
<button onClick="location.href='index.php'"> Return to Home</button>

</body>
</html>

但是当我尝试删除和编辑时遇到了问题。没有删除任何内容,也没有编辑任何内容。

这是我的delete.php

<?php

$db_host = 'localhost'; 
$db_user = 'yeospace_nm0102'; 
$db_pass = '***********'; 
$db_name = 'yeospace_nm0102'; 

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
    die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
}

$sql = 'SELECT * FROM AddEntry';
$query = mysqli_query($conn, $sql);
if (!$query) {
    die ('SQL Error: ' . mysqli_error($conn));
}

$id = $_GET['id'];

$result = mysqli_query($conn, "DELETE FROM AddEntry WHERE id=$id");

header("Location:ListEntry.php");
?>

这是我的update.php(编辑并保存)

<?php
$db_host = 'localhost'; 
$db_user = 'yeospace_nm0102'; 
$db_pass = '********'; 
$db_name = 'yeospace_nm0102'; 

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
    die ('Failed to connect to MySQL: ' . mysqli_connect_error());  
}

$sql = 'SELECT * FROM AddEntry';
$query = mysqli_query($conn, $sql);
if (!$query) {
    die ('SQL Error: ' . mysqli_error($conn));
}

if(isset($_POST['update']))
{    
    $id = $_POST['id'];

    $author=$_POST['author'];
    $title=$_POST['title']; 
    $review=$_POST['review'];    


    $result = mysqli_query($conn, "UPDATE AddEntry SET author='$author',title='$title', review='$review' WHERE id=$id");

    header("Location: ListEntry.php");
}
?>
<?php
$id = $_GET['id'];

$result = mysqli_query($conn, "SELECT * FROM AddEntry WHERE id=$id");

while($res = mysqli_fetch_array($result)) {
    $author = $res['author'];
    $title = $res['title'];
    $review = $res['review'];
}
?>
<html>
<head>    
    <title>Edit Reviews</title>
</head>

<body>
    <a href="ListEntry.php">Home</a>
    <br/><br/>

    <form name="form1" method="post" action="update.php">
        <table border="0">
            <tr> 
                <td>Name:</td>
                <td><input type="text" name="author" value="<?php echo $author;?>"></td>
            </tr>
            <tr> 
                <td>Title:</td>
                <td><input type="text" name="title" value="<?php echo $title;?>"></td>
            </tr>
            <tr> 
                <td>Review:</td>
                <td><input type="text" name="review" value="<?php echo $review;?>"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>
</body>
</html>

我试图排除整晚的故障,但我找不到这3个文件中的错误。有谁可以帮助我吗?非常感谢 !

0 个答案:

没有答案