Home > front end >  How to target MySQL data row
How to target MySQL data row

Time:06-06

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

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";
    }
}
?>

CodePudding user response:

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);
    }

  • Related