dsc6517 2014-11-26 19:34
浏览 15
已采纳

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

I have this code that is supposed to delete a joke from a database but when I run it it gives me:

Notice: Undefined index: sure in C:\xampp\htdocs\Lab1\delete_joke.php on line 20

What is the problem? (Also, the sure attribute is supposed to be submitted by the user, but I don't even get to see the form.)

  <?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);


 ?>

The form:

 <?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>
</div>
  • 写回答

1条回答 默认 最新

  • duanlachu7344 2014-11-26 20:29
    关注

    Simply, try to substitute your if ($_SERVER['request_method'] = ... with this:

    if (isset($_GET['sure']))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab