1

I have a MYSQL table named issues_tot including following columns:

v_code, oid, amount, mod_date

02) Then I need to update or insert records of the table according to the given condition as follows:

  if(($vt == $vote)||($of == $ono)){

03) update is working properly, but insert is not (else part). My code is showing below:

if  (isset($_POST["submit"]))

{
      $ono =$_POST["oid"];
      $amt =$_POST["amt"];      
      $allo=mysql_fetch_array(mysql_query("SELECT * FROM allocation WHERE al_code='{$_GET['al_code']}'"));
      $vote=$allo['v_code'];
      $current_date = date("Y-m-d H:i:s");

      $query ="select * from issues_tot where v_code='$vote' "; 
      $result = mysql_query($query) or die ( mysql_error());
      $row = mysql_fetch_assoc($result);
      $vt = $row['v_code'] ;
      $of = $row['oid'] ;  

      if(($vt == $vote)||($of == $ono)){
        $query ="UPDATE  issues_tot SET  oid = $ono, amount = amount + $amt  WHERE v_code=$vote"; 
        $result = mysql_query($query) or die ( mysql_error());
        $rc = mysql_affected_rows();
     }else {
        $query ="INSERT INTO issues_tot (v_code, oid, amount, mod_date) VALUES ('$vote', '$ono', '$amt', '$current_date')";
        $result = mysql_query($query) or die ( mysql_error());
        $rc = mysql_affected_rows();
    }
}

I can not understand what I am going wrong. Can anyone help me ?. Pls

Josip Ivic
  • 3,639
  • 9
  • 39
  • 57
Xtern
  • 165
  • 2
  • 2
  • 13
  • 1
    It seems like you are forgetting to actually query after setting `$query`. `mysql_query($query)` invocation is needed there as well. – Selfish Sep 20 '15 at 12:05
  • Still remaining the problem – Xtern Sep 20 '15 at 12:15
  • if the following condition is true Update relevant record. If it is false does not insert a new record .....if(($vt == $vote)||($of == $ono)) – Xtern Sep 20 '15 at 12:19
  • 1
    If there's no change whatsoever in the database, `mysql_error()` must return an error description, explaining why nothing was done. Please get that error code/text. – Selfish Sep 20 '15 at 12:42
  • First problem: your code is vulnerable to SQL injection attacks. See: [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/q/60174/3908097). Second problem: it looks like `$vno` in your INSERT statement is undefined. – Rimas Sep 20 '15 at 12:52
  • @Rimas it is not like $vno. it should be $vote – Xtern Sep 20 '15 at 16:18
  • Your INSERT is inserting 4 string values into the database. What is the data type of each column? In particular, is "amount" a string type or a numeric type? Try removing single quotes from '$amt'. – BareNakedCoder Sep 21 '15 at 04:03

0 Answers0