php中的表中没有删除值?

时间:2013-11-27 18:04:59

标签: php mysql sql

您好我试图删除在下拉列表中选择的任何值。

我似乎无法理解发生了什么

我有2页1我的连接和功能,以下拉(有效)和删除功能(这似乎不起作用)查看表和另一个调用函数进入和删除哪个曾经选择了值。

connection.php

<?php

//Connect to the database
function getSQLConnection() {
$mysqlConnection = new PDO('mysql:host=localhost;dbname=isad235_100000', "root", "");

return $mysqlConnection;
}

//Get all results from members table
function getResults($tablename) {
$sql = "SELECT * FROM " . $tablename;

$mysqlConnection = getSQLConnection();

$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}

//Delete results from members table
function deleteValue($id) {
$sql = "DELETE FROM members WHERE member_id = '$id'";

$mysqlConnection = getSQLConnection();

$ResultSetting = $mysqlConnection->query($sql);
return $ResultSetting;
}

   ?>

delete.php

<?php
   include_once 'connection.php';
    ?>

<!DOCTYPE html>
<html>
   <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Add</title>
  </head>
  <body>

    <h1> Delete a Member from the Members Table. </h1>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>   
        Delete Member:
        <select name='members' value='members'id="Mmembers">       
            <?php
            $results = getResults('members');
            if ($results) {
                foreach ($results as $row) {
                    echo '<option value="' . $row['member_id'] . '">' . $row['name'] . '</option>';
                }
            }
            else
                echo '<option value="0"0"> No Data</option>';
            ?>

        </select>

        <input type="submit" id="delete" value="Delete"/>
        <br/> 
        <br/>
    </form>
    <?php
    if (isset($_POST['members'])) {


        $ResultSetting = deleteValue(($_POST['members']));
    }
    ?>
    <br/>
    <br/>

    <form action='index.php' method='GET'>
        Go Back: 
        <input type="submit" name="submit" value="Return"/>
    </form>

    <br/>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我运行了您的代码,但没有看到任何错误。确保“成员”表上的id列名为“member_id”。如果名称中存在差异,则不会设置选项元素的值。此外,您刚删除的值仍会在初始页面提交后显示。如果您在提交后重新加载页面,您将看到该值已消失。