Jump to content

Need a dropdown menu for a form that can be populated by one of my tables


shenagsandnags

Recommended Posts

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.

 

 

Link to comment
Share on other sites

That should work. but I may have missed something. 

 

<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"];

   for($i = 0;$i<count($categoryid);$i++){
      echo "<option value='$categoryid[$i]'>$category[$i]</option>";
   }
}
php?>

</select>

   
      <input name="" type="submit" value="send" />

      </form>
</html>

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.