Jump to content

aminkorea

Members
  • Posts

    18
  • Joined

  • Last visited

    Never

Everything posted by aminkorea

  1. Your are a god send. I tried something similar but I had a few things wrong so it didn't work. Where you have (isset($catid) && $catid=="{$row['id']}" I wrote (isset($catid) && $id=="{$row['id']}" Which gave me an out put where there were eight of every option in the list. It never occurred to me to write the code as you have it. But now that I see it, it makes perfect sense. But since I am new to all this my mind hasn't yet learned what syntax can and cannot be used. Please let me take a shot at explaining this and correct me is I am wrong. In simple English: if the catid is set and it exactly matches (==) the value in the row categories id then echo what has been selected by the user. The echo will be in the format of categories id and category name and will exactly match the links category id. Am I close? Thank you for your help. The script now does exactly what I need. I just hope someday I will know enough that I, too, can be helpful. Cheers
  2. Hey Drummin. I just tried the code you wrote and I got a syntax error. syntax error, unexpected '{' in /home3/johnsesl/public_html/links/linksadmin/update.php on line 30 So, I changed the code a bit so that in the form, where the syntax error was, I changed to in all the form fields. Here is the complete code now, <?php include("menu.php"); include("db.php"); if (isset($_GET['id']) && ctype_digit($_GET['id'])){ $id=$_GET['id']; $sql = "select * from links where id =$id"; $query = mysql_query($sql); while ($row = mysql_fetch_array($query)){ $id = $row['id']; $catid = $row['catid']; $name = $row['name']; $url = $row['url']; $content = $row['content']; } }//if (isset($_GET['id']) && ctype_digit($_GET['id'])) if (isset($_POST['submit_changes'])){ //process update }//if (isset($_POST['submit_changes'])) ?> <html> <head> <title>Update</title> </head> <body> <form action="updated.php" method="post"> <table width="65%" align="center"> <tr> <td align="left"><input type="hidden" value="<?php echo $id; ?>" name="id" /><b>Website Name:</b> <br />Change the name of the website listing.<br /> <input type="text" value="<?php echo $name; ?>" name="name" /> <br> <br><b>URL:</b> <br>Change the URL of the website listing. <br><input type="text" value="<?php echo $url; ?>" name="url"/> <br> <br><b>Description:</b> <br>Change the description of the website listing. <br>Limit 255 characters. <br/><textarea name="content" cols="45" rows="4" wrap="soft"><?php echo $content; ?></textarea> <br> <?php $result = mysql_query("SELECT id, categories FROM categories") or die(mysql_error()); echo "<select name=\"catid\">\r"; while ($row = mysql_fetch_array($result)) { echo "<option value=\"{$row['id']}\"" . (isset($id) && $id=="{$row['id']}" ? ' selected="selected"' : '') . ">{$row['id']}-{$row['categories']}</option>\r"; } echo "</select>\r"; ?> <div align="center"> <input type="submit" name="submit_changes" value="submit changes" /> </div></td> </tr> </table> </form> </body> </html> The code now displays all the correct information in the form fields, and it defaults to a single category within the list. The problem now is that the default category that the code chooses is equal to the data ID and not the catid. So, if a record has an id of 8 the default category chosen by t he code is also 8, but the id in the links is not associated with the categories, so it is the wrong default category. Example: links.id = 8 links.catid = 3 links.name = http://somesite.com || categories.id = 3 categories.categories = Bilingual Education Output: URL = http://somesite.com Name = Somesite Content = Description Default Category = 8-CALL I hope all that made sense..
  3. Thanks Drummin. I will try this and let you know the results. I appreciate the help. Cheers
  4. If I can get this fixed, I will have completed all but the admin login for this project - my first php/mysql project. Here is what I need. I have a list_records.php that list all the records in the table 'links' and the category each entry is in from the table 'categories'. Here are my table structures. -- Table structure for table `categories` -- DROP TABLE IF EXISTS `categories`; CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `categories` varchar(37) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ; -- -------------------------------------------------------- -- -- Table structure for table `links` -- DROP TABLE IF EXISTS `links`; CREATE TABLE IF NOT EXISTS `links` ( `id` int(4) NOT NULL AUTO_INCREMENT, `catid` int(11) DEFAULT NULL, `name` varchar(255) NOT NULL DEFAULT '', `url` varchar(255) NOT NULL DEFAULT '', `content` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `catid` (`catid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ; On the update.php file, I have a form that lets me make changes to the record. Here is the codes for update.php <? include "menu.php" ?> <? include "db.php" ?> <?php $id=$_GET['id']; $sql = "select * from links where id =$id"; $query = mysql_query($sql); while ($row = mysql_fetch_array($query)){ $id = $row['id']; $catid = $row['catid']; $name = $row['name']; $url = $row['url']; $content = $row['content']; //we will echo these into the proper fields } mysql_free_result($query); ?> <table width="65%" align="center"> <tr><td align="left"> <form action="updated.php" method="post"> <input type="hidden" value="<?php echo $id; ?>" name="id"/> <br> <b>Website Name:</B><br> Change the name of the website listing.<br> <input type="text" value="<?php echo $name; ?>" name="name"/> <br> <br> <b>URL:</b><br> Change the URL of the website listing.<br> <input type="text" value="<?php echo $url; ?>" name="url"/> <br> <br> <b>Description:</b><br> Change the description of the website listing.<br> Limit 255 characters.<br/> <textarea name="content" cols="45" rows="4" wrap="soft"><?php echo($content);?></textarea> <br> <?php $result = mysql_query("SELECT id, categories FROM categories") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $id = $rows["id"]; $categories=$row["categories"]; $options.= '<option value="'.$row['id'].'">'.$row['id'].'-'.$row['categories'].'</option>'; }; ?> <SELECT NAME=catid> <OPTION VALUE=selected><? echo $catid; ?><? echo $options; ?></OPTION> </SELECT> <?php mysql_close(); ?> <div align="center"> <input type="submit" value="submit changes"/> </div> </form> <br> </td></tr></table> The part of he code I need help with is <?php $result = mysql_query("SELECT id, categories FROM categories") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $id = $rows["id"]; $categories=$row["categories"]; $options.= '<option value="'.$row['id'].'">'.$row['id'].'-'.$row['categories'].'</option>'; }; ?> <SELECT NAME=catid> <OPTION VALUE=selected><? echo $catid; ?><? echo $options; ?></OPTION> </SELECT> I want it to default to the category that the entry is in. If you look, you will see in the select portion that I I have <$ echo $catid; ?> which echos the proper category ID, but if I use <? echo $categories; ?> it echos Writing, which is the last category in the list. Yet, the $options echo the catid and it corresponding category. How can I get the default option to echo BOTH the catid and category name while also listing all the other categories so that the records can be moved to a new category is needed? Any help will be appreciated. Thank you in advance.
  5. I decided to change the code to echo only the category without the dropdown list, so this has been solved. But I still need to know how to getthe drop down on the update.php to default, I will post the code for that soon. Thanks and have a nice day.
  6. I forgot to include my version of mysql. MySQL client version: 5.1.61
  7. I am working on a project that uses a drop down list to chose the category when inserting new data into the database. What I want to do now is make the drop down list default to the chosen category on the list records page and the update page. I have read several tutorials, but they all say that I have to list the options and then select the default. But since it is possible to add and remove categories, this approch won't work. I need the code to chose the correct category on the fly. There are two tables, one that has the category ID and category name. The second table has the data and the catid which is referenced to the category id in the first table. -- -- Table structure for table `categories` -- DROP TABLE IF EXISTS `categories`; CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `categories` varchar(37) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ; -- -------------------------------------------------------- -- -- Table structure for table `links` -- DROP TABLE IF EXISTS `links`; CREATE TABLE IF NOT EXISTS `links` ( `id` int(4) NOT NULL AUTO_INCREMENT, `catid` int(11) DEFAULT NULL, `name` varchar(255) NOT NULL DEFAULT '', `url` varchar(255) NOT NULL DEFAULT '', `content` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `catid` (`catid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ; Then is the list records file, I have <?php include ("db.php"); include ("menu.php"); $result = mysql_query("SELECT categories FROM categories") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $categories=$row["categories"]; $options.= '<option value="'.$row['categories'].'">'.$row['categories'].'</option>'; }; $id = $_GET['id']; $query="SELECT * FROM links ORDER BY catid ASC"; $result=mysql_query($query); ?> <table width="65%" align="center" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="100%" border="1" cellspacing="0" cellpadding="3"> <tr> <td colspan="7"><strong>List data from mysql </strong> </td> </tr> <tr> <td align="center"><strong>Category ID</strong></td> <td align="center"><strong>Category ID</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>URL</strong></td> <td align="center"><strong>Content</strong></td> <td align="center"><strong>Update</strong></td> <td align="center"><strong>Delete</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td> <SELECT NAME=catid> <OPTION>Categories</OPTION> <?php echo $options; ?> </SELECT> </td> <td><? echo $rows['catid']; ?></td> <td><? echo $rows['name']; ?></td> <td><a href="<? echo $rows['url']; ?>"><? echo $rows['url']; ?></a></td> <td><? echo $rows['content']; ?></td> <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td> <td align="center"><a href="delete.php?id=<? echo $rows['id']; ?>">delete</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> So, how do I get this code $result = mysql_query("SELECT categories FROM categories") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $categories=$row["categories"]; $options.= '<option value="'.$row['categories'].'">'.$row['categories'].'</option>'; }; <SELECT NAME=catid> <OPTION>Categories</OPTION> <?php echo $options; ?> </SELECT> to give me an output that will be something like if catid exactly matches categories.id echo categories.categorie ??? so far everything I have done produces either a default category of the last category, the catid (which is a number), all of the categories (logical since catid will always be = id, or nothing. How do I get just the category name? I will keep reading and try to figure this out, but any help would be greatly appreciated. Thanks in advance
  8. if you have phpmyadmin you can add a primary key very easily. If not try try ALTER TABLE table_name ADD PRIMARY KEY (column_name) In your case, ALTER table users ADD primary key (uname) should work.
  9. Sorry for the lengthy time between posts, but been busy with other stuff. Psycho, I did exactly as you said using the table structure you provided and it didn't work. I finally gave up and returned to my original table structure. However, your did provide the missing part I was looking for. I made a few modifications to my table and the code and it now works like I had intended. My table structure is now -- -- Table structure for table `categories` -- DROP TABLE IF EXISTS `categories`; CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `categories` varchar(37) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ; -- -------------------------------------------------------- -- -- Table structure for table `links` -- DROP TABLE IF EXISTS `links`; CREATE TABLE IF NOT EXISTS `links` ( `id` int(4) NOT NULL AUTO_INCREMENT, `catid` int(11) DEFAULT NULL, `name` varchar(255) NOT NULL DEFAULT '', `url` varchar(255) NOT NULL DEFAULT '', `content` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `catid` (`catid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ; As you can see, I changed 'categories' in my original links table to catid and added a foreign key. The I changed the code in my php files to <? include "db.php" ?> <?php $id = intval($_GET['id']); // force user input to an int if(!empty($id)) // if the user input is not empty or 0 { $query = "SELECT * FROM categories WHERE id = $id LIMIT 1"; // attempt to select the row for the given id $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) > 0) // if the categorie was found { $row = mysql_fetch_assoc($result); // fetch the resultset to an array called $row echo $row['categories']; // echo the categories field from the row // etc. } else { die('Error: Bad ID'); // the categories was not found } } else { die('Error: Bad ID'); // the id given was not valid } mysql_close(); ?> <? include "db.php" ?> <?php $id = $_GET['id']; $sql="SELECT name,url,content FROM links where catid = $id"; $result=mysql_query($sql); ?> <table width="80%" align="center" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="100%" border="1" cellspacing="0" cellpadding="3"> <tr> <td colspan="3"><strong>List data from mysql </strong> </td> </tr> <tr> <td align="center"><strong>Name</strong></td> <td align="center"><strong>URL</strong></td> <td align="center"><strong>Content</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['name']; ?></td> <td><a href="<? echo $rows['url']; ?>"><? echo $rows['url']; ?></a></td> <td><? echo $rows['content']; ?></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> Now when I click a category from the list of categories in list_categories.php, list_records.php displays the category and any links posted in that category. Problem solved. Thanks for your help. Cheers
  10. Back again. I have worked out most of the bugs and can now add and list records in both the categories and the links table. But I haven't yet figured out how to get the links table to grab the cat_id that identifies the category name (cat_name) I am using a drop down list on the add and update scripts. I select the category that the link goes into and click submit. All the information is then inserted into the links table, except the cat_id. The cat_id always inserts as 0. The structure of my tables is `categories`; CREATE TABLE IF NOT EXISTS `categories` ( `cat_id` tinyint(2) unsigned NOT NULL AUTO_INCREMENT, `cat_name` varchar(37) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL, PRIMARY KEY (`cat_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=37 ; `links`; CREATE TABLE IF NOT EXISTS `links` ( `link_id` mediumint(6) unsigned NOT NULL AUTO_INCREMENT, `cat_id` tinyint(2) unsigned NOT NULL, `cat_name` varchar(255) NOT NULL, `link_name` varchar(255) NOT NULL, `url` varchar(255) NOT NULL, `content` varchar(255) NOT NULL, PRIMARY KEY (`link_id`), KEY `cat_id` (`cat_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ; I use the following form to add to the links table. <?php $result = mysql_query("SELECT cat_id,cat_name FROM categories") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $cat_id=$row["cat_id"]; $cat_name=$row["cat_name"]; $options.= '<option value="'.$row['cat_name'].'">'.$row['cat_name'].'</option>'; }; ?> <?php mysql_close(); ?> <title>Links add to table form add.php</title> <center> <form id="addtolinks" action="added.php" method="post" name="addtolinks"> <table width="448" border="0" cellspacing="2" cellpadding="0"> <tr><td width="150"> <input id="cat_id" name="cat_id" type="hidden" valuse=""> </td></tr> <tr><td width="150"> <div align="right"> <label for="categories">Category Name</label> </div> </td><td><select id="cat_name" name="cat_name"><options><?php echo $options; ?></select"> </td> </tr><tr> <td width="150"> <div align="right"> <label for="name">Website Name</label> </div> </td> <td> <input id="link_name" name="link_name" type="text" size="25" value="Site Name" maxlength="25"> </td> </tr><tr> <td width = "150"> <div align="right"> <label for="url">Website URL</label> </div> </td> <td> <input id="url" name="url" type="text" size="25" value="http://" maxlength="100"> </td> </tr><tr> <td width = "150"> <div align="right"> <label for="content">A brief Description</label> </div> </td> <td> <textarea id="content" name="content" rows="4" cols="40"> </textarea></td></tr><tr><td width="150"></td><td> <input type="submit" name="submitButtonName" value="Add"> </td> </tr></table> </form> </center> and this is the code that actually inserts the information that is submitted. $query="INSERT INTO links ('links.cat_id') SELECT cat_id FROM categories WHERE categories.cat_name = links.cat_name;" ?> <? $link_id =trim($post['link_id']); $cat_id = trim($_POST['cat_id']); $cat_name = trim($_POST['cat_name']); $link_name = trim($_POST['link_name']); $url = trim($_POST['url']); $content = trim($_POST['content']); $query = "INSERT INTO links (link_id, cat_id, cat_name, link_name, url, content) VALUES ('', '$cat_id','$cat_name', '$link_name', '$url', '$content')"; $results = mysql_query($query); if ($results) { echo "Details added."; } mysql_close(); ?> I am not getting any errors. And everything is being inserted except the cat_id. So, how do I get the cat_id from the categories table to get added to the links table when the category is selected in the submit form? What am I missing? Again, thanks in advance for any help. Cheers
  11. Hey Psyco, When I added the date to the table, I chose CURRENT TIMESTAMP update Current Timestamp. The sql table, as you said, defaulted to 0000.00.00. I then added the code that you provided and I got the default on the page. So, I went back to the table settings and changed it to DATE with update currenet timestamp. The page then went blank. I tried a couple other variations of date fields and I remember that at one point I got a default date of 1970. I didn't know that the field set it self according to the UNIX Epoch date until after I had removed the coded and altered my tables to values that you suggested(minus the date_added row). But, I didn't just give up because it didn't work. I just put more priority on getting the content to display than the date. I worked with the code for the new tables all day yesterday and for a few hours today, and I still haven't got it to work, but I think I am closer. I will try to do some debugging tomorrow, but I will have to study up on how to debug first. Please keep in mind that I have only begun to learn this language and there is a lot to learn. Thanks for your input and advise.
  12. Just thought I should add that I have dropped the date_added row from the table. The date added is not important right now. What's important is that I get the content in the links table to display under the corrent category. I'm going to have a go at Psyco's table again and see if I can get it to work.
  13. Psycho, I appreciate the time and effort, but your code isn't working. I change the date_added as you suggested to `date_added` timestamp NULL default CURRENT_TIMESTAMP, Then I inserted the code you gave me into list_records.php in the admin (This is the only place I have the date added called from the db) The result was nothing. No error, no date displayed, just a blank column.
  14. Well, I had a few extra hours so, I tried changing the table structure and the respective code. Unfortunately, the changes actually made things worse. So changed everything back. Am not sure where to go from here.
  15. Thanks Psycho, I had come to the same conclusion about the table structure. I knew the structure was not correct, but this is my first php, mysql project, so I wasn't sure exactly where I had messed up. Like I said in my original post, I got the back end running and everything worked the way I wanted. It wasn't until I started working on the front end of the site that I realized that I had screwed up somewhere. I was hoping for an easier solution, but it looks like I am going to have to start over and see if I can get it right this time. It will take a few day, but I will give your suggestions a try and see what happens. Thanks again, I will post my results after I have made the changes and let you know how everything turns out. Cheers.
  16. I altered my tables and added a unique key on categories.categories and a foreign key of links.categories referencing the categories.categories and get the same results as before I altered the tables.
  17. Thanks for the quick reply. Here is the table structures. I have a PRIMARY KEY in both tables, but no UNIQUE or FOREIGN KEY. So, if I add a foreign key of links.categories and reference it to the id in categories, would I then be able to call the specific links for each category? CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `categories` varchar(37) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ; CREATE TABLE IF NOT EXISTS `links` ( `id` int(4) NOT NULL AUTO_INCREMENT, `categories` varchar(65) NOT NULL DEFAULT '', `name` varchar(255) NOT NULL DEFAULT '', `url` varchar(255) NOT NULL DEFAULT '', `content` varchar(255) NOT NULL DEFAULT '', `date_added` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
  18. Hi, I am new to php, mysql and have made some progress, but recently ran into a wall. I hope someone can help me. I am building a simple links script that has categories and a list of links with website name, url and a brief description. I have two tables-categories and links. I have gotten the back end finished and can add, edit and delete links and categories. The problem is in the front end. I have the script set up so that when I click on a category name a page is dynamically created using ?id=idnumber. and the category name is echoed on the page. But I can't seem to find the solution I need to get the links that have a matching category name to display. I have tried using JOIN, LEFT JOIN, INNER JOIN, and RIGHT JOIN and get the same result, ALL the links in the links table are displayed. I think I need to create a new array and use the foreach loop to accomplish this, but I am not sure. Nor am I sure how to use the foreach even though I have read several tutorials on it. Here is the code I have so far. Any help would be appreciated. Thanks in advance. <? include "db.php" ?> <?php $id = intval($_GET['id']); // force user input to an int if(!empty($id)) // if the user input is not empty or 0 { $query = "SELECT * FROM categories WHERE id = $id LIMIT 1"; // attempt to select the row for the given id $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) > 0) // if the categorie was found { $row = mysql_fetch_assoc($result); // fetch the resultset to an array called $row echo $row['categories']; // echo the categories field from the row // etc. } else { die('Error: Bad ID'); // the categories was not found } } else { die('Error: Bad ID'); // the id given was not valid } mysql_close(); ?> <? include "db.php" ?> <?php $sql="SELECT * FROM links,categories ORDER BY links.categories LIMIT 10"; $result=mysql_query($sql); ?> <table width="80%" align="center" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="100%" border="1" cellspacing="0" cellpadding="3"> <tr> <td colspan="3"><strong>List data from mysql </strong> </td> </tr> <tr> <td align="center"><strong>Name</strong></td> <td align="center"><strong>URL</strong></td> <td align="center"><strong>Content</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['name']; ?></td> <td><a href="<? echo $rows['url']; ?>"><? echo $rows['url']; ?></a></td> <td><? echo $rows['content']; ?></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?>
×
×
  • 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.