dousha7645 2017-11-17 07:14
浏览 275
已采纳

如何使用php减去sql

I would like to subtract the quantity in my database using php

part of HTML code

<label class="control-label">Medicine Name</label>
<input type="text" class="form-control" name="medname">
<label class="control-label">Quantity</label>
<input type="text" class="form-control input-sm" name="quantity">
<button type="submit" class="btn" style="background:#830006;" name="submit1">OK</button>

PHP

if (isset($_POST['submit1'])) {
  $mediname = pg_escape_string($_POST['medname']);
  $tits = pg_escape_string($_POST['quantity']);

  $quirt = ("UPDATE medicine_invent set med_stock = med_stock - $tits where med_name = $mediname");
  $sq = pg_query($quirt);
}

please help me. i'm still a beginner at php. Thanks a lot.

  • 写回答

1条回答 默认 最新

  • dqrqp8492 2017-11-17 07:16
    关注

    if the $mediname is string you should use quote around this var (string var)

     ("UPDATE medicine_invent 
           set med_stock = med_stock - $tits 
           where med_name = '$mediname'");
    

    Anyway you should not use php var directly in your sql code for avoid sqlinjection risk .. check for you framework/dbdriver the correct use of parametrized query and pass the value you need using binding param features

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程