Jump to content

Adding a link to displayed data from a db on a page


Recommended Posts

I want to add a link in this php code, what must be read up on to get something like this working?

It would be a profile page, so I'm just sort of wanting to gather some info on this.

Either I'd add another row for the table to display or using an existing one like first name which would be a hyper link.

if ( mysqli_num_rows( $r ) > 0 )
{
  echo '<table><tr><th>Posted By</th><th>Subject</th><th id="msg">Message</th></tr>';
  while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC ))
  {
    echo '<tr><td>' . $row['first_name'] .' '. $row['last_name'] . '<br>'. $row['post_date'].'</td>
    <td>' . $row['subject'] . '</td><td>' . $row['message'] . '</td> </tr>';
  }
  echo '</table>' ;
}

<?php # DISPLAY COMPLETE FORUM PAGE.

# Access session.
session_start() ;

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }

# Set page title and display header section.
$page_title = 'Forum' ;
include ( 'includes/header.html' ) ;

# Open database connection.
require ( 'connect_db.php' ) ;

# Display body section, retrieving from 'forum' database table.
$q = "SELECT * FROM forum" ;
$r = mysqli_query( $dbc, $q ) ;
if ( mysqli_num_rows( $r ) > 0 )
{
  echo '<table><tr><th>Posted By</th><th>Subject</th><th id="msg">Message</th></tr>';
  while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC ))
  {
    echo '<tr><td>' . $row['first_name'] .' '. $row['last_name'] . '<br>'. $row['post_date'].'</td>
    <td>' . $row['subject'] . '</td><td>' . $row['message'] . '</td> </tr>';
  }
  echo '</table>' ;
}
else { echo '<p>There are currently no messages.</p>' ; }

# Create navigation links.
echo '<p><a href="post.php">Post Message</a> | <a href="home.php">Home</a> | <a href="goodbye.php">Logout</a></p>' ;

# Close database connection.
mysqli_close( $dbc ) ;
  
# Display footer section.
include ( 'includes/footer.html' ) ;

?>

Thanks.

Link to post
Share on other sites

Well is it a hyperlink? It could be since a new page would load to display the name of the user.

Like some other example sites, a link would be on the name of the posted user like on a forum post. Like here, a good example.

Link to post
Share on other sites

So basically as we have discussed many many times a link to a page that will have id ref, where you would use that id ref to search a database for a users id that matches, and once that match is found, show required details stored in the row. THAT  hyperlink.

Link to post
Share on other sites
<?php # PROCESS MESSAGE POST.

# Access session.
session_start();

# Make load function available.
require ( 'login_tools.php' ) ;

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { load() ; }

# Set page title and display header section.
$page_title = 'Post Error' ;
include ( 'includes/header.html' ) ;

# Check form submitted.
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  # Check Subject and Message Input.
  if ( empty($_POST['subject'] ) ) { echo '<p>Please enter a subject for this post.</p>'; }
  if ( empty($_POST['message'] ) ) { echo '<p>Please enter a message for this post.</p>'; }

  # On success add post to forum database.
  if( !empty($_POST['subject']) &&  !empty($_POST['message']) )
  {
    # Open database connection.
    require ( 'connect_db.php' ) ;
  
    # Execute inserting into 'forum' database table.
    $q = "INSERT INTO forum(first_name,last_name,subject,message,post_date) 
          VALUES ('{$_SESSION[first_name]}','{$_SESSION[last_name]}','{$_POST[subject]}','{$_POST[message]}',NOW() )";
    $r = mysqli_query ( $dbc, $q ) ;

    # Report error on failure.
    if (mysqli_affected_rows($dbc) != 1) { echo '<p>Error</p>'.mysqli_error($dbc); } else { load('forum.php'); }
    
    # Close database connection.
    mysqli_close( $dbc ) ; 
    }
 } 
 
# Create a hyperlink back to the forum page.
echo '<p><a href="forum.php">Forum</a>' ;
 
# Display footer section.
include ( 'includes/footer.html' ) ;

?>

I took a look at post_action.php.

 

This could be the base for linking one uploaded image with a logged in name? Not sure if that would be beneficial, unless it was intended for removing it altogether with a user, which I don't have that for the comments. It was a thought.

# Execute inserting into 'forum' database table.
    $q = "INSERT INTO forum(first_name,last_name,subject,message,post_date) 
          VALUES ('{$_SESSION[first_name]}','{$_SESSION[last_name]}','{$_POST[subject]}','{$_POST[message]}',NOW() )";
    $r = mysqli_query ( $dbc, $q ) ;

    
Edited by Html
Added particular code for thought.
Link to post
Share on other sites

I'm not going to bother going through the code, been there, done that, the user id MUST be used wherever data related to user exists, you know a RELATIONAL database.

User table: user_id ref, username, email, ((one) unique user_id)

User address table:  user_id, address, postcode, county, city/town, county. ((one) unique user_id)

User posts table: user_id, posted messages,  dateposted..(many duplicate user_id)

All these are brought together using the users relational data key user_id

Edited by dsonesuk
Link to post
Share on other sites

Unfortunately, I last tried this whole task with the image upload, and yeah I read what you stated in those posts, I didn't understand the fundamentals, I was just tinkering with it.

As for this profile link example.

You mean the link and image type must be in the table? The post_action.php which I didn't take much look into, clearly shows something different.

CREATE DATABASE IF NOT EXISTS site_db;

USE site_db;

CREATE TABLE IF NOT EXISTS users (
user_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
pass VARCHAR(256) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (user_id),
UNIQUE (email)
);

 

 

CREATE DATABASE IF NOT EXISTS site_db;

USE site_db;

CREATE TABLE IF NOT EXISTS forum (
post_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
subject VARCHAR(60) NOT NULL,
message TEXT NOT NULL,
post_date DATETIME NOT NULL,
PRIMARY KEY (post_id)
);

 

Link to post
Share on other sites

If you are duplicating fields (first, last name) and data in separate tables you are doing it wrong!

If the image is a singular image, specific to user as in profile image it would be best stored in user table or separate table with user_id to specific user and single image. If it is none specific as in forum posts with different possible images it would be stored  in forum posts table where it will be a many posts to single user user_id of which could be many duplicate user_id for each posts they made, which is referred to as ONE to MANY.

Edited by dsonesuk
Link to post
Share on other sites

Well for the moment, this is what I have tried.

For the image upload code, I decided just to insert this.

<?php

# Access session.
session_start() ; 

# Redirect if not logged in.
if ( !isset( $_SESSION[ 'user_id' ] ) ) { require ( 'login_tools.php' ) ; load() ; }

# Create navigation links.
echo '<p><a href="home.php">Home</a> | <a href="upload.php">Upload image</a> | <a href="gallery.php">Gallery</a> | ';

# Execute inserting into 'forum' database table.
    $q = "INSERT INTO shop(first_name,last_name,item_img) 
          VALUES ('{$_SESSION[first_name]}','{$_SESSION[last_name]}','{$_POST[item_img]}',NOW() )";
    $r = mysqli_query ( $dbc, $q ) ;

$filelist = glob("./images/*");

// Initialize message variable
  $msg = "";

  // If upload button is clicked ...
  if (isset($_POST['upload'])) {
  	// Get image name
  	$image = $_FILES['image']['name'];
  	// Get text
  	$image_text = mysqli_real_escape_string($db, $_POST['image_text']);

  	// image file directory
  	$target = "images/".basename($image);

  	if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
  		$msg = "Image uploaded successfully";
  	}else{
  		$msg = "Failed to upload image";
  	}
      {
    move_uploaded_file( $image , $tmp_name ) ;
    echo 'File uploaded : '.$image ;
    echo '<br><img src="'.$image.'">' ;
  }
  
  }
  $result = mysqli_query($dbc, "SELECT * FROM images");
?>

The image code work as is, but not with the code, but still uploads an image.

As for the table, so an image table, is what you suggest, which I had originally tried, but I got rid of that, bare in mind that was months ago.

The example, I saw as in above,

CREATE DATABASE IF NOT EXISTS site_db;

USE site_db;

CREATE TABLE IF NOT EXISTS shop (
item_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
item_name VARCHAR(20) NOT NULL,
item_desc VARCHAR(200) NOT NULL,
item_img VARCHAR(20) NOT NULL,
item_price DECIMAL(4,2) NOT NULL,
PRIMARY KEY (item_id)
);

/*
INSERT INTO shop ( item_name, item_desc, item_img, item_price )
VALUES ( "Cow", "A friendly field buddy.", "images/cow.png",19.99 );
INSERT INTO shop ( item_name, item_desc, item_img, item_price )
VALUES ( "Dog", "A friendly lap buddy.", "images/dog.png",14.99 );
INSERT INTO shop ( item_name, item_desc, item_img, item_price )
VALUES ( "Goat", "A friendly mountain buddy.", "images/goat.png",16.99 );
INSERT INTO shop ( item_name, item_desc, item_img, item_price )
VALUES ( "Leopard", "A friendly spotted buddy.", "images/leopard.png",17.99 );
INSERT INTO shop ( item_name, item_desc, item_img, item_price )
VALUES ( "Rhino", "A friendly jungle buddy.", "images/rhino.png",29.99 );
*/

INSERT INTO shop
( item_name, item_desc, item_img, item_price )
VALUES
( "Cow", "A friendly field buddy.", "images/cow.png",19.99 ),
( "Dog", "A friendly lap buddy.", "images/dog.png",14.99 ),
( "Goat", "A friendly mountain buddy.", "images/goat.png",16.99 ),
( "Leopard", "A friendly spotted buddy.", "images/leopard.png",17.99 ),
( "Rhino", "A friendly jungle buddy.", "images/rhino.png",29.99 );

It got me thinking, while seeing these images stored in a table as they a part of a shop, try and recreate some of this, if you get my view.

Edited by Html
Link to post
Share on other sites

So item_img I added to forum table as another column, obviously it didn't store the uploaded image.

So in the shop table, this is what looks like, so if I could use this to store an image that has been uploaded, and connect it with the other two tables or something, I hope you get my point.

  item_id item_name item_img
  Edit Copy Delete 1 Cow images/cow.png
    Copy   2 Dog images/dog.png
        3 Goat images/goat.png
      Delete 4 Leopard images/leopard.png
        5 Rhino images/rhino.png

I modified this, I removed two columns.

Edited by Html
Added table example
Link to post
Share on other sites

No! Again! You are duplicating, you use the id ref as a key not image name. Again!

3 hours ago, dsonesuk said:

If you are duplicating fields (first, last name) and data in separate tables you are doing it wrong!

You would list the column names, and tables used, you then would use the key id in all these tables to bring this info together. User id ref in user table 007 has image in image table with its own matching userid of 007 that matches, to bring up the image of his aston martin. Using  WHERE usertable.userid = 007 AND imagetab.userid = 007, this bring up the data in both tables related to user with user_id of 007

Link to post
Share on other sites

Well, the shop table can be used to store an image, that is my point, otherwise how am I going to link it just like the user is linked with the comments when posting to the forum.

I used the above example, as I thought it is the same as the other tables, you get my point here?

There is item_id, in shop to store an image, that is displayed in a shop. My point is to use that to store the user's uploaded images, it already has images/ since that the folder I have where they are stored.

What may be obvious to you, isn't quite so to me, I'm returning to this, and just had a thought based on looking at post_action.php and seeing what is what, and what I need to get this similar process that already works for the other examples, to for the upload code.

Edited by Html
Link to post
Share on other sites

Profile, forum, upload to shop is this another similar code you found on internet you thought you would include,  how do expect anyone to help when you move the goal post everytime someone sneezes "paste and copy"

Edited by dsonesuk
Link to post
Share on other sites

The shop table, and file is actually a part of the user login site from the Php & Mysql book, but by viewing post_action.php, and seeing the stored images in the table for the database, I sort of thought, well this may be a way of getting the upload feature linked with a registered user.

I am not sure how I am not going in the correct direction, it just needs a little tinkering, to link it together.

Shop table will store Admin's uploaded image, as the code above demonstrates, or would if the code would work with the database insertion code.

# Execute inserting into 'forum' database table.
    $q = "INSERT INTO shop(first_name,last_name,item_img) 
          VALUES ('{$_SESSION[first_name]}','{$_SESSION[last_name]}','{$_POST[item_img]}',NOW() )";
    $r = mysqli_query ( $dbc, $q ) ;

item_img image uploaded, and sent to that variable or property.

                                                       item_img
      Copy      

images/cow.png

This example obviously, stores an image for the shop, what is on sale, add to a basket and so on.

Link to post
Share on other sites

The first_name and last_name are necessary for the shop table? I'd of thought so, as both users and forum have them. Otherwise it is item_id, and the other was Item_name, as well item_img, I will need at least one of these, obviously an uploaded image won't store in these, but then again the code while reports no errors probably isn't structured correctly, when adding the sql code to the image upload code.

sing  WHERE usertable.userid = 007 AND imagetab.userid = 007

Edited by Html
Link to post
Share on other sites

Are forum, profile and shop separate database tables? are the users from others separate from shop users, how would i know that! When you go off subject to list profile, forum, shop.

We started of with forum and now apparently ending with shop, it looked like you had done the norm in finding code that almost similar to what you are looking for,  copy and paste without understanding how any of it works in the first place.

Link to post
Share on other sites

I looked back at the topic from last year, on how to upload an image, and send the data to the db.

I then thought I had perhaps got somewhere with that problem, as I only got as far as the example the user on here had posted, and had trouble with it.

So the shop table already stores an image, as multiple items to buy in the shop network login site, all this code is connected. I just removed the shop as it wasn't needed.

users, and forum are separate tables, yes this is how this system works, I did state users table and forum table. I haven't expressed they were all in one, that would be messy. I guess. So image will be stored in shop, now two columns, so if forum and users are interconnected, based on that post_action.php, how do I get this image upload page to do exactly the same process, linked with it in the code, but obviously would not display the linked image in the db on the user forum or profile page or control panel area of the user site.

Ineasysteps produce the code the way it is.

Link to post
Share on other sites

This is in the wrong location in the image code.

shop table has first_name, last_name, item_id and Item_img.

varchar 20 for all.

I removed the stored image cow.png, so there is nothing in the table.

Session is for the user which has uploaded the image, and item_id is linked with it, just like having post_id for the comments. It can't be the properties, that are wrong, the code structure has got to be.

# Execute inserting into 'shop' database table.
    $q = "INSERT INTO shop(first_name,last_name,item_id,item_img) 
          VALUES ('{$_SESSION[first_name]}','{$_SESSION[last_name]}','{$_POST[item_id]}','{$_POST[item_img]}',NOW() )";
    $r = mysqli_query ( $dbc, $q ) ;
Edited by Html
Added removed table property info
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...