IF-ELSE using PHP and SQL -


i creating stock trading game php , sql. have 'buy' function adds stock id, user id , quantity table named 'ownedstocks'.

i having trouble in implementing simple 'if-else' statement. basically, want:

if user id , stock id of stock being purchased exists in 'ownedstocks' table, want update quantity. else if no rows exist given user id , stock id, want insert new row.

i have code unsure of using if-else.

thanks in advance!

<?php  include_once 'header.php'; $user = $_session['user']; $id = $_session['id'];   $price = $_session['price']; $amount=$_post['amount']; $total = $amount*$price; $balance = $_session['balance']; $con=mysqli_connect("localhost","root","usbw","stocktrading"); // check connection if (mysqli_connect_errno())   {   echo "failed connect mysql: " . mysqli_connect_error();   }              $newbalance = ($balance - $total);              querymysql("update ownedstocks                             set quantity = (quantity+$amount)                             ownedstocks.userid = '$user' , ownedstocks.stockid = '$id'");              querymysql("insert ownedstocks                         values ('$user', '$id', '$amount')");              querymysql("update members                         set balance = $newbalance                         members.user = '$user'");               echo("you purchased $id costing $price per stock<br/><br/>             bought $amount stocks<br/><br/>             calculate bill: $price x $amount equals $total<br/><br/>             have spent $total, new balance $newbalance <br/><br/>");              ?> 

what want mysql insert on duplicate key update function.

basically:

insert ownedstocks values (...) on duplicate key update amount = amount + '$amount' 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -