使用PHP将表单提交到MySQL数据库

时间:2015-11-03 18:37:56

标签: php html mysql forms phpmyadmin

我正在尝试将表单信息从HTML表单提交到mySQL数据库。我已经在这里找到了各种各样的帖子,但无法找到解决问题的紧密解决方案。

我的HTML表单代码如下:

<form action="ProfileInfo.php" method=post>
    <h3 class="FormHeader">First Name: 
        <input type="text" name="name_first"><br>
    Last Name: 
        <input type="text" name="name_last"><br>
    Email: 
        <input type="email" name="email"><br>
    Gender: 
        <input type="radio" name="gender" value="Male">Male
        <input type="radio" name="gender" value="Female">Female<br>
    Dominant Hand: 
        <input type="radio" name="dominant_hand" value="Right">Right
        <input type="radio" name="dominant_hand" value="Left">Left
        <input type="radio" name="dominant_hand" value="Ambidextrous">Ambidextrous<br>
    Birthday: 
        <input type="date" name="dob"><br>
    What's your best sport other than golf?
        <select name="best_sport">
            <option value="No Other Sports">I don't play other sports.</option>
            <option value="Baseball">Baseball</option>
            <option value="Basketball">Basketball</option>
            <option value="Football">Football</option>
            <option value="Soccer">Soccer</option>
            <option value="Hockey">Hockey</option>
            <option value="Tennis">Tennis</option>
            <option value="Lacrosse">Lacrosse</option>
            <option value="Volleyball">Volleyball</option>
            <option value="Cricket">Cricket</option>
            <option value="Track and Field">Track and Field</option>
            <option value="Water Sports">Water Sports</option>
            <option value="Snow Sports">Snow Sports</option>
            <option value="Dance">Dance</option>
            <option value="Auto Racing">Auto Racing</option>
            <option value="Other Sport">Other Sport</option>
        </select><br>
    How many years have you played golf?
        <input type="number" name="golf_duration" min="0" max="100"><br>
    What's your average golf score?
        <input type="number" name="golf_score" min="0" max="200"><br>
    Weather permitting, how many times a month do you play golf?
        <input type="number" name="golf_play" min="0" max="35"><br>
    Weather permitting, how many times a month do you practice golf?
        <input type="number" name="golf_practice" min="0" max="35"><br>
    What is your handicap or current index?
        <input type="number" name="golf_handicap" min="0" max="41"><br></h3>
   <input type="submit" value="Go">
</form>

这是我的php:

<html>

   <head>
      <title>Profile Info</title>
   </head>

   <body>
      <?php
         if(isset($_POST['add']))
         {
            $dbhost = 'localhost';
            $dbuser = '*********';
            $dbpass = '*********';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);

            if(! $conn )
            {
               die('Could not connect: ' . mysql_error());
            }

            $name_first = $_POST['name_first'];
            $name_last = $_POST['name_last'];
            $email = $_POST['email'];
            $gender = $_POST['gender'];
            $dominant_hand = $_POST['dominant_hand'];
            $dob = $_POST['dob'];
            $best_sport = $_POST['best_sport'];
            $golf_duration = $_POST['golf_duration'];
            $golf_score = $_POST['golf_score'];
            $golf_play = $_POST['golf_play'];
            $golf_practice = $_POST['golf_practice'];
            $golf_handicap = $_POST['golf_handicap'];

            $sql = "INSERT INTO Profile ". "(name_first, name_last, email, gender, dominant_hand, dob, best_sport, golf_duration, golf_score, golf_play, golf_practice, golf_handicap) ". "VALUES('$name_first','$name_last','$email', '$gender', 'dominant_hand', 'dob', 'best_sport', 'golf_duration', 'golf_score', 'golf_play', 'golf_practice', 'golf_handicap')";

            mysql_select_db('test_db');
            $retval = mysql_query( $sql, $conn );

            if(! $retval )
            {
               die('Could not enter data: ' . mysql_error());
            }

            echo "Entered data successfully\n";

            mysql_close($conn);
         }
    ?>

</body>
</html>

任何帮助将不胜感激!如果有帮助,我使用phpMyAdmin来管理我的数据库...

0 个答案:

没有答案