使用php从数据库中删除记录

时间:2014-11-26 19:34:10

标签: php sql

我有这个代码应该从数据库中删除一个笑话但是当我运行它时它给了我:

  

注意:未定义的索引:确定在第20行的C:\ xampp \ htdocs \ Lab1 \ delete_joke.php

有什么问题? (另外,确定属性应该由用户提交,但我甚至不能看到表单。)

  <?php 
  $page_title = 'Delete a joke';
 include ('includes/header.html');
 echo '<h1>Delete a joke</h1>';

 // Check for a valid joke ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_joke.php
$id = $_GET['id'];
} else { // No valid ID, kill the script.
echo '<p class="error">This page has been accessed in error.</p>';

exit();
}

  require ('mysqli_connect.php');

// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'GET') {

if ($_GET['sure'] == 'Yes') { // Delete the record.

    // Make the query:
    $q = "DELETE  FROM joke WHERE joke_id=$id LIMIT 1";     
    $r = @mysqli_query ($dbc, $q);
    if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

        // Print a message:
        echo '<p>The joke has been deleted.</p>';   

    } else { // If the query did not run OK.
        echo '<p class="error">The joke could not be deleted due to a system error.</p>'; //    Public message.
        echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.
    }

} else { // No confirmation of deletion.
    echo '<p>The joke has NOT been deleted.</p>';   
}

}else { // Show the form.

// Retrieve the joke's information:
$q = "SELECT title FROM joke WHERE joke_id=$id";
$r = @mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) { // Valid joke ID, show the form.

    // Get the joke's information:
    $row = mysqli_fetch_array ($r, MYSQLI_NUM);

    // Display the record being deleted:
    echo "<h3>joke title: $row[0]</h3>
    Are you sure you want to delete this joke?";

    // Create the form:
    echo '<form action="delete_joke.php" method="GET">
<input type="radio" name="sure" value="Yes" /> Yes 
<input type="radio" name="sure" value="No" checked="checked" /> No
<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="id" value="' . $id . '" />
</form>';

} else { // Not a valid joke ID.
    echo '<p class="error">This page has been accessed in error.</p>';
}

 } // End of the main submission conditional.

 mysqli_close($dbc);


 ?>

表格:

 <?php 
 $page_title = 'Add joke';

 include ('includes/header.html');

   ?>

  <form action="joke_handler.php" method="GET" align="center">


<p>Title: <input type="text" name="title"  
                          value="<?php if (isset($_GET['title'])) echo $_GET['title']; ?>" /></p>

    <p>Body : <input  type="text"  id="text" style="height: 100" size="100" name='body'  ><?php   if (isset($_GET['body'])) echo $_GET['body']; ?></p>

<p>Date: <input type="date" name="date" 
                             value="<?php if (isset($_GET['date'])) echo $_GET['date']; ?>"  />     </p>

  <p><input type="submit" name="submit" value="Submit your joke" /></p>
  </form>

1 个答案:

答案 0 :(得分:0)

只需尝试将if ($_SERVER['request_method'] = ...替换为:

if (isset($_GET['sure']))