PHP删除不起作用并显示结果表

时间:2016-04-21 02:45:45

标签: php mysql

我遇到一个问题,我有一个下拉列表,其中包含所有者的姓氏,一旦我选择它并按下删除按钮,它应该从下拉列表中删除所有者名称以及任何相关的所有者信息和船只信息在mySQL数据库中。我编写了@sql查询来执行删除功能但似乎没有删除它。

一旦用户点击删除按钮,我怎样才能打印出表(所有者表和MarinaSlip表,这些是mySQL数据库中的名称)。我希望它在同一页面下显示两个表格。

deletedowner.php:

<?php  #index.php for Assignment 10
$page_title = 'Assignment 10 for Marina Database';
include('header.html');
require('dbConn.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{

    $id = $_POST['OwnerID'];
    try
    {
        $sql = "DELETE m, o
              FROM Owner AS o
              LEFT JOIN MarinaSlip AS m
              ON o.OwnerNum = m.OwnerNum
              WHERE o.OwnerNum = :ownerId";
        $stmt = $conn->prepare($sql);
        $stmt->execute(array(':ownerId' => $id));

    //include('DeletedUpdatedList.php'); when I put uncomment this line, it shows table but the delete button disappears

    } // end try

catch (PDOException $e)
    {

    echo 'Error: '.$e->getMessage();

    } //end catch

} //end if server
echo '<center>';
echo '<h3> Select the owners last name from drop down list to delete owner and their boats.</h3>';
$sql = "select OwnerNum, LastName from Owner"; //prints sql query
echo '<form action="Assignment10deleteowner.php" method="POST">';

echo "<select name='OwnerID' id=OwnerID'>";

foreach($conn->query($sql) as $row) 
{
    echo '<option value = "';
    echo $row['OwnerNum'];
    echo '"> ';
    echo $row['LastName'];
    echo '</option>';

} // end foreach
echo '</select>';

echo '<br><input type="submit" name="submit" value="Delete"> <br>';
echo '</form>'; //end form

 // now to check if the delete button has been clicked 

include('footer.html'); 
?>

DeletedUpdatedList.php

<?php  #index.php for Assignment 10
$page_title = 'Assignment 10 for AlexaMara Marina Database';
echo '<h2> Updated list of Owners and MarinaSlip:</h2>';
$stmt = $conn->prepare("select * from Owner");  //prepare statment to print all of the owners
$stmt->execute(); //excute the sql query

$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt = $conn->prepare("select * from MarinaSlip");  //prepare statment to print all of the owners
$stmt->execute(); //excute the sql query

$result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

echo "<table style='border: solid 1px black;'>"; //make table to display column headers
echo "<tr><th>OwnerNum</th><th>LastName</th><th>FirstName</th><th>Address</th><th>City</th><th>State</th><th>Zip</th></tr>";


class TableRows extends RecursiveIteratorIterator 
{ 
function __construct($it) { 
    parent::__construct($it, self::LEAVES_ONLY); 
}

function current() {
    return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}

function beginChildren() { 
    echo "<tr>"; 
} 

function endChildren() { 
    echo "</tr>" . "\n";
} 

} 

foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) 
{ 
     echo $v;

}


$conn = null;
echo "</table>"; //end table

//$sql = 'select BoatName, m.MarinaNum, SlipID from MarinaSlip s, Marina m where s.MarinaNum //= m.MarinaNum';
//echo '<form action="Assignment9.php" method="POST">';
//echo '</form>';

?>


    [only prints 1 table and now formatting is messed up.  The drop down and delete button should be first and then should display both tables][1]

非常感谢任何帮助,非常感谢

2 个答案:

答案 0 :(得分:0)

执行DELETE查询以执行删除。

try
{
    $sqlDel =  "DELETE o, m 
                FROM Owner o 
                left join MarinaSlip m 
                on o.OwnerNum = m.OwnerNum
                WHERE o.OwnerNum = '{$id}';" ;
    $conn->query($sqlDel);
} // end try
catch (PDOException $e)
{
    echo 'Error: '.$e->getMessage();
} //end catch

如果两个表始终具有数据

,则可以使用内连接

答案 1 :(得分:0)

由于多种原因,包括protection from SQL injection,您应该为DELETE语句使用预准备语句。您还必须执行语句才能使其生效。

以下是代码的相关部分,经过修改后可正常运行:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $id = $_POST['OwnerID'];
    try
    {
        $sql = "DELETE m, o
                  FROM Owner AS o
                  LEFT JOIN MarinaSlip AS m
                    ON o.OwnerNum = m.OwnerNum
                  WHERE o.OwnerNum = :ownerId";
        $stmt = $conn->prepare($sql);
        $stmt->execute(array(':ownerId' => $id));
    } // end try
    catch (PDOException $e)
    {
        echo 'Error: '.$e->getMessage();
    } //end catch
} //end if server

通过使用预准备语句,您可以获得变量的自动转义,引用和类型匹配。

另外,不要过度复杂化。要显示表格,

$stmt = $conn->query('SELECT * FROM Owner');
echo '<table>';
while ($row = $stmt->fetch(PDO::FETCH_NUM))
{
    echo '<tr>';
    foreach ($row as $value)
    {
        echo "<td>{$value}</td>";
    }
    echo '</tr>';
}
echo '</table>';

应该足够了。根据需要更改MarinaSlip表的查询字符串。一旦它正常工作,然后你可以玩花哨的格式。