Jump to content

shenagsandnags

Members
  • Posts

    34
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

shenagsandnags's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. been trying to get a code up and working for a update database form, couldnt really get any working i found from google and then after a bit of help from here i got a crazy idea and stopped dead and my tracks and switched gears. so i thought, why not just use the form i used to insert my database and change it around a little. same concept, how should this not be possible ? for reference here are my tables and structure Movies ID(PK) Title Category(FK) URL 0 Name 1 http://www. Categories ID(PK) Category 1 Drama note: i know i should change Category from movies to CategoryID for less confusion. to make long story short when i tried i got errors, to much time to go back over everything so i will just stick with what i got for now. ok so my insert form inserts a movie title,category and url into my movies table. the dropdown for category is populated with a list of categories in my categories table, the way the code is writen it shows the actual category name instead of the id inside the dropdown. the form goes like: Title: textfield1 Category:dropdown URL:textfield2 so what i did was took the code from it, replaced title textfield1 with the same dropdown for category and just edited the select and echo to pull and show movie titles. i also added a WHERE to it so i can only select titles in a certian category. so it should look like, Title: dropdown Category:dropdown so here is the code below (ignore leftovers from URL or anything else i plan on taken them out pending the outcome) <html> <form id="form1" name="Update" method="post" action="ad3.php"> <label> Title: <input type="text" name="Title" id="Title" /> </label> <br /> <select name='dropdownt' id='dropdownt'> <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect to DB: ' . mysql_error() ); } mysql_select_db ("5", $con); $query = "SELECT Title FROM movies WHERE Category='1'"; $result = mysql_query($query) OR DIE ("There was an error" .mysql_error()); while($row=mysql_fetch_array($result)) { echo " <option value=\"{$row['Title']}\">{$row['Title']}</option>"; } php?> </select> <select name='dropdown' id='dropdown'> <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect to DB: ' . mysql_error() ); } mysql_select_db ("5", $con); $query = "SELECT ID, Category from categories"; $result = mysql_query($query) OR DIE ("There was an error" .mysql_error()); while($row=mysql_fetch_array($result)) { echo " <option value=\"{$row['ID']}\">{$row['Category']}</option>"; } php?> </select> <input name="" type="submit" value="send" /> </form> </html> one thing that i could not figure out (due to my lack of php knownledge) is that after my SELECT i have the echo " <option value=\"{$row['Title']}\">{$row['Title']}</option>"; before i had edited this code there was ID where the first 'Title' is. after trying to take one part of that out i got a error so i just left it. so what i thought i would do for the process page (ad3.php) i would just turn the INSERT into a UPDATE. <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect to DB: ' . mysql_error() ); } mysql_select_db ("5", $con); $sql="UPDATE movies SET Category = '$_POST[dropdown]', Title = '$_POST[dropdownt]' LIMIT 1"; if (!mysql_query($sql,$con)) { die ('Error: ' . mysql_error()); } echo "<a href=\"form2.php\">Record added. Click here to make a new entry</a>"; the surprising thing (to me anyways) is that it actually worked! well.. ALMOST it should work by letting me select a certian title thats in a certian category and change its current category into a different one and then submit. after a few hours i finaly got it to do what i need BUT the problem i am having is that when looking into my phpmyadmin, it just changes the name and category to the first record in my movies table which is id "0". one of the too. i almost done it on my own.. almost... sorry for the extended post but i didnt want to leave anyone confused. so my question is, did i forget to add anything, take anything out, miss a change i needed to make ? maybe its the echos after my SELECT in my form >
  2. again my tables are movies| ID(PK) - Titles - Category(FK) - URL 0 Name 1 www.xyz.com categories| ID(PK) - Category 1 Comedy say i create a query SELECT * FROM movies WHERE Category="1" and it gave me all the movies from my table that is in the comedy category which should look like this: ID - Title - Category - URL 0 Name Comedy www.xyz 1 xyz Comedy www.abc etc.. How can i add a drop down menu beside each movie listed in that result that would let me change the category for it and save it ? like this, ID - Title - Category - URL 0 Name Comedy www.xyz [Dropdown] 1 xyz Comedy www.abc [Dropdown] [save Button] being a newbie im going to guess that i will need a INSERT in here somewhere and not really sure how i would write my result echos. i have been trying all kinds of "update records" forms and cant get them right so i figured it may be easier to just try and create my own. if i could get some example or if you have more simple suggestions
  3. you know its weird because i should have already known that but it just goes to show how not focusing on one thing at a time will make you over look the simple solutions. thanks guys works great!
  4. here is my form layout, Title: txtfield URL: txtfield Category: dropdown (pulls the just category fields in my "categories" table submit - when working right this form is suppose to enter this info into my "movies" table. Code to form.php <html> <form id="form1" name="Update" method="post" action="add3.php"> <label> Title: <input type="text" name="Title" id="Title" /> </label> <br /> <label> URL: <input type="text" name="URL" id="URL" /> </label> <select name='dropdown' id='dropdown'> <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect to DB: ' . mysql_error() ); } mysql_select_db ("mydb", $con); $query = "SELECT Category from categories"; $result = mysql_query($query) OR DIE ("There was an error" .mysql_error()); while($row=mysql_fetch_array($result)) { $category = $row['Category']; echo " <option value=\"$category\">$category</option>"; } php?> </select> <input name="" type="submit" value="send" /> </form> </html> then here is the code to the process page (add.php) <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect to DB: ' . mysql_error() ); } mysql_select_db ("m5", $con); $sql="INSERT INTO movies (Category, URL, Title) VALUES ('$_POST[dropdown]','$_POST[url]','$_POST[Title]')"; if (!mysql_query($sql,$con)) { die ('Error: ' . mysql_error()); } echo "Record added"; after i submit my form i get "Error: Cannot add or update a child row: a foreign key constraint fails (`mydb`.`movies`, CONSTRAINT `movies_ibfk_1` FOREIGN KEY (`Category`) REFERENCES `categories` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE)" correct me if im wrong but im almost %99 sure that my tables are set up correctly for my project. just to be sure, movies ------------ ID (PK) Title Category (FK) URL categories --------------- ID (PK) Category at first i was also getting a "undefined index" along with this error but i reversed the values in the INSERT so now im just getting the CONSTRAINT error so this now leaves me to beleive that i may have made a error one of the tags possibly. also while i was looking around i decided to create a different procces page hoping i would run into some quick luck: add3.php <?php $Category=$_POST['dropdown']; $URL=$_POST['URL']; $Title=$_POST['Title']; $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect to DB: ' . mysql_error() ); } mysql_select_db ("mymb", $con); $query = "INSERT INTO movies VALUES ('$Category','$URL','Title')"; $result= mysql_query($query); if ($result) { echo "Successful"; } else { echo "Failed"; } mysql_close(); ?> and this one gives me "Failed" error. i have noticed that there are many many ways of doing this so i went ahead and posted both process page hoping this could give some better ideas rather then confusion (i hope anyways). im not really sure which ones its more along the lines of php/form "standard" if there is any but whichever may be the easiest for you to take a look at it doesnt matter and i would be greatly thankful. IMO i think its just a varriation of not having the formats right and just not taylored correctly to my needs. many thanks in advanced.
  5. hey all. i previously posted about how i lost all my data so im trying to recreate my form again. after not many replies from my desperate call for help i tried to figure it out on my own, i spent hours and hours going through this forum and others but im just running into brick walls because of my minor knowledge of php (though i do have to say i am picking it up a little!) my form is short and simple so this shouldnt be too hard but this is what i have come up with. my tables ---------------- Movies: ID(P_id),Title,Category (F_K),URL Categories: ID (P_Id),Category All i am needing my form to do is enter records into my movies table, therefore it goes like this: Title: "textbox" URL: "textbox" Category: "dropdown menu" "submit" now the thing here is that the dropdown menu needs to be populated with data from Category.Categories table so i can add that info into category in the movies table. so pretty much it will be sending the ID from Categories table into Category in the Movies and yes they both have the same datatype. so here is what i have so far. my form.php <html> <form id="form1" name="Update" method="post" action="add.php"> <label> First Name: <input type="text" name="Title" id="textfield" /> </label> <br /> <label> Last Name: <input type="text" name="URL" id="textfield2" /> </label> <select name='Category'> <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect to DB: ' . mysql_error() ); } mysql_select_db ("mydb", $con); $query = "SELECT Category from Catgories"; $result = mysql_query($query) OR DIE ("There was an error .mysql_error()); while ($line = mysql_fetch_array($result,MYSQL_ASSOC)){ $categoryid = $line["playerid"]; $category = $line["category"]; echo "<option value='$categoryid'>$category</option>\n"; } php?> </select> <input name="" type="submit" value="send" /> </form> </html> and then my add.php <?php $con = mysql_connect("localhost", "root", ""); if (!$con) { die('Could not connect to DB: ' . mysql_error() ); } mysql_select_db ("mydb", $con); $sql="INSERT INTO movies (title, url) VALUES ('$_POST[title]','$_POST[url]')"; if (!mysql_query($sql,$con)) { die ('Error: ' . mysql_error()); } echo "Record added"; so first things first, although i have tested the code its throwing me a variable error i think on line 30 so i figured that instead of focusing on just the error i should just have everything looked over since i dont have anyone i can turn to about this stuff to make sure i didnt forget any " or { etc.. i am also not sure if the while loop is even needed in form.php, i kind of taylored this code to my own needs. THEN i have the add.php, granted that by some miracle that the form.php is correct i dont know what else i would need to add to it for my dropdown. i really need this bad and i hope someone can help me you know. if anyone has any ideas for a more simple code please, enlighten me but just give me some good examples because i really am a beginner. thanks alot.
  6. sort of, it didnt have all those elements and such, tables etc. but if you leave me with just that code i will be lost. i know the actual form page itself is pretty simple. the 2 things that are really stopping me is i have no idea how to put the drop down box inside the form and make it pull the categories from the categories table and then the post.php i have no clue what the code was for it.
  7. had a leak in my home which ruinned my pc but luckly i had most of my project and db info backed up on a stick but i didnt have my forms backed up! can someone please help me! the form was actually pretty basic but since i am still new too php simple things are still a challenge. (my db is based off 2 tables Movies,Categories) Movies: ID,Title,Category(FK),URL Categories: ID,Category so the first part was for entering movie info into my movies table while selecting a category from the categories table. ----------------------------------------------- Title: (txt field) Category: a dropdown/combo box that would list the categories from the categories table. URL: (txt field) and a sumbit button. like i said this was a pretty basic form. the second part showed a single list of all my movie titles, when i selected one i was able to change the category that the title was listed in, i dont know how to explain how it was setup but i am in such a rush to get this fixed and caught back up that i dont care how this next one works as long as it lets me do what i needed it too do. i also had another that let me add/delete categores in the categories table but i can deal with that another time and just manually enter it with myphpadmin when needed because i have to get the above asap.. i will be so greatful if anyone can help me get my stuff back on!
  8. ok i have just now noticed this echo "<tr><td><a href='".$row['URL']."'>" . $row['Title'] . "</a></td></tr><br/>"; whenever i hover my mouse over one of the links, in my browsers status bar it will show it as if its in my root directory "http://www.mysite.com/root/www.google.com" instead of just "www.google.com". and obviously when clicked on it wont work. through out this whole process i haven't touch the <a href" line so im kinda clueless on this one. besides i need to add the target function in this but im not sure where and how to put it. i have tried, echo "<tr><td><a href='".$row['URL']."' target="_blank">" . $row['Title'] . "</a></td></tr><br/>"; and a few other things but i just get a syntax error. help me get this figured out so i can seal this topic before they start charging me rent lol.
  9. yea but shouldnt echo " . $row['Category'] . " echo "<a href='".$row['URL']."'>" . $row['Title'] . "</a>"; just give me: Drama Movie1 Movie2 Comedy Movie3 Horror Movie4 Movie5 ? believe it or not as dumb as i sound about php i use to know a little html back in the day. i do know how to create tables but i really dont care to have the tables for this code unless i absolutely have to (not sure if its required in php). just a simple listing without boarders and such as shown above would be just fine for me
  10. ok so when i test the code i displays like this, DramaMovie1Movie2ComedyMovie3HorrorMovie4Movie5 so technically speaking this is almost exactly what i wanted but obviously there is a problem with the "way" its displaying it which i assume thats why you were telling me about the elements. im pretty sure i understood you correctly so i tried what you suggested and even took out the elements completely but the displays are still not changing, maybe there is something i am forgetting. Even this does not change anything. echo "<td><th>" . $row['Category'] . "</td></th>"; $last_category = $row['Category']; // 'remember' the new category value } echo "<tr><td><a href='".$row['URL']."'>" . $row['Title'] . "</a></td></tr>"; i really dont care for the tables i would rather just keep it simple and have no tables and just put the category names in bold and walla! just as long as i can have them display vertically rather then horizontally but when i take out the elements it throws me a syntax error *scratching my head*
  11. like this ? <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mydb", $con); $result = mysql_query("SELECT a.ID, a.Title, b.Category, a.URL FROM Movies AS a JOIN Categories b ON a.Category = b.ID ORDER BY b.Category"); $last_category = ''; // initialize to a value that will never exist in the data while($row = mysql_fetch_array($result)){ // test if the category changed if($last_category != $row['Category']){ echo "<td width='150'>" . $row['Category'] . "</td>"; $last_category = $row['Category']; // 'remember' the new category value } echo "<td width='100'><a href='".$row['URL']."'>" . $row['Title'] . "</a></td>"; } mysql_close($con); ?>
  12. lol STILL no difference. if its working for you then what else could it be on my end ? or have you even tested it yourself ?
  13. lol, still no difference. double check that i made correct changes. <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mydb", $con); $result =mysql_query("SELECT DISTINCT category FROM categories"); $result_num=mysql_num_rows($result); if(!empty($result_numm)) { echo "<table>"; while($result_array=mysql_fetch_array($result)) { $category=$result_array['category']; echo "<tr><td style='border-bottom:dashed 1px #000000'>$category</td></tr>"; $check_table2=mysql_query(sprintf("SELECT * FROM movies WHERE ID='%s'", mysql_real_escape_string($category))); $check_table2_num=mysql_num_rows($check_table2); if(!empty($check_table2_num)) { while($check_table2_array=mysql_fetch_array($check_table2)) { $title=$check_table2_array['Title']; echo "<tr><td>$title</td></tr>"; } } } echo "</table>"; } mysql_close($con); ?>
  14. still no difference, lol your such a tease
  15. believe it or not still no difference, here is the exact code just to make sure that i didnt mess anything up myself. <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mydb", $con); $result =mysql_query("SELECT DISTINCT category FROM categories"); $result_num=mysql_num_rows($result); if(!empty($result)) { echo "<table>"; while($result_array=mysql_fetch_array($result)) { $category=$result_array['category']; echo "<tr><td style='border-bottom:dashed 1px #000000'>$category</td></tr>"; $check_table2=mysql_query(sprintf("SELECT * FROM movies WHERE ID='%s'", mysql_real_escape_string($category))); $check_table2_num=mysql_num_rows($check_table2); if(!empty($check_table2_num)) { while($check_table2_array=mysql_fetch_array($check_table2)) { $title=$check_table2_array['Titles']; echo "<tr><td>$title</td></tr>"; } } } echo "</table>"; } mysql_close($con); ?> so close so close i can taste it!
×
×
  • 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.