从搜索MYSQL PHP中分离插入

从搜索MYSQL PHP中分离插入,php,html,mysql,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 no

我在插入和搜索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);

使用以下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
    }

可以使用
oops
,也可以在html中使用字段来区分插入和搜索。您可以使用隐藏字段Welcome to StackOverflow。请出示您的HTML表格。另外,请注意您使用的是
mysql
,它不再被认为是安全的。谢谢。我添加了html表单。
<?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
    }
<form action="search.php" method="post">
<input type="submit" name="search" value="search">
</form>