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
Post a Comment