-1

I have got MySQL table with three columns 'primary Key','debit_cash','user_id' So now i want to update the debit_cash values to the corresponding user_id by adding "15" to the value already present. The debit_cash is in VARCHAR so i tried converting to int and sum it , but still the values in MySQL is not changing .

Here is my code:

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
    //Getting values 
    $user_id = $_POST['user_id'];


    //importing database connection script 
    require_once('dbConnect.php');

    //Creating sql query 
    $sql = "SELECT cos_details.debit_cash AS debitCash,
            (convert(int, debit_cash)+15) AS updatedDebitCash
            FROM cos_details
            UPDATE cos_details SET debit_cash = '$updatedDebitCash'
            WHERE user_id = $user_id";

    //Updating database table 
    if(mysqli_query($con,$sql)){
        echo 'Updated Successfully';
    }else{
        echo 'Could Not Update Try Again';
    }

    //closing connection 
    mysqli_close($con);
}

Any one please help me.

William Willi
  • 194
  • 1
  • 2
  • 19
  • 1
    You're trying to execute a multi query here. Use `mysqli_error($con)` against your query and you'll see the syntax error. – Funk Forty Niner Nov 24 '16 at 12:48
  • Possible duplicate of [How to execute two mysql queries as one in PHP/MYSQL?](http://stackoverflow.com/questions/802437/how-to-execute-two-mysql-queries-as-one-in-php-mysql) – Funk Forty Niner Nov 24 '16 at 12:54
  • Just a small point: Cash normally has a decimal component?? Are you sure `INT` is the right thing to cast it to?? – RiggsFolly Nov 24 '16 at 12:57
  • @Fred-ii- Esspresso pour moi si vois plat – RiggsFolly Nov 24 '16 at 13:17
  • 1
    **A:** Make it easier on yourself and dump your entire table, alter the column to be a type that math can be done, rather than trying to convert a varchar to an integer and run only one query at a time. How much clearer can I be here? I even said to check for errors but haven't responded back. If you're going to continue on your present route, then edit your question with the db schema and example values. – Funk Forty Niner Nov 24 '16 at 13:39

1 Answers1

1

Seems that you don't need the select but the update only

  UPDATE cos_details SET debit_cash = cast( (convert(int, debit_cash)+15) as VARCHAR(20))
  WHERE user_id = $user_id

could be that your user_id is a string too so you should surround the value with quote

  UPDATE cos_details SET debit_cash = cast( (convert(int, debit_cash)+15) as VARCHAR(20))
  WHERE user_id = '$user_id'
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107