簡體   English   中英

PHP PDO Crud建議

[英]PHP PDO Crud advice

我剛剛開始學習PDO,並且已經設法在一個表上執行簡單的CRUD操作。

我只是從表中執行SELECT *。 但是此表具有另一個表的外鍵,我寧願在該列上顯示值而不是ID。

所以我的表結構如下。 我有一個具有ID和joketext的笑話表以及一個外鍵authorId。 author表具有authorId和作者名稱。

與其在笑話表上執行SELECT *,不如使用以下代碼創建視圖:

SELECT
joke.joketext,
author.name
FROM
author
INNER JOIN joke
 ON author.id = joke.authorid

但是對於CRUD操作,我想改為在下拉列表中顯示author.name,這樣用戶就不會錯誤地輸入錯誤的值。

這就是index.php的樣子:

<?php
//including the database connection file
include_once("config.php");

//fetching data in descending order (lastest entry first)
$result = $dbConn->query("SELECT * FROM joke ORDER BY id DESC");
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <a href="add.html">Add New Data</a><br/><br/>

    <table width='80%' border=0>

    <tr bgcolor='#CCCCCC'>
        <td>Joke</td>
        <td>AuthorId</td>
        <td>Update</td>
    </tr>
    <?php     
    while($row = $result->fetch(PDO::FETCH_ASSOC)) {         
        echo "<tr>";
        echo "<td>".$row['joketext']."</td>";
        echo "<td>".$row['authorid']."</td>";
        echo "<td><a href=\"edit.php?id=$row[id]\">Edit</a> | <a href=\"delete.php?id=$row[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";        
    }
    ?>
    </table>

我的編輯文件如下所示:

<?php
// including the database connection file
include_once("config.php");

if(isset($_POST['update']))
{    
    $id = $_POST['id'];

    $name=$_POST['joketext'];
    $authorid=$_POST['authorid']; 

    // checking empty fields
    if(empty($joketext) || empty($authorid)) {    

        if(empty($joketext)) {
            echo "<font color='red'>Name field is empty.</font><br/>";
        }

        if(empty($authorid)) {
            echo "<font color='red'>Author field is empty.</font><br/>";
        }


    } else {    
        //updating the table
        $sql = "UPDATE joke SET joke=:joketext, authorid=:authorid WHERE id=:id";
        $query = $dbConn->prepare($sql);

        $query->bindparam(':id', $id);
        $query->bindparam(':joketext', $joketext);
        $query->bindparam(':authorid', $authorid);
        $query->execute();

        // Alternative to above bindparam and execute
        // $query->execute(array(':id' => $id, ':name' => $name, ':email' => $email, ':age' => $age));

        //redirectig to the display page. In our case, it is index.php
        header("Location: index.php");
    }
}
?>
<?php
//getting id from url
$id = $_GET['id'];

//selecting data associated with this particular id
$sql = "SELECT * FROM joke WHERE id=:id";
$query = $dbConn->prepare($sql);
$query->execute(array(':id' => $id));

while($row = $query->fetch(PDO::FETCH_ASSOC))
{
    $joketext = $row['joketext'];
    $authorid = $row['authorid'];
}
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <a href="index.php">Home</a>
    <br/><br/>

    <form name="form1" method="post" action="edit.php">
        <table border="0">
            <tr> 
                <td>Joke</td>
                <td><input type="text" name="joketext" value="<?php echo $joketext;?>"></td>
            </tr>
            <tr> 
                <td>Author</td>
                <td><input type="text" name="authorid" value="<?php echo $authorid;?>"></td>
            </tr>

            <tr>
                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?></td>
                <td><input type="submit" name="update" value="Update"></td>
            </tr>
        </table>
    </form>

有人可以告訴我至少有關php代碼的編輯操作提示嗎?

謝謝

如果您希望在編輯頁面上提供可供選擇的作者供用戶選擇的元素,而不是讓他們輸入作者的ID號,則可以從數據庫中選擇所有作者並循環遍歷,從而構建選擇元素的選項。 select元素可以向用戶顯示作者的姓名,但可以將作者的ID傳遞回服務器。 您還可以預選擇一個作者,以在默認情況下向用戶顯示當前關聯的作者,並且只有在錯誤的情況下,他們才需要對其進行更改。

因此,首先,從數據庫中選擇所有作者:

$authorSql = 'SELECT * FROM author';
$authorQuery = $dbConn->prepare($authorSql);
$authorQuery->execute();

然后使用該數據構建一個select元素:

<select name="authorid">
<?php
    while($author = $authorQuery->fetch(PDO::FETCH_ASSOC)) {
        if ($author['id'] == $authorid) {
            //The author is currently associated to the joke, select it by default
            echo "<option value=\"{$author['id']}\" selected>{$author['name']}</option>";
        } else {
            //The author is not currently associated to the joke
            echo "<option value=\"{$author['id']}\">{$author['name']}</option>";
        }
    }
?>
</select>

輸出可能看起來像這樣:

<select name="authorid">
    <option value="1">George Carlin</option>
    <option value="2" selected>Richard Pryor</option>
    <option value="3">Don Rickles</option>
</select>

無論用戶選擇什么選項,他們都會在頁面上看到<option></option>標記之間的內容,但是表單會將value屬性的value作為authorid參數傳遞給服務器。

生成select元素的代碼替換<input type="text" name="authorid" value="<?php echo $authorid;?>">並保留在<td></td>標記內。

希望我能夠解決您的實際需求,如果我錯過了您提出問題的意圖,請告訴我。

注意:我的代碼未經測試,因此可能需要進行一些調整。


編輯#1:修正了不正確的變量名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM