簡體   English   中英

UPDATE表說成功但未更新?

[英]UPDATE table saying successful but not updating?

我正在嘗試使用自己擁有的表單來更新表格-因此我從表格中檢索了一些信息,以達到美觀目的,我可以編輯該信息,然后更新表格。 我有一個名為cats的表,它是一個類別表。 我想更新cat_name,cat_color和cat_icon。

我通過抓取id來顯示cats表中的數據以進行編輯,如下所示:

// Check for a valid document ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_cats.php
    $id = $_GET['id'];

} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form submission.
    $id = $_POST['id'];

} else { // No valid ID, kill the script.
    echo '<p class="error">This page has been accessed in error.</p>';
    exit();
}

然后,我繼續檢索數據:

// Retrieve the document's information:
    $q = "SELECT * FROM cats WHERE cat_id=$id"; 

    $r = mysqli_query ($dbc, $q);

    if (mysqli_num_rows($r) == 1) { // Valid document ID, show the form.


        // Get the document's information:
        $row = mysqli_fetch_array ($r, MYSQLI_NUM);


        // Create the form:
        echo '<form action="actions/edit_cat.php" method="post">

        <div class="row">
            <div class="col-group-two-third">
                <input placeholder="Category Name" type="text" name="cat_name" value="' . $row[1] . '" />
            </div>
        </div> ';
            ?>

我的表格繼續:

 <input type="radio" id="radio1" name="cat_color" value="#d31b26" required>
                    <label for="radio1"><div class="redSelect" onclick="button_click('#d31b26');"></div></label>

                    <input type="radio" id="radio2" name="cat_color" value="#f9c04c" required>
                    <label for="radio2"><div class="yellowSelect" onclick="button_click('#f9c04c');" ></div></label>

                    <input type="radio" id="radio3" name="cat_color" value="#ec9292" required>
                    <label for="radio3"><div class="pinkSelect" onclick="button_click('#ec9292');"></div></label>

                    <input type="radio" id="radio4" name="cat_color" value="#b7d04e" required>
                    <label for="radio4"><div class="greenSelect" onclick="button_click('#b7d04e');"></div></label> 

                    <input type="radio" id="radio5" name="cat_color" value="#637a91" required>
                    <label for="radio5"><div class="slateSelect" onclick="button_click('#637a91');"></div></label> 

                    <input type="radio" id="radio6" name="cat_color" value="#AEA8D3" required>
                    <label for="radio6"><div class="purpleSelect" onclick="button_click('#AEA8D3');"></div></label> 

                    <input type="radio" id="radio13" name="cat_color" value="#72bce9" required>
                    <label for="radio13"><div class="blueSelect" onclick="button_click('#72bce9');"></div></label>  

                <input type="radio" id="radio7" type="radio" name="cat_icon" value='<i class="fa fa-phone" style="font-size: 2em;"></i>' required>
                <label for="radio7"><div class="iconSelect" onclick="button_click_icon1()"><i class='fa fa-phone' style='font-size: 2em;'></i></div></label>

                <input type="radio" id="radio8" type="radio" name="cat_icon" value='<i class="fa fa-graduation-cap" style="font-size: 2em;"></i>' required>
                <label for="radio8"><div class="iconSelect" onclick="button_click_icon2()"><i class='fa fa-graduation-cap' style='font-size: 2em;'></i></div></label>

                <input type="radio" id="radio9" type="radio" name="cat_icon" value='<i class="fa fa-users" style="font-size: 2em;"></i>' required>
                <label for="radio9"><div class="iconSelect" onclick="button_click_icon3()"><i class='fa fa-users' style='font-size: 2em;'></i></div></label>

                <input type="radio" id="radio10" type="radio" name="cat_icon" value='<i class="fa fa-question-circle" style="font-size: 2em;"></i>' required>
                <label for="radio10"><div class="iconSelect" onclick="button_click_icon4()"><i class='fa fa-question-circle' style='font-size: 2em;'></i></div></label>

                <input type="radio" id="radio11" type="radio" name="cat_icon" value='<i class="fa fa-file-text" style="font-size: 2em;"></i>' required>
                <label for="radio11"><div class="iconSelect" onclick="button_click_icon5()"><i class='fa fa-file-text' style='font-size: 2em;'></i></div></label>

                <input type="radio" id="radio12" type="radio" name="cat_icon" value='<i class="fa fa-at" style="font-size: 2em;"></i>' required>
                <label for="radio12"><div class="iconSelect" onclick="button_click_icon6()"><i class='fa fa-at' style='font-size: 2em;'></i></div></label>  

                <input type="radio" id="radio12" type="radio" name="cat_icon" value='<img src="../img/pudsey.png" /><' required>
                <label for="radio12"><div class="iconSelect_alt" onclick="button_click_icon7()"><img src="../img/pudsey.png" /></div></label> 

            <div class="indexBox">
                <div style="<?php echo 'background-color: '.$row[2].'' ?>" class="indexBoxHeader" id="box">
                    <siv id="icon"><?php echo ''.$row[3].'' ?></div>
                <div class="indexBoxFooter">
                    <div class='printchatbox' id='printchatbox'></div>
                </div>
            </div>

操作提交的腳本如下所示:

<?php

require ('../../db_con.php'); 

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        $q = "SELECT * FROM cats";

        $r = mysqli_query($dbc, $q);

        if (mysqli_num_rows($r) == 0) {

            // Make the query:
            $q = "UPDATE cats SET cat_name, cat_color, cat_icon";

            if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.

                // Print a message:
                echo '<p>The document has been edited.</p>';    

            } else { // If it did not run OK.
                echo '<p>FAIL!</p>';    

            }

        } else { // Already used.
            echo '<p class="error">The document name has already been used.</p>';
        }

}
?>

現在它向我報告該腳本可以正常工作,並且使mt一切正常,但表未更新:SI上有錯誤報告,但沒有返回錯誤?

現在我得到了錯誤:

The document name has already been used.

最有效的觀點是@RiggsFolly在評論中指出的:

停! 認為! 碼!

其中最重要的是停止! 無論您做什么,都停止編寫代碼,並開始閱讀文檔,對它進行思考。

mysqli和准備好的語句。 嘗試簡單的腳本,不使用表單,進行驗證,只需要簡單的select語句就可以了,直到您非常了解為止。 甚至在此之前,您似乎仍對正確的MySQL語法感到困惑。 這是無效的,它將永遠不會運行:

更新cats set cat_name,cat_color,cat_icon

我不會在這里寫該怎么做,因為它是sql的基礎,您必須在進行非常簡單的google搜索(例如basic update sql syntax后才能自己學習。 無論如何,我還是會為您說明

邏輯。 在腳本的頂部,檢查您是否有來自$_GET$_POST的ID,但僅接受post方法:

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

這聽起來很奇怪,如果您仍然不接受$_GET ID,為什么還要麻煩呢?

驗證 php具有內置的驗證功能,例如filter_input() ,您應該使用它們:

if (isset($_GET['id'])) {
    $id = filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT);
}

報告錯誤和消息 停止制作自定義錯誤消息,這些消息充其量是錯誤的,而在最壞的情況下,您會被帶到錯誤的方向。 如果mysqli_num_rows不為0,則並不意味着文檔名稱已被使用,尤其是在查詢中選擇了表中的所有行之后使用的文檔名稱。

如果您必須根據查詢結果絕對向用戶回顯消息,則可以使用execute()函數返回的值,這將比易受錯誤影響的行值提供更多信息。

$q = "UPDATE cats SET cat_name='new_name'";
$s = $dbc->prepare($q);
if ($s->execute()) {
    // the update was successful, tell it to your user
} else {
    // the update was not successful, do something about it
}

停止使用驗證,查詢,更新和控制流對表單進行編碼。 從簡單的腳本開始,在合並它們之前使程序的每一小部分都可以工作,因為現在您什么都看不到,有太多事情做錯了。

暫無
暫無

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

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