社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

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

Arham Jamal • 5 年前 • 1737 次点击  

<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
        );
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/57175
 
1737 次点击  
文章 [ 1 ]  |  最新文章 5 年前
James
Reply   •   1 楼
James    6 年前

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

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

'productname'=>$product_name, 

'productdesc'=>$product_desc,

'productprice'=>$product_price, 

'productstock'=>$product_stock,

'productimage'=>$product_image
 );

您还需要在MongoDB集合中执行同样的操作,并移除空间。