简体   繁体   English

UPDATE表说成功但未更新?

[英]UPDATE table saying successful but not updating?

I am trying to UPDATE a table with a form I have - so I retrieve some information from the form which is for aesthetics and I can edit the information and then update the table. 我正在尝试使用自己拥有的表单来更新表格-因此我从表格中检索了一些信息,以达到美观目的,我可以编辑该信息,然后更新表格。 I have a table called cats which is a categories table. 我有一个名为cats的表,它是一个类别表。 I am wanting to update the cat_name, cat_color & cat_icon. 我想更新cat_name,cat_color和cat_icon。

I show the data from the cats table to edit by grabbing the id like so: 我通过抓取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();
}

I then go on to retrieve the data: 然后,我继续检索数据:

// 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> ';
            ?>

My form continues: 我的表格继续:

 <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>

The script which actions the submit looks like this: 操作提交的脚本如下所示:

<?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>';
        }

}
?>

Now it reports back to me that the script works and gives mt the all clear but the table isnt updating :SI have error reporting on but no errors are returned? 现在它向我报告该脚本可以正常工作,并且使mt一切正常,但表未更新:SI上有错误报告,但没有返回错误?

Now I get the rror: 现在我得到了错误:

The document name has already been used.

The most valid point has been made by @RiggsFolly in a comment : 最有效的观点是@RiggsFolly在评论中指出的:

STOP! 停! THINK! 认为! CODE! 码!

with the most important of them being STOP! 其中最重要的是停止! Whatever you are doing, stop writing code, and start reading documentation, thinking about it. 无论您做什么,都停止编写代码,并开始阅读文档,对它进行思考。

mysqli and prepared statements. mysqli和准备好的语句。 Try simple scripts, without forms, validation, without anything but a simple select statement until you know it very well. 尝试简单的脚本,不使用表单,进行验证,只需要简单的select语句就可以了,直到您非常了解为止。 And even before that, you seem confused with proper MySQL syntax. 甚至在此之前,您似乎仍对正确的MySQL语法感到困惑。 This is not valid, it would not run ever: 这是无效的,它将永远不会运行:

UPDATE cats SET cat_name, cat_color, cat_icon 更新cats set cat_name,cat_color,cat_icon

I will not write here how to do it because it is the very basis of sql, which you must be able to learn by yourself after making a very simple google search such as basic update sql syntax . 我不会在这里写该怎么做,因为它是sql的基础,您必须在进行非常简单的google搜索(例如basic update sql syntax后才能自己学习。 Whatever, I'll spell it out for you anyway. 无论如何,我还是会为您说明

logic. 逻辑。 At the top of your script, you check if you have an id coming from either a $_GET or $_POST , yet you accept only the post method: 在脚本的顶部,检查您是否有来自$_GET$_POST的ID,但仅接受post方法:

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

This sounds strange, why bother accepting ids from $_GET if you do not accept them anyway? 这听起来很奇怪,如果您仍然不接受$_GET ID,为什么还要麻烦呢?

Validation . 验证 php has built-in validation functions like filter_input() , you should use them: php具有内置的验证功能,例如filter_input() ,您应该使用它们:

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

Reporting errors and messages . 报告错误和消息 Stop making custom error messages which at best are wrong, and at worst will throw you off in bad directions. 停止制作自定义错误消息,这些消息充其量是错误的,而在最坏的情况下,您会被带到错误的方向。 If mysqli_num_rows is not 0 it doesn't mean that the document name has already been used, especially if used after a query selecting all the rows in a table. 如果mysqli_num_rows不为0,则并不意味着文档名称已被使用,尤其是在查询中选择了表中的所有行之后使用的文档名称。

If you must absolutely echo messages to your user based on your query results, you can use the value returned by the execute() function, which will tell you a whole lot more than the error-prone affected rows value. 如果您必须根据查询结果绝对向用户回显消息,则可以使用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
}

Stop coding forms with validation, queries, updates and control-flow. 停止使用验证,查询,更新和控制流对表单进行编码。 Start with simple scripts, and make every small bits of your program work before merging them together, because right now you won't get anywhere, there are too many things you are doing wrong. 从简单的脚本开始,在合并它们之前使程序的每一小部分都可以工作,因为现在您什么都看不到,有太多事情做错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM