我写了一段代码,通过表格从用户那里获取值,并根据用户输入的产品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> $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
);
答案 0 :(得分:0)
您在数组中使用空格时,请尝试删除空格,看看是否可行。
$doc=array('productid'=>$product_id,
'productname'=>$product_name,
'productdesc'=>$product_desc,
'productprice'=>$product_price,
'productstock'=>$product_stock,
'productimage'=>$product_image
);
您还需要在MongoDB集合中执行相同的操作,并删除空格。