如何从选择下拉菜单将数据发布到数据库?

时间:2014-01-10 00:05:05

标签: php jquery mysql sql

我正在构建一个注册表单,它会获取大量相关的用户信息,并使用MySQL将其存储在数据库中。我是新手使用php中的下拉菜单(这是我第一次使用它),所以我让它工作,这意味着它正在查询数据库并显示存储在数据库中的大学名称,但是当我点击提交时,它会说“请选择一所大学”。这意味着在我创建的if语句中告诉我即使我选择了一个值,也没有回应我选择了一个值,因为if语句是真的,因为该字段为“空”。有问题的if语句是:

if (empty($_POST['college_id'])) {
    $errors[] = 'Please select a college.';
}

那是什么呢?

<select name="college" size="1" value="<?php if (isset($_POST['college_id'])) echo $_POST['college_id']; ?>">
<option id="0">-- Select your College --</option>
<?php
    require ('../mysqli_connect.php');
    $c = "SELECT college_id, college_name FROM college";
    $getCollege = mysqli_query($dbc, $c);
    while($viewCollege = mysqli_fetch_array($getCollege)) {
?>   

<option id="<?php echo $viewCollege['college_id']; ?>"><?php echo $viewCollege['college_name'] ?></option>

那导致if语句返回空查询?

如果您需要更多详细信息,这是完整的PHP:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>$page_title</title>
    <link rel="stylesheet" href="css/index.css" type="text/css" media="screen" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="SpryAssets_index/SpryMenuBar.js" type="text/javascript"></script>
    <link href="SpryAssets_index/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
    <link type="text/css" rel="stylesheet" href="css/index.css" />
</head>

<body>
    <div class="container"> 
<?php
$page_title = 'Register';
include ('includes/header.html');

// Check for form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

require ('../mysqli_oop_connect.php'); //Connect to the database.

$errors = array(); // Initialize an error array.

// Check for a first name:
if (empty($_POST['user_fname'])) {
    $errors[] = 'You forgot to enter your first name.';
} else {
    $fn = (trim($_POST['user_fname']));
}

// Check for a last name:
if (empty($_POST['user_lname'])) {
    $errors[] = 'You forgot to enter your last name.';
} else {
    $ln = (trim($_POST['user_lname']));
}

// Check for an address:
if (empty($_POST['user_address'])) {
    $errors[] = 'You forgot to enter your address.';
} else {
    $add = (trim($_POST['user_address']));
}

// Check for a city:
if (empty($_POST['user_city'])) {
    $errors[] = 'You forgot to enter your city.';
} else {
    $cit = (trim($_POST['user_city']));
}

// Check for a state:
if (empty($_POST['user_state'])) {
    $errors[] = 'You forgot to enter your state.';
} else {
    $st = (trim($_POST['user_state']));
}

// Check for a zip code:
if (empty($_POST['user_zip'])) {
    $errors[] = 'You forgot to enter your zip code.';
} else {
    $zip = (trim($_POST['user_zip']));
}

// Check for a phone number:
if (empty($_POST['user_phone'])) {
    $errors[] = 'You forgot to enter your phone number.';
} else {
    $ph = (trim($_POST['user_phone']));
}

// Check for an email address:
if (empty($_POST['email'])) {
    $errors[] = 'You forgot to enter your email address.';
} else {
    $e = (trim($_POST['email']));
}

// Check for a password and match against the confirmed password:
if (!empty($_POST['pass1'])) {
    if ($_POST['pass1'] != $_POST['pass2']) {
        $errors[] = 'Your password did not match the confirmed password.';
    } else {
        $p = (trim($_POST['pass1']));
    }
} else {
    $errors[] = 'You forgot to enter your password.';
}

if (empty($_POST['college_id'])) {
    $errors[] = 'Please select a college.';
} else {
    $cid = ($_POST['college_id']);
}

if (empty($errors)) { // Check for all fields completed, for validation.

// Register the user in the database:

//Make the query:
$q = "INSERT INTO users (user_id, user_fname, user_lname, user_address, user_city, user_state, user_zip, user_phone, email, pass, registration_date) VALUES ('NULL', '$fn', '$ln', '$add', '$cit', '$st', '$zip', '$ph' '$e', SHA1('$p'), NOW() )";
$uc = "INSERT INTO user_college (college_id, user_id) VALUES ('$cid', 'NULL')";
//Execute the query:    
$mysqli->query($q, $uc);

if($mysqli->affected_rows == 1) {
    echo '<br /><br /><h1>Thank  you!</h1>
    <p>You are now registered.</p><p><br /></p>';

} else { // If it did not run OK.

// Public message:
echo '<br /><br /><h1>System Error</h1>
<p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';

// Debugging message:
echo '<p>' . $mysqli->error . '<br /><br />Query: ' . $q . '</p>';
}

// Close the database connection.
$mysqli->close();
unset ($mysqli);

// Include the footer and quit the script:
include ('includes/footer.html');
exit();

} else { // Report the errors.

echo '<br /><br /><h1>Error!</h1>
<p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';

} //End of IF statement.
$mysqli->close();
unset ($mysqli);    

} // End of the main Submit conditional
?>

<br /><br />
<form action="register.php" method="post">
<p>First Name: <br /><input type="text" name="user_fname" size="20" maxlength="20" value="<?php if (isset($_POST['user_fname'])) echo $_POST['user_fname']; ?>" /></p>

<p>Last Name: <br /><input type="text" name="user_lname" size="20" maxlength="40" value="<?php if (isset($_POST['user_lname'])) echo $_POST['user_lname']; ?>" /></p>

<p>Address: <br /><input type="text" name="user_address" size="20" maxlength="40" value="<?php if (isset($_POST['user_address'])) echo $_POST['user_address']; ?>" /></p>

<p>City: <br /><input type="text" name="user_city" size="20" maxlength="40" value="<?php if (isset($_POST['user_city'])) echo $_POST['user_city']; ?>" /></p>

<p>State: <br /><input type="text" name="user_state" size="2" maxlength="2" value="<?php if (isset($_POST['user_state'])) echo $_POST['user_state']; ?>" /></p>

<p>Zip: <br /><input type="text" name="user_zip" size="5" maxlength="5" value="<?php if (isset($_POST['user_zip'])) echo $_POST['user_zip']; ?>" /></p>

<p>Phone: <br /><input type="text" name="user_phone" size="10" maxlength="10" value="<?php if (isset($_POST['user_phone'])) echo $_POST['user_phone']; ?>" /></p>

<p>Email Address: <br /><input type="text" name="email" size="20" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>

<p>Password: <br /><input type="password" name="pass1" size="10" maxlength="20" value="<?php if (isset($_POST['pass1'])) echo $_POST['pass1']; ?>" /></p>

<p>Confirm Password: <br /><input type="password" name="pass2" size="10" maxlength="20" value="<?php if (isset($_POST['pass2'])) echo $_POST['pass2']; ?>" /></p>

 <select name="college" size="1" value="<?php if (isset($_POST['college_id'])) echo $_POST['college_id']; ?>">
<option id="0">-- Select your College --</option>
<?php
    require ('../mysqli_connect.php');
    $c = "SELECT college_id, college_name FROM college";
    $getCollege = mysqli_query($dbc, $c);
    while($viewCollege = mysqli_fetch_array($getCollege)) {
?>   

<option id="<?php echo $viewCollege['college_id']; ?>"><?php echo $viewCollege['college_name'] ?></option>
<?php } ?>

</select>

<p><input type="submit" name="submit" value="Register" /></p>

</form>
<?php include ('includes/footer.html'); ?>
    </div>
</body>
</html>

提前感谢您的投入!我是php新手,所以这将是一次学习经历。

1 个答案:

答案 0 :(得分:0)

你的查询有问题试试这个:

$q = "INSERT INTO users (user_id, user_fname, user_lname, user_address, user_city, user_state, user_zip, user_phone, email, pass, registration_date) VALUES ('NULL', '".$fn."', '".$ln."', '".$add."', '".$cit."', '".$st."', '".$zip."', '".$ph."' '".$e."', SHA1('".$p."'), NOW() )";
$uc = "INSERT INTO user_college (college_id, user_id) VALUES ('".$cid."', 'NULL')";