Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php更新MongoDB数据库中的集合?_Php_Html_Mongodb - Fatal编程技术网

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

如何使用php更新MongoDB数据库中的集合?,php,html,mongodb,Php,Html,Mongodb,我已经编写了一段代码,通过表单获取用户的值,并根据用户输入的产品ID更新集合。这段代码可以工作,但不能产生正确的结果。我猜我无法获取产品ID的正确值,这就是为什么会发生这种情况。任何帮助都将不胜感激。另外,我对php和MongoDB还是新手 <script src="//cdn.tinymce.com/4/tinymce.min.js"></script> <script>tinymce.init({ selector:'textarea' }

我已经编写了一段代码,通过表单获取用户的值,并根据用户输入的产品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
        );

init({选择器:'textarea'});
.header\u包装器{
宽度:1000px;
高度:100px;
保证金:自动;
}
.main_包装{
宽度:1000px;
高度:自动;
保证金:自动;
}
更新产品
产品标识:
产品名称:
产品说明:
产品形象:
产品价格:
产品库存:
家

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

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

'productname'=>$product_name, 

'productdesc'=>$product_desc,

'productprice'=>$product_price, 

'productstock'=>$product_stock,

'productimage'=>$product_image
 );

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

集合中“我的文档”的格式包含空格。我的insert和delete函数也可以很好地处理空格。