James Wood
James Wood

Reputation: 13

PHP Query subtraction not working

Hello I am trying to subtract a users credit's after a transaction but something keeps going wrong when updating.while testing accepted_bidder = 15 and credit row in customer is 100. for some reason when I update it is -15 rather than 75 anyone know why this may be?

output: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given in C:\xampp\htdocs\payment2.php on line 20

Notice: Undefined variable: credit in C:\xampp\htdocs\payment2.php on line 26

<?php
session_start();
require 'config.php';


$id = $_SESSION['login_user'];
$jobid    = $_POST['job_id'];
$poster_id    = $_POST['poster_id'];
$accepted_bidder    = $_POST['accepted_bidder'];
$accepted_bid    = (int) $_POST['accepted_bid'];
$poster_id = $_POST['poster_id'];

$query = "SELECT credit FROM `customer` WHERE email_adress = '$id'";

$success = $conn->query($query);


$result = mysqli_fetch_array($success);

while($row = mysqli_fetch_array($result)):
$credit = (int)$row['credit'];
endwhile;

//var_dump($result);
//var_dump($accepted_bid); 
$updated_credit = $credit - $accepted_bid;
//echo $updated_credit;


$query2   = "UPDATE job SET start_escrow = '1' WHERE job_id = '$jobid'";
$success2 = $conn->query($query2);

$query3   = " UPDATE customer SET credit = '$updated_credit' WHERE email_adress = '$id'";
$success3 = $conn->query($query3);



if (!$success) {
    die("Couldn't enter data: ".$conn->error);

}

echo "Thank You For Contacting Us <br>";
 //header("location: myjobs.php");



$conn->close();




?>

Upvotes: 0

Views: 143

Answers (1)

Barmar
Barmar

Reputation: 781503

$result is not the result of the query, it's the first row you read using:

$result = mysqli_fetch_array($success);

So when you then do:

while ($row = mysqli_fetch_array($result))

you're trying to use that row as the query result.

Replace these two lines:

$success = $conn->query($query);
$result = mysqli_fetch_array($success);

with

$result = $conn->query($query);

Upvotes: 3

Related Questions