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

时间:2014-07-08 18:21:57

标签: php html mysql

我在论坛和互联网上搜索了一个解决方案,我找不到一个。我有一个HTML表单,我想提交到已经构建的数据库。这是HTML表单。

<form method="POST" action="form-actions/form-add-center.php" id="add-center">
          <div class="row">
            <div class="large-4 columns">
              <label>Center Name
                <input type="text" id="centerName" name="centerName" />
              </label>
            </div>
            <div class="large-4 columns">
              <label>Center Director
                <input type="text" id="centerDirector" name="centerDirector" />
              </label>
            </div>
            <div class="large-4 columns">
              <label>Center Type</label>
              <select name="centerType">
                  <optgroup id="centerType">
                        <option value="men">Men's Center</option>
                        <option value="men">Women's Center</option>
                        <option value="men">Boy's Home</option>
                        <option value="men">Girl's Home</option>
                        <option value="men">Hope Outreach</option>
                  </optgroup>
                </select>
            </div>
          </div>
          <div class="row">
            <fieldset>
                <legend>Address</legend>
            <div class="large-12 columns">
              <label>Street
                <input type="text" id="street" name="street" />
              </label>
            </div>
            <div class="large-4 columns">
                <label>City
                <input type="text" id="city" name="city" />
              </label>
            </div>
            <div class="large-4 columns">
                <label>State</label>
                <select name="physicalState">
                    <optgroup id="physicalState">
                        <option value=""></option>
                        <option value="AL">Alabama</option>
                    </optgroup>
                </select>
            </div>
            <div class="large-4 columns">
                <label>Zip Code
                    <input type="number" id="zip" name="zip" />
                </label>
            </div>
            </fieldset>
            </div>
                <div class="row">
                    <div class="large-4 columns">
                        <label>Phone Number
                            <input type="text" id="phone" name="phone" />
                    </label>
                </div>
                <div class="large-4 columns">
                    <label>Fax Number
                        <input type="text" id="fax" name="fax" />
                    </label>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="large-12 columns">
                    <input type="submit" class="button" value="Submit">
                    <input type="submit" class="button success" value="Clear Form" onclick="clearForm()">
                </div>
            </div>
        </form>

这是PHP:

<?php
if (isset($_POST['add'])) {

    $submit         = $_POST['Add'];
    $centerName     = $_POST['centerName'];
    $centerType     = $_POST['centerType'];
    $centerDirector = $_POST['centerDirector'];
    $street         = $_POST['street'];
    $city           = $_POST['city'];
    $state          = $_POST['state'];
    $zip            = $_POST['zip'];
    $phone          = $_POST['phone'];
    $fax            = $_POST['fax'];

    $dbhost = 'localhost';
    $dbuser = 'someuser';
    $dbpass = 'somepassword';
    $conn   = mysql_connect($dbhost, $dbuser, $dbpass);
    if (!$conn) {
        die('Could not connect: '.mysql_error());
    }

    $sql = "INSERT INTO     centers (center_name, center_type, director, street, city, state, zip, phone, fax)
                  VALUES('$centerName','$centerType','$centerDirector','$street','$city','$state','$zip','$phone','$fax')";

    mysql_select_db('elp');
    $retval = mysql_query($sql, $conn);
    if (!$retval) {
        die('Could not enter data: '.mysql_error());
    }
    echo "Entered data successfully\n";
    mysql_close($conn);
} else {
    echo "Something went wrong...";
}

?>

我得到了&#34;出了点问题......&#34;当我按照PHP中的else语句提交数据时。你能负担得起的任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

你错过了名字&#34;添加&#34;在提交输入。

 <input type="submit" class="button" name="add" value="Submit">

然后使用

<?php
  if (isset($_POST['add'])) {

$submit   = $_POST['add'];

由于