2016-04-21 90 views
0

我有一個問題,我有一個下拉列表擁有所有者的姓氏,一旦我選擇它,然後按刪除按鈕,它應該從下拉列表中刪除所有者名稱與mySQL數據庫中的任何關聯的所有者信息和船信息。我寫了@sql查詢來執行刪除功能,但似乎沒有刪除它。PHP刪除不工作,並顯示結果表

一旦用戶點擊刪除按鈕,我怎樣才能打印出表(所有者表和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] 

任何幫助做到這一點,將不勝感激,謝謝提前

+0

你在字符串中構造你的DELETE語句,但你永遠不會執行它。這會有所作爲。 –

+0

那我該如何解決這個問題? – user3325133

回答

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 

通過使用準備好的語句,你獲得AUTOMAGIC逃逸,報價和變量類型匹配。

另外,不要過分複雜的事情。爲了讓您的表格顯示,

$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表的查詢字符串。一旦這個工作,然後你可以玩花式格式。

+0

感謝您解決了第一個問題,但是當我點擊刪除時,爲什麼它不會刪除名稱,直到我刷新頁面?一旦我點擊刪除按鈕(我想在下面顯示兩個表格),我該如何實際顯示這兩個表格? – user3325133

+0

您可以使用與生成''會在您重新加載頁面時更新,當您提交表單時應該會發生這種情況,除非您做了一些奇怪的事情。 –

+0

對不起...你能給我一個你的意思的例子。我看到回聲