duan2477 2015-08-05 23:59
浏览 31
已采纳

使用_POST更新mysql中的信息

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

  • 写回答

1条回答 默认 最新

  • dpu66046 2015-08-06 00:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误