简体   繁体   中英

How to update a collection in MongoDB database using php?

I have written a piece of code to take the values from a user through a form and updating the collection based on the product ID entered by the user. The piece of code works but doesn't produce the right results. I guess I'm not able to get/fetch the correct value of product ID that's why this is happening. Any help would be appreciated. Also I'm new to php and MongoDB.

<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
        <script>tinymce.init({ selector:'textarea' });</script>
        <style>
                .header_wrapper{
                            width:1000px;
                            height:100px;
                            margin:auto;
                                }
                .main_wrapper{
                            width:1000px;
                            height:auto;
                            margin:auto;
                            }
        </style>
    </head>
    <body bgcolor="#212121">


    <div class="main_wrapper">
    <!--Header Container starts here-->
    </div>
    <div>
        <form action="update_product.php" method="post" enctype="multipart/form-data">



        <table align="center" width="700" border="2" bgcolor="#546e7a">


            <tr align="center">
                <td colspan="8"><h2>Update Product</h2></td>
            </tr>
                <td>
                        <?php
                                $m = new MongoClient();
                                $db = $m->ecommerce;
                                $coll = $db->product;
                                $cursor=$coll->find();
                                $product_id=$cursor->count();
                                $product_id++;


                                //echo "<b>&nbsp;$product_id</b>";
                        ?>

                </td>
            <tr>
                <td align="right"><b>Product ID:</b></td>
                <td><input type="text" name="product id" size="60" />
            </tr>
            <tr>
                <td align="right"><b>Product Name:</b></td>
                <td><input type="text" name="product_name" size="60" />
            </tr>
            <tr>
                <td align="right"><b>Product Description:</b></td>
                <td><textarea name="product_desc" cols="20" rows="10" ></textarea>
            </tr>
            <tr>
                <td align="right"><b>Product Image:</b></td>
                <td><input type="file" name="product_image"/>
            </tr>
            <tr>
                <td align="right"><b>Product Price:</b></td>
                <td><input type="text" name="product_price" size="60" />
            </tr>
            <tr>
                <td align="right"><b>Product Stock:</b></td>
                <td><input type="text" name="product_stock" size="60" />
            </tr>
            <tr align="center">
                <td colspan="8"><input type="submit" name="update_post" value="Update"/>
            </tr>

        </table>
        </form>
        <table align="center" width="700" border="8" bgcolor="#546e7a">
        <tr align="center">
                <td><form action="main.php"><button type="submit">Home</button></form></td>
            </tr>
        </table>
    </div>

    </body>
</html>
<?php

    $m = new MongoClient();
    $db = $m->ecommerce;
    $coll = $db->product;
    $cursor=$coll->find();
    $product_id=$cursor->count();
    $id = $_GET['product id'];
    $criteria = $_GET['product id'];

        $product_id=$_POST['product id'];
        $product_name=$_POST['product_name'];
        $product_desc=$_POST['product_desc'];
        $product_price=$_POST['product_price'];
        $product_stock=$_POST['product_stock'];
        $product_image=$_FILES['product_image']['name'];
        $product_image_tmp=$_FILES['product_image']['tmp_name'];

        move_uploaded_file($product_image_tmp,"product_images/$product_image");

        $doc=array('product id'=>$product_id,
                    'product name'=>$product_name, 
                    'product desc'=>$product_desc,
                    'product price'=>$product_price, 
                    'product stock'=>$product_stock,
                    'product image'=>$product_image,
                    );
                    $newdata = array('$set'=>$doc);
                    $options = array("upsert"=>false,"multiple"=>false);
    echo "
        <form>
        <table align='center' width='700' border='2' bgcolor='white'>


            <tr align='center'>
                <td colspan='8'><h2>Updated Product Details</h2></td>
            </tr>

            <tr>
                <td align='right'><b>Product ID:</b></td>
                <td>$product_id</td>
            </tr>
            <tr>
                <td align='right'><b>Product Name:</b></td>
                <td>$product_name</td>
            </tr>
            <tr>
                <td align='right'><b>Product Description:</b></td>
                <td>$product_desc</td>
            </tr>
            <tr>
                <td align='right'><b>Product Image:</b></td>
                <td><img src='product_images/$product_image' height='180' width='180'/><td/>
            </tr>
            <tr>
                <td align='right'><b>Product Price:</b></td>
                <td>$product_price<td/>
            </tr>
            <tr>
                <td align='right'><b>Product stock:</b></td>
                <td>$product_stock<td/>
            </tr>
        </table>

        </form> ";
        $ret = $coll->update(
            $criteria,
            $newdata,
            $options
        );

You are using spaces in your array try removing the spaces and see if this works.

$doc=array('productid'=>$product_id,

'productname'=>$product_name, 

'productdesc'=>$product_desc,

'productprice'=>$product_price, 

'productstock'=>$product_stock,

'productimage'=>$product_image
 );

You will also need to do same in your MongoDB collection and remove the spaces.

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