繁体   English   中英

如何使用php更新MongoDB数据库中的集合?

[英]How to update a collection in MongoDB database using php?

我编写了一段代码,通过表单从用户那里获取值,并根据用户输入的产品 ID 更新集合。 这段代码有效,但没有产生正确的结果。 我想我无法获取/获取正确的产品 ID 值,这就是发生这种情况的原因。 任何帮助,将不胜感激。 我也是 php 和 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
        );

您在数组中使用空格尝试删除空格,看看这是否有效。

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

'productname'=>$product_name, 

'productdesc'=>$product_desc,

'productprice'=>$product_price, 

'productstock'=>$product_stock,

'productimage'=>$product_image
 );

您还需要在 MongoDB 集合中执行相同操作并删除空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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