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