Php 更新数据库
当我更改产品数量并进行更新时,会发送数据,但数据会丢失,更新命令实际上会将“总计”列设置为0(实际上更新代码工作正常,直到我编辑表单页面,将总计包含在底部,下面是屏幕截图和更新代码) 格式脚本Php 更新数据库,php,mysql,Php,Mysql,当我更改产品数量并进行更新时,会发送数据,但数据会丢失,更新命令实际上会将“总计”列设置为0(实际上更新代码工作正常,直到我编辑表单页面,将总计包含在底部,下面是屏幕截图和更新代码) 格式脚本 <?php require_once("include/session.php");?> <?php require_once("include/dataconnect.php");?> <?php require_once("include/functions.
<?php require_once("include/session.php");?>
<?php require_once("include/dataconnect.php");?>
<?php require_once("include/functions.php");?>
<?php include("include/basicheader.php");?>
<div class="y-acc-contain">
<div class="page-top">
<table width="100% class="y-acc-pagehead">
<tbody>
<tr>
<td>
<h1>
"Your account"<?php echo $_SESSION['username'];?>
</h1>
</td>
</tr>
</tbody>
</table>
</div>
<div class="column-holder">
<div class="primary">
<div class="action-box rounded">
<div class="titlebar">
<h2>Order Form</h2>
</div>
<div class="listbar">
<div>
<?php
$submit = $_POST['Add'];
//form data
$Sname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Sname'])));
$Pname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pname'])));
$Pidno = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pidno'])));
$Psize = mysql_real_escape_string(htmlentities(strip_tags($_POST['Psize'])));
$Pcolour = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pcolour'])));
$Pquantity = $_POST['Pquantity'];
$Weblink = mysql_real_escape_string(htmlentities(strip_tags($_POST['Weblink'])));
$Price = mysql_real_escape_string(htmlentities(strip_tags($_POST['Price'])));
$date = date("Y-m-d");
if('POST' === $_SERVER['REQUEST_METHOD'])
{
if ($Sname&&$Pname&&$Pidno&&$Weblink&&$Price)
{
if (is_numeric($Price))
{
$repeatheck = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}' AND Pidno ='$Pidno' AND Sname='$Sname'");
$count = mysql_num_rows($repeatheck);
if($count!=0)
{
die ('PRODUCT ALREADY IN BASKET YOU CAN INCREASE OR DECREASE QUANTITY');
}
else
$tprice = $Price * $Pquantity;
$queryreg = mysql_query("
INSERT INTO repplac VALUES ('','$Sname','$Pname','$Pidno','$Psize','$Pcolour','$Pquantity','$Weblink','$Price','$tprice','$date','{$_SESSION['username']}')
")or die(mysql_error());
}
else
echo 'price field requires numbers';
}
else
echo 'please fill in all required * fields ';
}
?>
</div>
<form action='youraccount.php' method='Post' class='slistbar'>
<!--<div>
<label for='shoppinglist' class='fixedwidth'></label>
<textarea type='text' name='shoppinglist' id='username' cols='100' rows='15'></textarea>
</div> -->
<div>
<label for='Sname' class='fixedwidth'> * Shop name</label>
<input type='text' name='Sname' id='Sname'/>
</div>
<div>
<label for='Pname' class='fixedwidth'> * Product name</label>
<input type='text' name='Pname' id='Pname'/>
</div>
<div>
<label for='Pidno' class='fixedwidth'> * Product id no /ad reference</label>
<input type='text' name='Pidno' id='Pidno'/>
</div>
<div>
<label for='Psize' class='fixedwidth'>Product size</label>
<input type='text' name='Psize' id='Psize'/>
</div>
<div>
<label for='Pcolour' class='fixedwidth'>Product colour</label>
<input type='text' name='Pcolour' id='Pcolour'/>
</div>
<div>
<label for='Pquantity' class='fixedwidth'>Product quantity</label>
<select name="Pquantity" id="Pquantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div>
<label for='Weblink' class='fixedwidth'> * Web link</label>
<input type='text' name='Weblink' id='Weblink'/>
</div>
<div>
<label for='Price' class='fixedwidth'> * Price GBP</label>
<input type='text' name='Price' id='Price'/>
</div>
<div>
<div class='buttonarea'>
<p>
<input type='submit' name='submit' value='Add'>
</p>
</div>
</p>
</form>
</div>
</div>
<div class="action-box rounded">
<div class="titlebar">
<h2>Shopping List</h2>
<a href='totalprice.php'>Update</a>
</div>
<div class="listbar">
<form action='orderpplac.php' method='Post' class='slistbar'>
<table border='1'>
<tr>
<th>SHOP NAME</th>
<th>PRODUCT NAME</th>
<th>PRODUCT SIZE</th>
<th>PRODUCT COLOUR</th>
<th>PRODUCT QUANTITY</th>
<th>PRICE</th>
<th>TOTAL</th>
<th></th>
</tr>
<?php
// Get DB results and loop, outputting table rows with counter
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error());
while ($row = mysql_fetch_assoc($pplresult)) {
echo "
<tr>
<td>".htmlspecialchars($row['Sname'])."</td>
<td>".htmlspecialchars($row['Pname'])."</td>
<td>".htmlspecialchars($row['Psize'])."</td>
<td>".htmlspecialchars($row['Pcolour'])."</td>
<td>
<input type='text' name='item[$i][Pquantity]' id='Pquantity' value='".htmlspecialchars($row['Pquantity'])."' />
<input type='hidden' name='item[$i][Pidno]' id='Pidno' value='".htmlspecialchars($row['Pidno'])."' />
</td>
<td>".htmlspecialchars($row['Price'])."</td>
<td>
<input type='text' name=tprice value='".htmlspecialchars($row['Tprice'])."' readonly>
</td>
<td><a href='deleteproduct.php?del=".htmlspecialchars($row['Pidno'])."'>delete</a></td>
</tr>";
}
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error());
while ($row = mysql_fetch_assoc($pplresult))
//echo $row['Pquantity'] * $row['Price'];
{
$totalprice += $row['Tprice'];
}
//echo "$totalprice";
?>
<tr>
<th>Total Price</th>
<th><?php echo $totalprice; ?></th>
</tr>
<!--Close table and form-->
</table>
<input type='submit' name='submit1' value='UPDATE' />
<input type='submit' name='submit2' value='SUBMIT' />
</form>
</div>
</div>
<div class="action-box rounded">
<div class="titlebar">
<h2> payment & balance</h2>
</div>
<div class="listbar">
</div>
</div>
</div>
</div>
</div>
<?php include("include/footer.php");?>
您从哪里可以在线获得$i
"<input type='text' name='item[$i][Pquantity]' id='Pquantity' value='".htmlspecialchars($row['Pquantity'])."' />"
您的数量被设置为0,因为intval无法将您的数量转换为有效数字。您需要调试$item submit中返回的内容。是打字错误还是更新表单中有两个表单标记?请修复您的代码-缩进和准确度您需要在UP中包含总计计算日期
命令也是…由于您不包括,总计
默认为零。您可以提供完整的代码吗?请删除代码中不必要的注释。例如//echo$row['Pquantity']*$row['Price']
如果这不相关或者对我们没有帮助,那么请不要包含它。当我有post结果时,它会给出这个数组([item]=>Array([0]=>Array([Pquantity]=>2)[1]=>Array([Pidno]=>123))[tprice]=>24[submit2]=>SUBMIT),这没问题,因为对于此字段,我输入的数量是2。您的输出显示$item没有“Pquantity”键。该值位于子数组中。看起来您需要$item[0]['Pquantity']或在循环中$item[$i]['Pquantity']是的,我明白你的意思了。太好了!你介意投票并选择这个作为你的答案吗?
"<input type='text' name='item[$i][Pquantity]' id='Pquantity' value='".htmlspecialchars($row['Pquantity'])."' />"
SELECT SUM(Tprice) FROM repplac WHERE Uname = '{$_SESSION['username']}'