UPDATE table saying successful but not updating?

时间:2015-07-08 15:37:42

标签: php mysql

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. I am wanting to update the cat_name, cat_color & cat_icon.

I show the data from the cats table to edit by grabbing the id like so:

// 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 :S I have error reporting on but no errors are returned?

Now I get the rror:

The document name has already been used.

1 个答案:

答案 0 :(得分:2)

@RiggsFolly在comment

中提出了最有效的观点
  

STOP!认为! CODE!

其中最重要的是停止!无论你做什么,停止编写代码,开始阅读文档,思考它。

mysqli和准备好的陈述。尝试简单的脚本,没有表单,验证,除了简单的select语句之外什么都不做,直到你很清楚。甚至在此之前,您似乎对正确的MySQL语法感到困惑。这是无效的,它永远不会运行:

  

UPDATE cats SET cat_name,cat_color,cat_icon

我不会在这里写如何做到这一点因为它是sql的基础,你必须能够在进行一个非常简单的谷歌搜索后自己学习,例如basic update sql syntax。无论如何,无论如何我都会spell it out for you

逻辑。在您的脚本顶部,检查您是否有来自$_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
}

停止使用验证,查询,更新和控制流编码表单。从简单的脚本开始,在将它们合并在一起之前使程序的每一小部分工作,因为现在你不会到达任何地方,有太多的事情你做错了。