0

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.

Dario
  • 618
  • 1
  • 11
  • 28

1 Answers1

1

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

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Thanks a lot sir. i got an idea now. thank you very much. – Jason Ronda Nov 17 '17 at 07:29
  • @JasonRonda then you should give him 1+ ;) – Ravi Sharma Nov 17 '17 at 07:30
  • Hello sir, @scaisEdge then why you do not quoted this => `med_stock - $tits` – Ritesh Khatri Nov 17 '17 at 08:33
  • @Rits .. the number don't need quotes ... the string need quotes . .. this is the fact .. – ScaisEdge Nov 17 '17 at 08:41
  • but when i pass quotes to number it inserted, but why we not use quotes here? – Ritesh Khatri Nov 17 '17 at 08:49
  • 1
    @Rits the number is inserted because the sql engine perform a direct cast do the fact there is an aritmetic operation involved but fur a correct use (and a correct understanding of the difference between numeric and string variables) the number var not no must be written in quotes .. .. Anyway the comments of another answer are not the proper place for answer a new question .. If you need more post a new properly documented question – ScaisEdge Nov 17 '17 at 08:54
  • no brother i just wanted this, and many thanks -- m giving you upvote its same as give you on new question. – Ritesh Khatri Nov 17 '17 at 10:09