如何链接MySQL中的表

时间:2014-07-22 15:45:19

标签: php mysql

我为phpMyAdmin创建了2个表。其中一个是国家,另一个是用户。

国家/地区表:

Countries Table

用户表:

Users table

我知道如何使用HTML和PHP创建表单。我希望我的用户选择一个国家/地区,但这些国家/地区位于不同的表格中,无法放入用户表格中。我是否需要使用phpMyAdmin上的“SQL”部分将其链接起来,还是有PHP代码?

我还没有完全构建php表单(刚开始!)

<h1>Register</h1>
<form action="" method="POST">
  <p>
    <label>UserName : </label>
    <input id="username" type="text" name="usernamet" placeholder="Username" />
  </p>
  <input id="teamname" type="text" name="username" placeholder="Team Name" />
  </p>
  <select name="countries">
    <option value="England">Volvo</option>
    <option value="Spain">Saab</option>
    <option value="Turkey">Fiat</option>
    <option value="France">Audi</option>
  </select>
  <p>
    <label>E-Mail&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</label>
    <input id="password" type="email" name="email" />
  </p>
  <p>
    <label>Password&nbsp;&nbsp; : </label>
    <input id="password" type="password" name="password" placeholder="password" />
  </p>
  <a class="btn" href="login.php">Login</a>
  <input class="btn register" type="submit" name="submit" value="Register" />
</form>

1 个答案:

答案 0 :(得分:0)

好的......这是我的答案。

正如我所说,如果你打算要求一个用户所在国家/地区,你必须有一个地方将它存储在同一个表中,但是如果你计划将它存储在另一个表中,那也没关系,但你需要将我的单个查询插入更改为两个查询插入。一个用于输入用户信息的表,另一个用于输入表,其中包含用户表(或id列)中的用户ID#以及表单中的值。< / p>

原始表:

Original Table from Question Provided

我正在使用的新表格: Modified Table to Answer Question

此表单还包括错误处理选项,如空白表单。如果您想验证电子邮件地址等,请搜索可以执行此操作的函数,并在下面的验证部分中抛出处理程序。此代码假定您希望表单在其自身上进行处理,即表单和处理表单的代码驻留在同一脚本页面上。如果您打算执行其他操作,请修改<form action=并从中删除<?php ... ?>编码。如果要删除任何此类内容,请删除与之相关的代码行。

另请注意,此脚本使用mysqli_函数而非mysql_函数作为mysql_,并且相关函数被视为已弃用,并且最终将从PHP 5.4中删除PHP(我相信。)

如果您不想要确认页面,可以编辑脚本以便为您执行此操作。如果您还有其他需要,请编辑原始问题。

通过执行SELECT *查询然后继续循环查询该查询的结果并像我一样将它们添加到数组中,解决了遍历国家/地区列表的问题。在这种情况下,我使用while()循环遍历从mysqli_query()函数返回的数组的结果,并将格式化的结果添加到最终将输出到表单的数组中。

<?php

//Define MySQLi connection information
    $db = mysqli_connect("localhost", "stackoverflow", "", "stackoverflow");
    // mysqli_connect(SERVER_ADDR, DB_USER, DB_PASSWORD, DB_NAME)

    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }

if(isset($_POST) && sizeof($_POST) > 0 ) {
    // If there is something to post, start this branch

    $username = mysqli_real_escape_string($db, $_POST['username']);
    $teamname = mysqli_real_escape_string($db, $_POST['teamname']);
    $usercountry = mysqli_real_escape_string($db, $_POST['countries']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $password = mysqli_real_escape_string($db, $_POST['password']);

    // Check to make sure we have a valid form. 
    // If you need more, follow the same pattern.

    if (empty($username) || empty($_POST['username'])) $error[] = "Username cannot be empty.";

    if (empty($email) || empty($_POST['email'])) $error[] = "Email Address cannot be empty.";

    if (empty($password) || empty($_POST['password'])) $error[] = "Password cannot be blank.";

    if (empty($usercountry) || empty($_POST['countries'])) $error[] = "Please select a country from the drop down list.";

    if (!isset($error)) {
    // No errors? No problem. Insert the form data into the database.
    $user_insert = "INSERT INTO users (username, teamname, country, email, password, active, rank) VALUES ('" . $username . "' , '" .  $teamname . "', '" . $usercountry ."', '" . $email ."', PASSWORD('" . $password . "'), 1, 1)";

    mysqli_query($db, $user_insert);
    }

} ?>
<?php if (isset($error) || empty($_POST)) {

    // There was noting to post, so show the form to collect the information.

    // Retrieve the values from the countries table.

    $countries = mysqli_query($db, "SELECT * FROM countries ORDER BY countryName");

    $country_options = array();
    $country_options[] = array('value' => '', 'text' => '');
    while($row= mysqli_fetch_array($countries)) {
        $country_options[] = array('value' => $row['idCountry'], 'text' => $row['countryName']);
    }

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>User Registration Form</title>
<!-- include your <style> CSS or imports here -->
<style>
.leftCell {
    width: 160px;
}
.error {
    border: 1px solid #FF0000;
    background-color: #FFCCCC;
    color: #FF0000;
    padding: 10px;
</style>
</head>
<body>
<?php if (isset($error)) { ?>
<div class="error">
<b>Please fix the following errors:</b>
<ul>
<?php foreach($error as $error_text) {
    echo "<li>" . $error_text . "</li>";
}
?>
</ul>
</div>
<?php } ?>
<!-- assuming you do not already have a script to input the data, I'm using this page to input the data -->
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">

  <!-- Let's create a table -->
  <h1>Register</h1>
  <table>
    <tr>
      <td class="leftCell">Desired User Name:</td>
      <td><input id="username" type="text" name="username" placeholder="Username" value="<?php echo (isset($username) ? $username : ''); ?>"/></td>
    </tr>
    <tr>
      <td class="leftCell">Team Name:</td>
      <td><input id="teamname" type="text" name="teamname" placeholder="Team Name" value="<?php echo (isset($teamname) ? $teamname : ''); ?>"/></td>
    </tr>
    <tr>
      <td style="width: 50px;">User Country:</td>
      <td><select name="countries">
          <?php
        foreach($country_options as $country) {
            echo '<option value="' . $country['value'] . '" ' . ($country['value'] == $usercountry ? 'selected="selected"' : '') . '>' . $country['text'] . "</option>";
        }
        ?>
        </select></td>
    </tr>
    <tr>
      <td class="leftCell">E-Mail:</td>
      <td><input id="email" type="text" name="email" placeholder="E-Mail" /></td>
    </tr>
    <tr>
      <td class="leftCell">Password:</td>
      <td><input id="password" type="password" name="password" placeholder="Password" /></td>
    </tr>
  </table>
  <a class="btn" href="login.php">Login</a><br />
  <input class="btn register" type="submit" name="submit" value="Register" />
</form>
</body>
</html>
<?php } else { ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>User Registration - Success</title>
</head>

<body>

<h1>The user was successfully registered!</h1>
</body>
</html>
<?php } ?>