dongzhi5386 2014-06-01 12:34
浏览 20
已采纳

PHP比较2个整数和更新表与更大的一个

Firstly, sorry for my bad english. I hope you will understand what i need. I started a website with products bidding. I have a products page where users can see all the products and a button that send the user to a form where he cand input a new price for that product and it updates the existing price in the database with the new one. But i want this to happen only if the initial price is lower than the price that user inputs.

How can i do that ? I was thiking that i need another column newprice and compare it with price but i don't know how to do that...

This is the table "produse" :

id | titlu | stare | price | descriere
_____________________________________
 1 |  asd  |  asd  |  140 |    asd

This is the products page (produs_user.php)

<?php
include('connect.php');
?>

<?php
//create the query
$result = mysqli_query($con,"select * from produse");

//return the array and loop through each row
while ($row = mysqli_fetch_array($result))
{
?>

<div class="produse"> 
<div class="produse-header"> 
<h3><?php echo $row['titlu'];?></h3>
<h5><?php echo $row['stare'];?></h5>
</div>
<div class="produse-continut">
<textarea readonly><?php echo $row['descriere'];?></textarea>
</div>
<div class="produse-pret"> 
<p><?php echo $row['price'];?> lei</p>
</div>
<div class="produse-buton"> 
<form action="liciteaza.php">
  <input type="submit" value="Liciteaza">
</form>
</div></div>

<?php 
} 
mysqli_close($con);
?>

The above page sends users to liciteaza.php where i have a field where users inputs the new price This is the liciteaza.php

<?php
include('connect.php');
?>

<?php
//create the query
$result = mysqli_query($con,"select * from produse");

//return the array and loop through each row
while ($row = mysqli_fetch_array($result))
{
?>

<form method="post" action="update_pret.php">
<div class="produse"> 
<div class="produse-header"> 
<h3><?php echo $row['titlu'];?></h3>
<h5><?php echo $row['stare'];?></h5>
</div>
<div class="produse-continut">
<textarea readonly><?php echo $row['descriere'];?></textarea>
</div>
<div class="produse-pret">
<p><input name="price" class="modifica-pret" id="price" value="<?php echo $row['price']; ?>"></p>
<input name="id" type="hidden" id="id" value="<?php echo $row['id']; ?>">
</div>
<div class="produse-buton"> 
  <input type="submit" value="Liciteaza">
</form>
</div></div>

<?php 
} 
mysqli_close($con);
?>

That page send me to update_pret.php where i update the new price to mysql table

This is the update_pret.php

<?php
include('connect.php');

// update data in mysql database 
$result=mysqli_query($con,"UPDATE produse SET price='$_POST[price]' WHERE id='$_POST[id]'");

// if successfully updated. 
if($result)
{
header("location:produse_user.php");
}
else 
{
echo "ERROR";
}
mysqli_close($con);
?>

All these scripts are working perfectly but like i said i need this to work only when the new price chosen by the user is bigger than the initial price.

  • 写回答

4条回答 默认 最新

  • dpwuvfpu52502 2014-06-01 12:39
    关注

    Fist solution : Using IF

    UPDATE `produse` 
    SET `price` = IF(`price` > '$_POST[price]', `price`, '$_POST[price]') 
    WHERE `id` = '$_POST[id]'
    

    Second solution : Using CASE statement:

    UPDATE `produse` 
    SET `price` = (CASE WHEN `price` > '$_POST[price]' THEN `price` ELSE  '$_POST[price]' END)
    WHERE `id` = '$_POST[id]'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?