Hello i need little help overhere im confuzed getting user data from mysql database i want to update the payCheck column in the database but that column is located in the receiver or where (in the code) field i have this code please someone help me...

<?php
    require 'connect.php';
    require 'header.php';

    if(isset($_POST['howMuch']) && isset($_POST['where'])) {
        $howMuch = $_POST['howMuch'];
        $where = $_POST['where']; // this is the receiver

        if(!empty($howMuch) || !empty($where)) {
            $user_mBalance = getUserData('payCheck'); // getting already logged user field 'payCheck'
            if($howMuch > $user_mBalance) {
                echo "<br><br><br><br><br>You cannot donate more than you have!";
            } else {
                $query = "SELECT `ID` FROM `users` WHERE `First Name`='".mysql_real_escape_string($where)."'";

                if($query_run = mysql_query($query)) {
                    $query_num_rows = mysql_num_rows($query_run);

                    if($query_num_rows == 0) {
                        echo "<br><br><br>Username doesn't exist.";
                    } else if($query_num_rows == 1) {
                        $sender = getUserData('First Name'); // getting already logged user field 'First Name'
                        $total = $user_mBalance - $howMuch; // calculations of loged user 'payCheck' + 'howMuch'

                        $query = "UPDATE `users` SET `payCheck`='$total' WHERE `First Name`='$sender'"; // updating the sender `payCheck` field
                        $query_run = mysql_query($query);
                        echo "<br><br><br>You have donated $howMuch to $where";
                    }
                }
            }
        } else {
            echo "You can't leave empty!";
        }
    }
?>

So simply i just want to get the receiver payCheck balance and than to add the money which sender sended to him something like $total = $receiver_moneyBalance + $howMuch;

And also here is the code for getUserData();

function loggedin() {
        if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) {
            return true;
        } else {
            return false;
        }
    }

    function getUserData($field) {
        $query = "SELECT `$field` FROM `users` WHERE `ID`='".$_SESSION['user_id']."'";
        if($query_run = mysql_query($query)) {
            if($query_result = mysql_result($query_run, 0, $field)) {
                return $query_result;
            }
        }
    }

Recommended Answers

All 13 Replies

Please anyone ?

Get the total for the receiver with a query (similar to line 14), then add the howMuch to total and do the update query (similar to line 25) again but for receiver.

Using mysql is bad though, you should switch to mysqli or PDO.

Can you please do that in code because i tryed too much ways of doing that and its just adding the money into the field and not summing the values

Im doing that like this but doesnt work like i said in the previous comment

$getUser_payCheck = "SELECT `payCheck` FROM `users` WHERE `First Name`='$where'";
                            $total = $user_mBalance - $howMuch;
                            $total2 = $howMuch + $getUser_payCheck;
                            $query = "UPDATE `users` SET `payCheck`='$total' WHERE `First Name`='$sender'";
                            $query2 = "UPDATE `users` SET `payCheck`='$total2' WHERE `First Name`='$where'";
                            $query_run = mysql_query($query);
                            $query_run2 = mysql_query($query2);
                            echo "<br><br><center>You have donated $$howMuch to $where</center>";

Where do you fetch $total2? It needs to be the amount already known in the database, so you need to fetch it with a query similar to $getUser_payCheck. But that's pseudocode I hope.

$getUser_payCheck = "SELECT `payCheck` FROM `users` WHERE `First Name`='$where'";

won't execute anything.

Here is the script feel free to change something because im changin all day and cant figure out PLEASE

<?php
    require 'connect.php';
    require 'header.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 "<br><br><br>You cannot donate more than you have!";
            } else {
                $query = "SELECT `ID` FROM `users` WHERE `First Name`='".mysql_real_escape_string($where)."'";
                $getReceiver_payCheck = "SELECT `payCheck` FROM `users` WHERE `First Name`='$where'";

                if($query_run = mysql_query($query)) {
                    $query_num_rows = mysql_num_rows($query_run);

                    if($query_num_rows == 0) {
                        echo "<br><br><br>Username doesn't exist.";
                    } else if($query_num_rows == 1) {
                        $sender = getUserData('First Name');
                        if($where == $sender) {
                            echo "<br><br><br>You can't donate to your self!";
                        } else {
                            $total = $user_mBalance - $howMuch;
                            $total2 = $getReceiver_payCheck + $howMuch;
                            $query = "UPDATE `users` SET `payCheck`='$total' WHERE `First Name`='$sender'";
                            $query2 = "UPDATE `users` SET `payCheck`='$total2' WHERE `First Name`='$where'";
                            $query_run = mysql_query($query);
                            $query_run2 = mysql_query($query2);
                            echo "<br><br><center>You have donated $$howMuch to $where</center>";
                        }
                    }
                }
            }
        } else {
            echo "<br><br><br><center>You can't leave empty!</center>";
        }
    }
?>

A quick note on line 9 - if(!empty($howMuch) || !empty($where)) {, this will always be true unless both variables are not defined (or empty). Are you sure you want that? Shouldn't it be && instead of ||?

No i im sure and please if you can help me what im asking please answer the question because im tired already 7 hours of sitting on the PC for nothing ...
And those who see this question just do it in the code dont post some tips and tricks of how to do it !

$user_mBalance = getUserData('payCheck');

That is the balance of the current user, i.e. $total. Just do the exact same for the receiving user. Same query but with a different ID. That would be $total2. Then update it with howMuch and re-insert.

If I changed your code I'd have to rewrite every part because of the mysql. That's a bit much. You can follow this tutorial if you want to change it to a correct format.

And those who see this question just do it in the code dont post some tips and tricks of how to do it !

This is rather rude to me. And my answer to you is, no I am not fixing someone code by rewriting it. No, I do not help those who claim to be a programmer/developer but at the same time show no afford, yet all they do are asking for the code and then copy and paste. 7 hours in front of computer coding/scripting is NOTHING to me. I worked 12~16 hours on my computer before. It is what programmer/developer do as their career in order to get to where they can actually finish the work much faster than many other people estimate.

PS: This forum is not for getting free code. It is for asking to understand your issue and fix it yourself.

commented: Agreed 100%, he's fortunate I missed his comment before posting what I did. I'm done here. +6

Do you mean on this ? this actualy worked but not correct i think i need to define the First Name of the query but i dont have any variables in this class core.php

function getReceiverData($field) {
        $query = "SELECT `$field` FROM `users`";
        if($query_run = mysql_query($query)) {
            if($query_result = mysql_result($query_run, 0, $field)) {
                return $query_result;
            }
        }
    }

No, read it again. The other one of the two. You're on your own now though as far as I'm concerned. I'm not here to write your code for you, and I doubt others will. This is our free time. All you did was repost your original code.

Instead of wasting 7 hours on this, like you said, spend those 7 hours following tutorials and learn proper PHP CRUD. Find another one if you don't want to do the one I posted. But don't just order others to change your code for you.

If you're tired, go sleep. And again, better to waste 5 hours doing proper PHP/MySQL and start fresh than 5 hours trying to fix the wrong way of doing PHP/MySQL.

hahahaha :D daaamn guys you are too stubborn but anyway thanks for the help and the spended time on this question :D BTW i made a misstake its about 10 hours of "working" on this error :D Thank you again :)
PS: Taywin dude don't get mad that fast i was just too much pissed for what i did today :D . But anyway my apology for that

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.