Hello i have this code and something isn't working but i dont know what, the code it does not updating the database at all and i don't get any errors.

<?php
if(isset($_POST['howMuch']) && isset($_POST['where'])) {
$howMuch = $_POST['howMuch'];
$where = $_POST['where'];

if(!empty($howMuch) && !empty($where)) {
    $user_mBalance = getUserData('payCheck');
    if($howMuch > $user_mBalance) {
        echo "You cannot donate more than you have!";
    } else {
        $sql = "SELECT `ID` FROM `users` WHERE `First Name`='".$where."'";
        $result = $conn->query($sql);
        if($result === false) {
            trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);

        if($row = $result->num_rows > 0) {
            echo "Username doesn't exist.";
        } else {
            $sender = getUserData('First Name');
            if($where == $sender) {
                echo "You can't donate to your self!";
            } else {
                $total = $user_mBalance - $howMuch;
                $query = "UPDATE `users` SET `payCheck`='".$total."' WHERE `First Name`='".$sender."'";
                $res = $conn->query($query);
                printf("You have donated $ %d to %s", $howMuch, $where);
            }
        }
    $conn->close();
    }
}
} else {
    echo "You can't leave empty! ";
}
}
?>

Recommended Answers

All 24 Replies

      if($result === false) {
        trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);

        if($row = $result->num_rows > 0) {
          echo "Username doesn't exist.";
        } else {
          $sender = getUserData('First Name');
          if($where == $sender) {
            echo "You can't donate to your self!";
          } else {
            $total = $user_mBalance - $howMuch;
            $query = "UPDATE `users` SET `payCheck`='".$total."' WHERE `First Name`='".$sender."'";
            $res = $conn->query($query);
            printf("You have donated $ %d to %s", $howMuch, $where);
          }
        }
        $conn->close();
      }

That is all in one block, if result is false you do all that.

commented: Well spotted :-) +11

Maybe there is something wrong with your update query (maybe the $sender or $total are not what you expect). You can simply check it by sticking the following piece of code right after line 24:

die($query);

This will display your update query with the parameters and stop the script. Now you can copy the query into phpmyadmin and check if it returns any data.

Oh daamn :D but now its showing always username doesn't exist

It prints that if $result->num_rows > 0. So if results has more than 0 rows.

I have changed the code into this but still showing Username doesn't exist.

<?php
    if(isset($_POST['howMuch']) && isset($_POST['where'])) {
        $howMuch = $_POST['howMuch'];
        $where = $_POST['where'];

        if(empty($howMuch) && empty($where)){
            echo "You cant leave it empty!";
        } else {
            $user_mBalance = getUserData('payCheck');
            if($howMuch > $user_mBalance) {
                echo "You cannot donate more than you have!";
            } else {
                $sql = "SELECT `ID` FROM `users` WHERE `First Name`='".$where."'";
                $result = $conn->query($sql);
                if($result === false) {
                    trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
                } else {
                    if($result->num_rows > 0) {
                        echo "Username doesn't exist.";
                    } else if($result->num_rows > 1){
                        $sender = getUserData('First Name');
                        if($where == $sender) {
                            echo "You can't donate to your self!";
                        } else {
                            $total = $user_mBalance - $howMuch;
                            $query = "UPDATE `users` SET `payCheck`='".$total."' WHERE `First Name`='".$sender."'";
                            die($query);
                            $res = $conn->query($query);
                            printf("You have donated $ %d to %s", $howMuch, $where);
                            }
                        }
                    $conn->close();
                }
            }
        }
    }
?>
if($result->num_rows > 0) {
    echo "Username doesn't exist.";
} else if($result->num_rows > 1){

If it's larger than 1 it's also larger than 0.

If the amount of results is 0 the user name probably does not exist. If it is 1 it does, if it's larger than 1 you'd have the problem of multiple people with the same name.

Oh found the error in $result->num_rows > 0 it needs to be $result->num_rows == 0 :D ... thank you alot Traevel and broj1 really helped :)
Also i really need to ask how do i update the receiver data with the current balance he have, how do i get his payCheck field and substract with the receiving amouth and than just add the total amouth in the database

Do what you're doing in getUserData() except instead of the session name/id you use the receiver's. Then

$receiverTotal = $receiver_mBalance + $howMuch;

and

$query = "UPDATE `users` SET `payCheck`='".$receiverTotal."' WHERE `First Name`='".$receiver."'";

So it will be something like this ?

function getReceiverData($field) {
        $DBServer = 'localhost';
        $DBUser   = 'root';
        $DBPass   = '';
        $DBName   = 'upstrey';
        $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT `$field` FROM `users` WHERE `ID`='".$_SESSION['receiver_id']."'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            if($row = $result->fetch_assoc()) {
                return $row["$field"];
            }
        }
        $conn->close();
    }

Im using the same code for getUserData but just the session is $_SESSION['user_id']

If receiver_id is in the session, then that should work the same as it does for user yes.

But it gives me error Notice: Undefined index: receiver_id in C:\xampp\htdocs\Training\core.php on line 46 what should i do to place receiver_id in the session ?

How do i set the receiver_id session to get the receiver data from the fields in this case i want to get the payCheck field.

anyone ?

Sorry, have been quite busy lately.

To store the receiver_id into a session variable you only have to read it from the database and assign it to a session variable:

$_SESSION['receiver_id'] = $row['receiver_id'];

But I am not sure if this is useful for you to also store the receiver in the session. Depends on the logic of your app.

Thanks and where should i place this code in which file login.php, core.php, donate.php ?

See this code:

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

Try to change it into like this follows:

$result = $conn->mysql_query($sql);

Try to change it into like this follows:

$result = $conn->mysql_query($sql);

You're advising him to use mysql_query? Wow. Maybe read up a little on PHP before you sell another website to one of your vict.. eh.. customers.

The use of query() is perfectly fine, it's mysqli which unlike the deprecated mysql you're advising is a good choice.

@Stefan

where should i place this code in which file

Same spot where you're storing the user session variable. Where you determine to whom the user has to pay.

commented: I actually agree, mysql ext is outdated +11

Sorry for late reply but yesterday daniweb was down so i was unable to reply, anyway the user_id is placed in login.php and core.php so what i have done... I copyed the getUserData and paste it like getReceiverData with different session variable in this case receiver_id so now when im tryng to get the receivers data i get error Notice: Undefined index: receiver_id in C:\xampp\htdocs\Training\core.php on line 46 here is the code for getReceiverData

function getReceiverData($field) {
        $DBServer = 'localhost';
        $DBUser   = 'root';
        $DBPass   = '';
        $DBName   = 'upstrey';
        $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        //$_SESSION['receiver_id'] = 'receiver_id'; <- here need to be ?
        $sql = "SELECT `$field` FROM `users` WHERE `ID`='".$_SESSION['receiver_id']."'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                return $row["$field"];
            }
        }
        $conn->close();
    }

So this error gives me hint to store somewhere the session variable like you said in previous post but the problem is i do not know how and where to store, is there any option that you can change the code and store it the session because im really confuzed, Thank you alot for replying here and have a nice day or night, depends.... :D

P.S i almost forgoted, i have a really small error over here for Undefined variable: count take a look ever here and why is showing me the error btw its working just the error bothering me. Im sorry for asking theese questions in same topic but i think it will be stupid to post a new question about this error :D

<?php
    $user = getUserData('Username');
    $sql = "SELECT `Username`, `Uplata`, `Dobivka`, `Date` FROM `kladilnica` WHERE `Username`='$user'";
    $result = $conn->query($sql);
    $count = $count + 1; // here is the $count variable 
    if($result === false) {
        trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
    } else {
        if($result->num_rows == 0) {
            echo "Cannot add the ticket. Try again later.";
        } else if($result->num_rows >= 1){
            echo "<table id='t01'><tr><th>ID</th><th>Username</th><th>Uplata</th><th>Dobivka</th><th>Date</th></tr>";
            while($row = $result->fetch_assoc()) {
                //and here too
                echo "<tr><td>".$count++."</td> <td>".$row["Username"]."</td> <td>".$row["Uplata"]."</td> <td>".$row["Dobivka"]."</td> <td>".$row["Date"]."</td></tr>";
            }
            echo "<tr><th colspan='5'>Vkupna Uplata:</th></tr>";
            echo "<tr><th colspan='5'>Vkupna Dobivka:</th></tr>";
            echo "</table>";
        }
    }
    $conn->close();
?>

And how do i get all numbers from Uplata field and gather each other to get the total of them ?

@Traevel can you help or anyone else ?
also that with the $count is fixed dont look it
And also Uplata is done, so just reciver_id session variable

// $_SESSION['receiver_id'] = 'receiver_id'; <- here need to be ?

This should be between lines 16 and 17 (after querying the database, and uncommented). Or maybe outside this function altogether.

It doesn't work i tryed different ways of that but neither works.. so im asking everyone who can help me with this problem does i can contact with she/him on Skype ? Facebook ? so i can get a better response from the helper. Thank you very much.
My skype is: stefanrafaa
and facebook: Facebook

This is the first error you need to fix Notice: Undefined index: receiver_id in C:\xampp\htdocs\Training\core.php on line 46

You're getting this error because you're not setting $_SESSION['receiver_id'] anywhere, just like broj1 pointed out to you. If this isn't being set properly, then there is no point in worrying about any of the other errors down the script until this error has been resolved, since the rest of your code relies on the session containing receiver_id.

Ok now i dont get any error but the receiver payCheck field is updating just with the number they send to him.

I am a bit lost now. Can you post the last version of the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.