使用PHP

时间:2016-02-11 11:39:49

标签: php mysqli

我有一个选择下拉列表,其中包含在我的表中找到的所有现有用户名。我想通过选择相应的用户名从我的表中删除一个条目。我似乎无法在我的php文件中找到错误...即使条目仍然存在,我也会收到“成功删除”消息。想知道我的php有什么问题。感谢。

以下是视图的相关部分:

<form action="deleting.php" method="post">
<select id="username">
    <option ng-repeat="user in users">
      {{user.username}}
    </option>
 </select>
<input type="submit" value="Delete"/>
</form>

这是我的deletion.php文件

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "abc";


$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$deletecontent= isset( $_POST["username"] ) ? $_POST["username"] : "null" ;


$sql = "DELETE from users where username='$deletecontent'";


if (mysqli_query($conn, $sql)) {
    echo "Successfully deleted";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


mysqli_close($conn);
?>

3 个答案:

答案 0 :(得分:4)

只有在输入中有name属性时,才能在$_POST中获取值。所以试试

<select id="username" name="username">

答案 1 :(得分:2)

您必须将name属性添加到select选项。然后只能使用$_POST访问它。

<select id="username" name = "username">

答案 2 :(得分:0)

name="username"

中设置<select>

value中设置<option>属性。

<form action="deleting.php" method="post">
    <select id="username" name="username">
          <option ng-repeat="user in users" value="{{user.username}}">
            {{user.username}}
          </option>
     </select>
    <input type="submit" value="Delete"/>
</form>