简体   繁体   中英

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. 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 :SI have error reporting on but no errors are returned?

Now I get the rror:

The document name has already been used.

The most valid point has been made by @RiggsFolly in a comment :

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. Try simple scripts, without forms, validation, without anything but a simple select statement until you know it very well. And even before that, you seem confused with proper MySQL syntax. This is not valid, it would not run ever:

UPDATE 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 . 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:

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

This sounds strange, why bother accepting ids from $_GET if you do not accept them anyway?

Validation . php has built-in validation functions like filter_input() , you should use them:

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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