To begin, I adore PHP. Wow.

The "problems" with PHP is not PHP, it's me, honestly.

I am having issues coding at times and a tad of frustration. But, I am receiving great results with reseach, help and hard work.

My current issue is rather simple I believe but I am yet to solve it:

Data is input into a page form then submitted > Data is loaded into a MySql DB. (This works perfecly.) :)

The issue now is how to call that specific data and display it, formatted on a new page. I'm stuck at this point.

  • How do I return the exact User data from the DB and render it on a new page? Specific data: Via sessions, cookies, etc? I'm not sure. I can return and display randomized fields from the table and display them on the same page as the form upon refresh, but it is not the specific data just entered. This is confusing.
  • I am reading about jQuery/Ajax to do this - Is that even necessary? I hesitate to implement too many scripting languages at once.
  • I'm stuck. I simply wish to enter data, submit and have that data displayed for the User, the previous form not present.
  • I need to redirect to a new page I presume. This is not working correctly. What is a "PHP header"?

Any assistance would be greatly appreciated. I am just trying to build a perfect product and I'm still learning.

Thank you in advance,
Matthew

Recommended Answers

All 4 Replies

Start from the database, for example MySQL: when you enter something, if the table has a primary key and this is numeric and increments automatically, i.e.:

id int(10) primary key autoincrement not null

then you can use last_insert_id() to get the last data entered in the table. The above is a MySQL function, but you can get the same result with the libraries provided by PHP, or you can simply query after you do an insert:

select last_insert_id();

Now, if you want to display data related to a specific user, for a specific user then create a authorization procedure to create a session, for example a login form. When the script validates the request, start a session in which you save the ID of the user:

$_SESSION['userID'] = $row['id'];

And redirect the user to the restricted area, here you can use the header():

header('Location: /profile');

So if we want to create a simple form:

<form method="post" action="/login.php">
    username <input type="text" name="username" /><br />
    password <input type="password" name="password" /> <br />
    <input type="submit" name="submit" value="login" />
</form>

And the login.php page:

<?php

    session_start();

    if($_POST && array_key_exists('username', $_POST) && array_key_exists('password', $_POST))
    {
        $sql = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
        $sql->prepare("select id from users where username = ? AND password = ? limit 1");
        $sql->execute(array($_POST['username'], sha1($_POST['password'])));

        if($sql->rowCount() > 0)
        {
            $result = $sql->fetch(PDO::FETCH_ASSOC);
            $_SESSION['userID'] = $result['id'];

            header("Location: /profile.php");
        }
        else
        {
            # back to the login form
            header("Location: $_SERVER[HTTP_REFERER]");
        }
    }
    else
    {
        echo 'Not allowed';
    }

The landing page, i.e. profile.php, can contain something like this:

<?php

    session_start();

    if( ! array_key_exists('userID', $_SESSION))
    {
        # session not valid, log out the request
        header('Location: /logout.php');
    }

    $images = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
    $images->prepare("select * from images where user_id = ?");
    $images->execute(array($_SESSION['userID']));

    if($images->rowCount())
    {
        while($image = $images->fetchAll())
        {
            echo '<img src="/images/' .$image['filename']. '" />';
        }
    }

This kind of setting requires the use of a foreign key, i.e. a column that referes to another table:

create table users(
    id int unsigned not null primary key autoincrement,
    username varchar(100) not null unique,
    password char(40) not null
) engine = innodb default charset = utf8;

insert into users (username, password) values('bob', sha1('password'));

create table images(
    id int unsigned not null primary key autoincrement,
    user_id int unsigned not null,
    filename varchar(100) not null unique,
    foreign key(user_id) references users(id) on delete cascade on update cascade
) engine = innodb default charset = utf8;

insert into images (user_id, filename) values(1, 'image001.jpg'), (1, 'image002.jpg'), (1, 'image003.jpg');

In this example the foreign key is user_id and referes to the id column of the users table. The constraint in the second table works like this: when you delete a user, all his images will be deleted automatically, if you update the id of the user, it will be updated automatically into images.

For more information check the documentation:

The above code is not tested, and it is an example, it needs to be improved if you want to apply it in production.

You can use AJAX/JQuery if you want, but it is not required.

If you have doubts or want more help provide your code, we will try to help.

commented: Great! TY for your detailed help. +0

cereal:

Thank you so much for taking the time to reply and share such wonderful code examples. I really appreciate the assistance.

I will be trying your examples soon and shall report back here with the results and any possible questions.

Have a great day/evening!
Matthew

Cereal:

It is I, The_Thorn.

This is my original account, mattyd, dating back years and has been recovered for me by the gracious <daniweb> team.

I am looking at your code samples and planning to implement. I shall let you know how it all works out.

-Matthew

That's ok :)

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.