zelig Posted November 12, 2011 Share Posted November 12, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/251028-linking-to-field-in-database-hyperlink/ Share on other sites More sharing options...
xyph Posted November 12, 2011 Share Posted November 12, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/251028-linking-to-field-in-database-hyperlink/#findComment-1287700 Share on other sites More sharing options...
zelig Posted November 13, 2011 Author Share Posted November 13, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/251028-linking-to-field-in-database-hyperlink/#findComment-1287711 Share on other sites More sharing options...
xyph Posted November 13, 2011 Share Posted November 13, 2011 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'] Quote Link to comment https://forums.phpfreaks.com/topic/251028-linking-to-field-in-database-hyperlink/#findComment-1287727 Share on other sites More sharing options...
zelig Posted November 13, 2011 Author Share Posted November 13, 2011 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.) Quote Link to comment https://forums.phpfreaks.com/topic/251028-linking-to-field-in-database-hyperlink/#findComment-1287788 Share on other sites More sharing options...
zelig Posted November 15, 2011 Author Share Posted November 15, 2011 *bump* Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/251028-linking-to-field-in-database-hyperlink/#findComment-1288479 Share on other sites More sharing options...
zelig Posted November 21, 2011 Author Share Posted November 21, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/251028-linking-to-field-in-database-hyperlink/#findComment-1289872 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.