Jump to content

Linking to field in database - hyperlink


zelig

Recommended Posts

Here's the scenario:

 

I have an output (list of fields from a database in a table format). It is a list of items in my game, but I want to make the name be hyperlinked to the information from the database (name, attributes, price, etc.).

 

How would I create a hyperlink so that when I do click on the item's name, it will open up the item's information to make it editable?

 

(I have a form that creates the item already, but I want to make it have that information pre-populated for that item so if it needs edited, it could be at that point.)

 

Thanks!

 

Hopefully I explained that ok...

Link to comment
Share on other sites

If you post your attempts so far, with each line commented explaining (simply) why it's there, I can help you through this with examples.

 

I first need to know how fluent you are with the syntax.

 

You should also post the basic structure of your table. If you use PHPMyAdmin, go to your table, and follow the 'Export' link.

Once on that page, select SQL as Export type, and uncheck Data (unless it's small sample data), then click Go.

 

Copy and paste the Create Table block

 

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL COMMENT 'unique auto-increment id',
  `uname` varchar(15) NOT NULL COMMENT 'username of the user',
  `rname` int(30) NOT NULL COMMENT 'real name of the user'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Link to comment
Share on other sites

CREATE TABLE IF NOT EXISTS `items` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `price` int(11) NOT NULL,
  `name` text NOT NULL,
  `type` enum('armor','weapon','item','pet','potion') NOT NULL,
  `bonus` text NOT NULL,
  `slot` enum('not applicable','arm','body','feet','finger','hands','head','legs','neck','pet','shoulders','shield','weapon','wrists') CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
  `target` enum('foe','self') NOT NULL,
  `attr` text NOT NULL,
  `effect` text NOT NULL,
  `value` int(11) NOT NULL,
  `descript` text NOT NULL,
  `location` varchar(255) NOT NULL DEFAULT 'all',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=48 ;

 

Here is what I have for the "list" function so far.

 

<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
include("lib.php");
define("PAGENAME", "Item List");
$table = 'items';

$result = mysql_query("SELECT `name`, `type`, `slot`, `value`, `descript` FROM `items` ORDER BY `name`");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<center><h1>Items</h1></center>";
echo "<center><table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td><center>{$field->name}</center></td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr></center>\n";
}
mysql_free_result($result);

?>

Link to comment
Share on other sites

Okay, well, first thing's first. We have something unique to each item, and that's its ID.

 

We'll use that.

 

Create a page called view.php, and inside it, code something like this

<?php

// Let's verify that a user has accessed this page using an id
if( !isset($_GET['id']) ) {
// If not, end execution of the script, and display an error page
die( 'Bad call to view.php' );
// Otherwise, let's make sure the value is only a digit, to prevent bad values from entering our SQL query
} elseif( !ctype_digit($_GET['id']) ) {
// If not, abort the script
die( 'Bad ID' );
}

// Create a new instance of the MySQLi class
$db = new MySQLi( 'localhost','root','','db' );

// Set up our query. We can plug the user-provided data directly into our query without sanitizing because
// we verified it only contained digits earlier
$query = 'SELECT `name`,`price`,`type` FROM `items` WHERE `id` = ' . $_GET['id'];
// Execute the query
$result = $db->query( $query );

// If no rows were returned, a non-existent id was entered
if( $result->num_rows == 0 ) {
die( 'Bad ID' );
}

// Fetch an array of the results, and put each key into the list()'ed variables
list( $name, $price, $type ) = $result->fetch_row();
// Free up the memory associated with the result
$result->free();

?>

<h3>You want to view ID <?php echo $_GET['id']; ?></h3>
<ul>
<li>Name: <?php echo $name; ?></li>
<li>Price: <?php echo $price; ?></li>
<li>Type: <?php echo $type; ?></li>
</ul>

 

If you access the page normally, it claims there's a 'Bad call to view.php'

 

But if you populate $_GET['id'] using the query string, and call it as view.php?id=1 you will get the results where id = 1

 

Now you just have to include the ID in the rows you are fetching to list them out, and provide that in a link - view.php?id=$row['id']

Link to comment
Share on other sites

1) Forgot to ask, now that I've coded it to bring out the ID as well, how do I make it so that the ID is clickable to go to this view.php page that I just created?

 

2) Would the view.php allow, then, someone to change the information of the item and then be able to re-save it with the new info? (I assume yes, if it had some of the same functions that are in the create_equipment php page that I have.)

Link to comment
Share on other sites

Okay. I now have it so that it opens up the form where I created the item, but the item's details don't populate into the appropriate fields.

 

How do I create that link so that when the person clicks on the item to edit it, it pre-populates the information and then can be resaved?

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.