搜索用户特定信息

时间:2014-04-21 10:49:52

标签: php search

PHP新手,请具体说明。 用户登录后,他们会转到表单提交数据。 我希望用户能够搜索过去一个月中输入的所有数据。他们可以根据需要编辑或删除。 请使用什么代码进行此搜索。以下是我所拥有的(搜索根本不工作)

    <h1>Welcome!</h1>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
Search: <input type="text" name="search">
<input type="submit" value="submit">
    </form> 


    <?php

// Check if the form value
// was submitted, if not
// assign a default value
if(isset($_GET['search'])){
    $s = $_GET['search'];
}else{
    $s = "%";
}


//echo $s;

// Connects to the database
mysql_connect("localhost","sample","qwerty") or die("Error " . mysqli_error($link));

// Selects which of the databases to use
mysql_select_db('crm');

// Create and runs the query
$sql = "SELECT * FROM hours WHERE employee_id LIKE 'RHilfi'";
//echo $sql;

$results = mysql_query($sql);



echo "<br /><br />";
// Loops though all of the results
while($results_array = mysql_fetch_array($results)){

    /*
    echo "<pre>";
    print_r($results_array);
    echo "</pre>";
    */
    //print_r($results_array);
    echo " Employee ID: " . $results_array['employee_id'] . " , Date: " .      $results_array['date'] . " , Rate of Pay: " . $results_array['rate_of_pay'] . " , Hours: " . $results_array['hours'] ." , Amount Due: " . $results_array['amount_due']. " [ <a href=\"edit.php?id=" . $results_array['hours'] . "\">EDIT</a>]";

    /*
    // USE THIS ONE
    echo $results_array['title'] . " by " . $results_array['director'] . " [ <a href=\"edit.php?id=" . $results_array['hours'] . "\">EDIT</a>]";
    */


    echo "<br />";

}
    ?>

    <h1>Submit New Claim</h1>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
Employee ID: <input type="text" name="employee_id">
<br />
Date (yyyy/mm/dd): <input type="text" name="date">
<br />
Rate of Pay: <input type="text" name="rate_of_pay">
<br />
    Hours: <input type="text" name="hours">
<br />
    Amount Due: <input type="text" name="amount_due">
<br />
<input type="submit" value="Submit">
    </form> 



    <?php


if(isset($_GET['employee_id'])){
    $employee_id = $_GET['employee_id'];
    $date = $_GET['date'];
    $rate_of_pay = $_GET['rate_of_pay'];
    $hours = $_GET['hours'];
    $amount_due = $_GET['amount_due'];
}else{
    $employee_id = "";
    $date = "";
    $rate_of_pay = "";
    $hours = "";
    $amount_due = "";
}


if(isset($_GET['employee_id'])){
    //echo "INSERT NEW RECORD";
    // INSERT NEW RECORD

    // Connects to the database
    mysql_connect("localhost","sample","qwerty") or die("Error " .  mysqli_error($link));
    // Selects which of the databases to use
    mysql_select_db('crm');

    // Create and runs the query
    // INSERT INTO `sample`.`dvd` (`dvd_id`, `title`, `year`, `director`) VALUES  (NULL, 'title', '1234', 'director'); 
    //$sql = "INSERT INTO crm . 'hours' (employee_id, date, rate_of_pay, hours, amount_due) VALUES (NULL, '$employee_id', '$date', '$rate_of_pay', '$hours', $amount_due)";
    $sql = "INSERT INTO hours (ID, employee_id, date, rate_of_pay, hours, amount_due) VALUES (NULL, '$employee_id', '$date', '$rate_of_pay', '$hours', $amount_due)";
    //echo $sql;
    $results = mysql_query($sql);

    echo "<br /><br />";


    // OPTIONAL
    if(mysql_affected_rows() >= 1){
        echo "<h3>Thank you! You have successfully submitted a claim </h3>";
    }

}

    ?>



<a href="login.php">Log Out</a>

1 个答案:

答案 0 :(得分:0)

请使用mysqli,如果您在MySQL中使用LIKE,请使用以下内容:

$sql = "SELECT * FROM hours WHERE employee_id LIKE '%RHilfi%'";