user1946688
user1946688

Reputation:

Update info in mysql using _POST

I have this code that does not do anything but add to my database and only adds the item ounces and retail price. any ideas on where i am going wrong?

--update.php---

<?php

        session_start(); // start session cookies
        require("Login.class.php"); // pull in file
        $login = new Login; // create object login

        $login->authorize(); // make user login
?>




<style type="text/css">
        body {background-color:#42520e; color: #f0cb01;

        }
        th {background: URL(http://www.athenahealth.com/_img/boxes/carousel_bg.png);}
        p {color:blue;}
        a:link {color: #f0cb01; text-decoration: underline; }
        a:active {color: #f0cb01; text-decoration: underline; }
        a:visited {color: #f0cb01; text-decoration: underline; }
        a:hover {color: #f0cb01; text-decoration: none; }
</style>

<?php
        $retail=$_REQUEST['retail'];
        $id=$_REQUEST['id'];
        $name=$_REQUEST['name'];
        $upc=$_REQUEST['upc'];
        $ounces=$_REQUEST['ounces'];
        define("HOST", "localhost");

        $retail=$_REQUEST['retail'];
        $id=$_REQUEST['id'];
        $name=$_REQUEST['name'];
        $upc=$_REQUEST['upc'];
        $ounces=$_REQUEST['ounces'];


?>
Admin Panel to Update Keywords
<hr>

    <table>
            <tr><td valign="top"><br /><br />
            <td>    
                    Your are editing information for Product #: <b><? echo $_REQUEST['id']; ?></b>
                    <form method="post" action="update_ac.php">
                            <table>         <tr><br /></tr>
                                    <tr><td>  Product #:  </td><td> <input type="text" name="id" id="id" value="<? echo $_REQUEST['id']; ?>">* Enter Keyword as it currently appears<br></td></tr>
                                    <tr><td> Name: </td><td><input type="text" name="name" id="name" value="<? echo $_REQUEST['name']; ?>">* Enter New Information<br></td></tr>
                                    <tr><td> Suggested Retail:</td><td> <input type="text" name="retail" id="retail" value="<? echo $_REQUEST['retail']; ?>">* New Phone Number<br></td></tr>
                                    <tr><td> Ounces: </td><td> <input type="text" name="ounces" id="ounces" value="<? echo $_REQUEST['ounces']; ?>">* Enter new block description here. <br></td></tr>
                                    <tr><td> UPC: </td><td> <input type="text" name="upc" id="upc" value="<? echo $_REQUEST['upc']; ?>">* Enter new block description here. <br></td></tr>

                                    <tr><td> </td><td align="left"> <input type="submit" name="submit" value="Submit Data"></td></tr>
                            </table>
                    </form>
            </td></tr>
    </table>                <br />
<a href="index.php">Add Keyword</a> | <a href="../admin">Back to search form</a> | 


<?php 
       echo '<pre>'; 
        print($retail); 
        echo '</pre>'; 
?> 




<a href="index.php?action=clear_login">logout</a>
</body>

and this is the update link that it uses and carries it over to the update_ac.php

 <?php

    session_start(); // start session cookies
    require("Login.class.php"); // pull in file
    $login = new Login; // create object login

    $login->authorize(); // make user login
    ?>

<style type="text/css">
        body {background-color:#42520e; color: #f0cb01;

        }
        th {background: URL(http://www.athenahealth.com/_img/boxes/carousel_bg.png);}
        p {color:blue;}
        a:link {color: #f0cb01; text-decoration: underline; }
        a:active {color: #f0cb01; text-decoration: underline; }
        a:visited {color: #f0cb01; text-decoration: underline; }
        a:hover {color: #f0cb01; text-decoration: none; }
</style>

<?php
        $retail=$_REQUEST['retail'];
        $id=$_REQUEST['id'];
        $name=$_REQUEST['name'];
        $upc=$_REQUEST['upc'];
        $ounces=$_REQUEST['ounces'];


        define("HOST", "localhost");

        $retail=$_REQUEST['retail'];
        $id=$_REQUEST['id'];
        $name=$_REQUEST['name'];
        $upc=$_REQUEST['upc'];
        $ounces=$_REQUEST['ounces'];


        // Database user
        define("DBUSER", "root");

        // Database password
        define("PASS", "Password!");

        // Database name
        define("DB", "SnyderLanceSku");

        ############## Make the mysql connection ###########

        $conn = mysql_connect(HOST, DBUSER, PASS) or  die('Could not connect !<br />Please contact the site\'s administrator.');

        $db = mysql_select_db(DB) or  die('Could not connect to database !<br />Please contact the site\'s administrator.');

        mysql_query("UPDATE products SET UPC='$upc', Name='$name', Item_Ounces='$ounces' WHERE UPC='$upc' ") or die (mysql_error());  
?>
<table>
        <tr><td valign="top"><br /><br />
        <td>    
                <table>  <tr><b>Data Updated Successfully</b></tr>       
                        <tr><td>  Keyword:  </td><td><? echo $id; ?><br></td></tr>
                        <tr><td> Block?: </td><td><? echo $name; ?><br></td></tr>
                        <tr><td> Phone #:</td><td><?php echo $upc; ?><br> </td></tr>
                        <tr><td> Reason: </td><td><? echo $retail; ?></td></tr>
                        <tr><td> Reason: </td><td><? echo $ounces; ?></td></tr>

                </table>
        </form>
        </td></tr>
</table>

<a href="./">Go Back</a>

I'm not really sure where to look here... any help is appreciated.

EDIT:

Here is both scripts. It almost works correct. ONly thing now is it is not updating the correct row. its ADDING a new row instead of updating on id or in this case updating on UPC

Upvotes: 0

Views: 1398

Answers (1)

Script47
Script47

Reputation: 14540

You have a few issues with this code, from the code you've provided it doesn't seem as though you're sending POST requests rather you're sending GET requests as you're passing them through the URL. So first thing, change your $_POST to $_GET.

Also your query is wrong.

$sql = "UPDATE `products` SET ".implode(", ", $update)."WHERE Product_sku = '$ID'";

It should be,

$sql = "UPDATE `products` SET ".implode(", ", $update)."WHERE Product_sku = '$id'";

I don't see in your code a $ID variable, but I do see a $id variable. So you need to change that too.

Edit 1

Also, turn on error reporting to make debugging easier.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);

Edit 2

As Fred said, you're mistyping when trying to get data from HTML to PHP.

In your product input you set the name value to Product as you do with the Name input too. Yet in your PHP you're doing $name=$_GET['name']; when it should be $name=$_GET['Name'];.

You should adopt a familiar naming convention such as camelCase which will make it harder to make these errors which are hard to spot. I use the PSR coding conventions personally. So try to adopt one and stick to it.

Another thing, because you never posted your form earlier I suggested you use $_GET, however for certain things (such as the form data) you should change it back to $_POST as your form method="post".

Edit 3

You should also get in to the habit of checking if variables actually contain a value and are correctly set, this is known as validation. I would personally check if the submit button has been pressed (if (isset($_POST['yourSubmitButtonNameValue'])) then validate the rest of the fields within that if statement. This way, you know the input you're adding in to your database is valid.

Upvotes: 1

Related Questions