从搜索MYSQL PHP中分离插入

时间:2017-03-17 09:42:32

标签: php html mysql

我在插入和搜索MYSQL数据库时遇到了麻烦。如果我插入数据,它会同时添加和搜索,反之亦然。我想一次只执行一次。我把它们放在html表单上的单独按钮上。有什么想法吗?

<?php

$host = "localhost";
$user = "root";
$password = "pass";
$dbname = "server";

$link = mysql_connect(localhost, root, pass, server);

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

$db_selected = mysql_select_db(server, $link);

if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());

 }


 $sql = "INSERT INTO nameOne" . "(firstName, lastName, phone,           address, city, state, zip, birthdate, userName, sex, relationship)".  
    "VALUES ('$firstName', '$lastName', '$phone', '$address', '$city', '$state', '$zip', '$birthdate', '$userName', '$sex', '$relationship')";

 mysql_select_db('server');
 $retval = mysql_query( $sql, $link );

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

      echo "Entered data successfully\n";



   $query = sprintf("SELECT firstName, lastName, phone, address,    city, state, zip, birthdate, userName, sex, relationship FROM nameOne
WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string($firstName),
mysql_real_escape_string($lastName));

  $result = mysql_query($query);

   if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
 }
 while ($row = mysql_fetch_assoc($result)) {
echo $row['firstName'];
echo $row['lastName'];
echo $row['phone'];
echo $row['address'];
echo $row['city'];
echo $row['state'];
echo $row['zip'];
echo $row['birthdate'];
echo $row['userName'];
echo $row['sex'];
echo $row['relationship'];
  }


  mysql_close($link);

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <form action="results.php" method="post">
        <table>
            <tr>
                <td>Enter Name</td>
                <td><input type="text" name="firstName"> </td>
            </tr>
            <tr>    
                <td>Enter Last Name </td>
                <td><input type="text" name="lastName">
            </tr>
            <tr>
                <td>Phone Number</td>
                <td><input type="text" name="phone"> </td>
            </tr>
            <tr>
                <td>Address</td>
                <td><input type="text" name="address"> </td>
            </tr>
            <tr>
                <td>City</td>
                <td><input type="text" name="city"> </td>
            </tr>
            <tr>
                <td>State</td>
                <td class="selection">
                    <select name="state">
                        <option value="AL">Alabama</option>
                        <option value="AK">Alaska</option>
                        <option value="AZ">Arizona</option>
                        <option value="AR">Arkansas</option>
                        <option value="CA">California</option>
                        <option value="CO">Colorado</option>
                        <option value="CT">Connecticut</option>
                        <option value="DE">Delaware</option>
                        <option value="DC">District of Columbia</option>
                        <option value="FL">Florida</option>
                        <option value="GA">Georgia</option>
                        <option value="HI">Hawaii</option>
                        <option value="ID">Idaho</option>
                        <option value="IL">Illinois</option>
                        <option value="IN">Indiana</option>
                        <option value="IA">Iowa</option>
                        <option value="KS">Kansas</option>
                        <option value="KY">Kentucky</option>
                        <option value="LA">Louisiana</option>
                        <option value="ME">Maine</option>
                        <option value="MD">Maryland</option>
                        <option value="MA">Massachusetts</option>
                        <option value="MI">Michigan</option>
                        <option value="MN">Minnesota</option>
                        <option value="MS">Mississippi</option>
                        <option value="MO">Missouri</option>
                        <option value="MT">Montana</option>
                        <option value="NE">Nebraska</option>
                        <option value="NV">Nevada</option>
                        <option value="NH">New Hampshire</option>
                        <option value="NJ">New Jersey</option>
                        <option value="NM">New Mexico</option>
                        <option value="NY">New York</option>
                        <option value="NC">North Carolina</option>
                        <option value="ND">North Dakota</option>
                        <option value="OH">Ohio</option>
                        <option value="OK">Oklahoma</option>
                        <option value="OR">Oregon</option>
                        <option value="PA">Pennsylvania</option>
                        <option value="RI">Rhode Island</option>
                        <option value="SC">South Carolina</option>
                        <option value="SD">South Dakota</option>
                        <option value="TN">Tennessee</option>
                        <option value="TX">Texas</option>
                        <option value="UT">Utah</option>
                        <option value="VT">Vermont</option>
                        <option value="VA">Virginia</option>
                        <option value="WA">Washington</option>
                        <option value="WV">West Virginia</option>
                        <option value="WI">Wisconsin</option>
                        <option value="WY">Wyoming</option>
                    </select>
                </td>
            <tr>
                <td>Zip </td>
                <td><input type="text" name="zip"> </td>
            </tr>
            <tr>
                <td>Birthdate</td>
                <td><input type="text" name="birthdate"> </td>
            </tr>
            <tr>
                <td>Username</td>
                <td><input type="text" name="userName"> </td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="text" name="password"> </td>
            </tr>
            <tr>
                <td>Sex</td>
                <td><input name="sex" type="radio" checked="checked" value="Male">Male <input name="sex" type="radio" id="mypassword" value="Female">Female
                </td>

            </tr>
            <tr>
                <td>Relationship</td>
                <td><input type="text" name="relationship"> </td>
            </tr>

        </table>
        <input type="submit" name="submit" value="insert">
        <input type="submit" name="update" value="update">
        <input type="submit" name="search" value="search">
    </form>


</body>

1 个答案:

答案 0 :(得分:1)

使用以下PHP代码结构:

<?php
    // here your database connection
    if (isset($_POST['submit'])) {
        // your insert code here
    }
    if (isset($_POST['update'])) {
        // your update code here
    }
    if (isset($_POST['search'])) {
        // your search code here
    }

首先,我建议您使用 mysqli ,然后从插入和更新表单中创建单独的搜索表单。像:

<form action="search.php" method="post">
<input type="submit" name="search" value="search">
</form>

然后在search.php中编写PHP搜索代码并显示。