将下拉列表数据添加到MySQL数据库中

时间:2014-03-01 15:28:49

标签: php mysql

我在将注册页面上的下拉菜单中的数据插入数据库时​​遇到问题。

以下是注册页面:

      <html>
      <title>favNet!</title>
<head>
<link href="CSS/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="headerMenu">
    <div id="wrapper">
        <div class="logo">
        <a href="index.html"><img src="pics/logo.png"></a>
            <div id="menu">
                <a href="index.html">Home</a>
                <a href="about.html">About</a>
                <a href="#">Contact</a>
                <a href="#">FAQ & Rules</a>
            </div>
        </div>
    </div>
</div>
<table>
<tr>
    <td width="15%" valign="top">
    <br />
        <h1 align="center">Already a Member? Login Below</h1>
        <br/>
            <form action="#" method="POST" align="center">
                <input class ="textbox" type="text" size="25" name="username" placeholder="Username"/><br /><br />
                <input class ="textbox" type="password" size="25" name="password" placeholder="Password"/><br /><br />
                <input class= "submit" type="submit" name="submit" value="Sign In">
                <br />
                <br />
                <br />
                <br />
                <img src="pics/sidemenu_img.png" width="350" height="300">
            </form>
    </td>
    <td width="55%" valign="top">
    </td>
    <td width="30%" valign="top">
        <br />
        <h1 align="center">Not Enjoying the Awesomeness???</h1>
        <h1 align="center">Than Sign Up Here!!!</h1>
    </br />
        <form action="signed-up.php" method="POST" align="center">
        <input class ="textbox" type="text" size="25" name="fname" placeholder="First Name"/><br /><br />
        <input class ="textbox" type="text" size="25" name="lname" placeholder="Last Name"/><br /><br />
        <input class ="textbox" type="text" size="25" name="username" placeholder="Username"/><br /><br />
        <input class ="textbox" type="text" size="25" name="nname" placeholder="Nickname"/><br /><br />
        <input class ="textbox" type="text" size="25" name="email" placeholder="Email"/><br /><br />
        <input class ="textbox" type="text" size="25" name="age" placeholder="Age (Example: 21)"/><br /><br />
        <input class ="textbox" type="password" size="25" name="password" placeholder="Password"/><br /><br />
        <input class ="textbox" type="password" size="25" name="password2" placeholder="Repeat Password"/><br /><br />
        <select align="right" class="select" name="tv">
            <option>Favorite TV Show</option>
            <option>Game of Thrones</option>
            <option>Breaking Bad</option>   
            <option>The Walking Dead</option>
            <option>The Simpsons</option>
            <option>South Park</option>
            <option>Family Guy</option>
            <option>Arrested Development</option>
            <option>American Horror Story</option>
            <option>True Detective</option>
            <option>Once Upon a Time</option>
            <option>Pretty Little Liars</option>
        </select>
        <br />
        <br />
        <select align="right" class="select" name="movie">
            <option>Favorite Movie Series</option>
            <option>Star Wars</option>
            <option>Fast & Furious</option>
            <option>Lord of the Rings/The Hobbit</option>
            <option>Harry Potter</option>
            <option>Star Trek</option>
            <option>The Avengers</option>
            <option>Batman</option>
            <option>James Bond</option>
            <option>Pirates of the Caribbean</option>
            <option>X-Men</option>
            <option>Transformers</option>
            <option>The Hunger Games</option>
            <option>The Hangover</option>
            <option>Alien</option>
            <option>Predator</option>
            <option>Jurassic Park</option>
            <option>Ghostbusters</option>
            <option>Back to the Future</option>
            <option>Die Hard</option>
            <option>Rush Hour</option>
            <option>Planet of the Apes</option>
            <option>Austin Powers</option>
            <option>American Pie</option>
        </select>
        <br />
        <br />
        <select align="right" class="select" name="book" id="book">
            <option>Favorite Book Series</option>
            <option value="Harry Potter">Harry Potter</option>
            <option value="Game of Thrones">Game of Thrones</option>
            <option value="Lord of the Rings/The Hobbit">Lord of the Rings/The Hobbit</option>
            <option value="Percy Jackson">Percy Jackson</option>
            <option value="The Hunger Games">The Hunger Games</option>
            <option value="Diary of a Wimpy Kid">Diary of a Wimpy Kid</option>
            <option value="Goosebumps">Goosebumps</option>
            <option value="The Mortal Instruments">The Mortal Instruments</option>
            <option value="The Chronicles of Narnia">The Chronicles of Narnia</option>
        </select>
        <br />
        <br />
        <input class= "submit" type="submit" name="submit" value="Sign Up">
        </form>
    </td>
</tr>
</table>
</body>
</html>

这是我的注册页面,表单中的信息会添加到数据库中。

<?php 
$con = mysql_connect("localhost","root",""); 
if (!$con) 
  { 
  die('Could not connect: ' . mysql_error()); 
  } 


mysql_select_db("favnet", $con); 

extract($_POST); 

$sql="INSERT INTO membership (FirstName,LastName,Age,Email,Username,Nickname,TV,Movie,Book) VALUES ('$fname','$lname','$age','$email','$username','$nname','$tv','$movie','$book')";

// Execute query 
if(mysql_query($sql,$con) === false) 
{ 
   echo mysql_error() . "<br />$sql"; 
}  

mysql_close($con); 
print "Record Added"; 
?>

我不知道我做错了什么,有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

value分配给option作为

    <select align="right" class="select" name="tv">
        <option value="favourite show">Favorite TV Show</option>
         ...
    </select>

答案 1 :(得分:0)

通过表单提交select(下拉)数据时,所有标签都需要一个值属性,该属性将通过POST发送。像电影一样为电影和电视选项添加值属性。