Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to target MySQL data row

I am struggling with PHP and React. I am busy obtaining data from my back-end and displaying it on my front end, I have the email from the back-end to display it. But, what I want is the first name of the logged-in user and display it on my front end. Any help would be appreciated.

Thanks!

Below is my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

JS

    const [receptionist, setReceptionist] = useState({
        email: sessionStorage.getItem('activeUser'),
        name: ''
    })


    return (
        <div className='page'>
            <div className="leftPage">
                <Nav/>
            </div>
  
            <div className="middlePage">
                <h1>Welcome, <span>{name}</span></h1>
                <Date />
                            
                <div className='welcome'>
                    <p>Welcome to your management portal !
                        Manage all doctor’s appointments right here and look at upcoming appointments.
                    </p>
                    <img src={Dash} width={250}/>
                </div>

                <CalendarCom/>
                {/* <div className="footerImg">
                    <img src={Logo}/>
                </div> */}
               
            </div>
            <div className="rightPage">
                <AppointmentsCom/>
            </div>
    
           
        </div>
    );

PHP

<?php 

include 'db_connection.php';

header('Access-Control-Origin: *');
header('Access-Control-Headers: *');

$request_body = file_get_contents('php://input');
$data = json_decode($request_body);

$name = $data->activeUser;

if($name === ""){
    echo "";
} else {
    $sql = "SELECT * FROM receptionists WHERE name = '$name';";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);

    if($resultCheck > 0){

        // $emparray = array();

        // while($row = mysqli_fetch_assoc($result)){
        //     $emparray[] = $row;
        // }

        // echo json_encode($emparray);


    } else {
        echo "false";
    }
}
?>

>Solution :

What you need to do is make use of a useEffect and then create an associative array in your PHP that will then get the sessionStorage email, compare it to your MySQL database and find all the data that is associated with that.

Javascript

const [receptionist, setReceptionist] = useState({
        email: sessionStorage.getItem('activeUser'),
        name: ''
    })
useEffect(() => {
        axios.post('http://localhost:8888/dbName/phpname.php', JSON.stringify(receptionist))
            .then((res) => {
                console.log(res.data[0]);
                setReceptionist({
                   email: res.data[0].email,
                   name: res.data[0].name
                });
            })
    }, []);
    return (
        <div className='page'>
            <div className="leftPage">
                <Nav/>
            </div>
  
            <div className="middlePage">
                <h1>Welcome, <span>{name}</span></h1>
                <Date />
                            
                <div className='welcome'>
                    <p>Welcome to your management portal !
                        Manage all doctor’s appointments right here and look at upcoming appointments.
                    </p>
                    <img src={Dash} width={250}/>
                </div>

                <CalendarCom/>
                {/* <div className="footerImg">
                    <img src={Logo}/>
                </div> */}
               
            </div>
            <div className="rightPage">
                <AppointmentsCom/>
            </div>
    
           
        </div>
    );

PHP

include 'db_connection.php';

    header('Access-Control-Origin: *');
    header('Access-Control-Headers: *');

    $request_body = file_get_contents('php://input');
    $data = json_decode($request_body, true); // true makes associative array

    $email = $data['email'];

    if($email)
    {
        $sql = "SELECT * FROM receptionists WHERE email = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("s", $email);
        $stmt->exexute();
        $result = $stmt->get_result();
        $emparray = $result->fetch_all(MYSQLI_ASSOC);
        echo json_encode($emparray);
    }

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading