PHP删除基于所选的值

时间:2015-11-14 21:57:43

标签: php mysql

我试图根据所选的值删除表中的行。例如, 我有一个选择列表和一个文本框当您从选择列表中选择行并键入城市时,它应该删除该行。我无法正常工作。

<p>Select which row to <span class="del">DELETE</span> by selecting a Row from the dropdown and verifying by typing in the city.</p>
<br>
<form method="POST" action"<?php echo $_SERVER['PHP_SELF']; ?>">
<select>
  <option value=""></option>
  <option value="teamnameD">Team Name</option>
  <option value="cityD">City</option>
  <option value="bestplayerD">Best Player</option>
  <option value="yearformedD">Year Formed</option>
  <option value="websiteD">Website</option>
</select>
<br><br>
Verify <span class="del">DELETE</span> by Typing in Name of city&nbsp
<input type="text" name="cityD1">
<br><br>
  <input class="styled-button" type="submit" name="selectList" value="Submit"><br><br>
</form>
<?php

if (isset($_POST['selectList'])){
 }

if (isset($_POST['teamnameD'])) {
  teamnameD();
}

function teamnameD() {




$servername = "localhost:3306";
$username = "";
$password = "";
$dbname = "";

  $conn = new mysqli($servername, $username, $password, $dbname);

  if ($conn->connect_error) {
      echo "Cannot Connect to database ";
  }

  $stmt = $conn->prepare ("DELETE FROM Teams WHERE $selectName LIKE    $selectCity");

  $selectName = $_POST['teamnameD'];
  $selectCity = $_POST['cityD1'];
  $stmt->execute();

  if ($conn->query($sql) === TRUE) {
     echo "Record deleted successfully";
  } else {
      echo "Error deleting record: " . $conn->error;
  }

  $conn->close();


}

3 个答案:

答案 0 :(得分:2)

代码无法正常进入该功能。

我认为你没有为列表中的选定选项创建正确的弊端。您应该选择一个名称更改

<select>

<select name="foo">

然后改变

if (isset($_POST['teamnameD'])) {
  teamnameD();
}

if (isset($_POST['foo']) && $_POST['foo'] == 'teamnameD') {
  teamnameD();
}

你在功能中也有问题。

您在宣布之前使用$selectName$selectCity。尝试发布声明

$selectName = $_POST['foo'];    //notice the change here!
$selectCity = $_POST['cityD1'];

到此行之前的地方,您使用它们

$stmt = $conn->prepare ("DELETE FROM Teams WHERE $selectName LIKE    $selectCity");

另外,请尝试更改

$stmt = $conn->prepare ("DELETE FROM Teams WHERE $selectName LIKE    $selectCity");

$sql = "DELETE FROM Teams WHERE $selectName LIKE    $selectCity";

然后删除此行

$stmt->execute();

不要忘记$selectName$selectCity仍然必须在'$ sql'之前得到它们的值!

答案 1 :(得分:0)

  $stmt = $conn->prepare ("DELETE FROM Teams WHERE $selectName LIKE    $selectCity");

  $selectName = $_POST['teamnameD'];
  $selectCity = $_POST['cityD1'];

应该是这样的

  $selectName = $_POST['teamnameD'];
  $selectCity = $_POST['cityD1'];
  $stmt = $conn->prepare ("DELETE FROM Teams WHERE $selectName LIKE    $selectCity");

答案 2 :(得分:0)

有一些问题。选择没有名称所以在下面我将它设置为&#34; fieldName&#34;。如果提交条件,我更改了检查以检查提交按钮值。

然后,您应该将参数绑定到准备好的查询,然后执行。在下面的代码中,我还添加了错误检查(简单回显和失败时退出)。

<p>Select which row to <span class="del">DELETE</span> by selecting a Row from the dropdown and verifying by typing in the city.</p>
<br>
<form method="POST" action"<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="fieldName">
  <option value=""></option>
  <option value="teamnameD">Team Name</option>
  <option value="cityD">City</option>
  <option value="bestplayerD">Best Player</option>
  <option value="yearformedD">Year Formed</option>
  <option value="websiteD">Website</option>
</select>
<br><br>
Verify <span class="del">DELETE</span> by Typing in Name of city&nbsp;
<input type="text" name="cityD1">
<br><br>
  <input class="styled-button" type="submit" name="selectList" value="Submit"><br><br>
</form>
<?php
if (isset($_POST['selectList'])){
    teamnameD();
}
function teamnameD() {
    // check for a selection
    if (empty($_POST['fieldName']) || empty($_POST['cityD1'])) { 
        echo 'Please select a row and city'; 
        exit(); 
    }
    // database config
    $servername = "localhost:3306";
    $username = "";
    $password = "";
    $dbname = "";
    // connect to the database
    $conn = new mysqli($servername, $username, $password, $dbname);
    // check for connection error
    if ($conn->connect_error) {
      echo "Cannot Connect to database ";
      exit();
    }
    // prepare the query
    if (!($stmt = $conn->prepare("DELETE FROM Teams WHERE ? = ?"))) {
        echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
        exit();
    }
    // bind the parameters
    if (!$stmt->bind_param("ss", $_POST['fieldName'], $_POST['cityD1'])) {
        echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
        exit();
    }
    // execute the query
    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        exit();
    } else {
        echo "Record deleted successfully";
    }
    $conn->close();
}
?>