简体   繁体   中英

php-sql code displays all data in the database instead of one

i have this php form that search my database for a particular user using a tracking number that was assigned to each user ( each user different tracking) and the form is suppose to display the particular user that i am searching for but it end up displaying all the database

this is the php code

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "class";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT ctracking, cname, cemail, crport, cdport, clocation, cdestination FROM demo";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "ctracking: " . $row["ctracking"]. " - Name: " . $row["cname"]. " - Email: " . $row["cemail"]. " - Port: " . $row["crport"]. " - Dport: " . $row["cdport"]." - Location: " . $row["clocation"]. " - Destination: " . $row["cdestination"].  "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();

?>

this is the html form code

<html>
<head>
<title> Static Page</title>
</head>

<body>
<form action="making.php" method="post" />
 <h1>Britchi Tracking</h1>
    <p>
    Costumers Name (required) <br/>
    <input type="text" name="search" placeholder="Emmanuel John"' . '" size="70"/>
    </p>
    <p><input type="submit" name="csearch" value="Search"></p>
    </form>

<form action="http://localhost/wordpress">
<input type="submit" value="Go to Home Page">
</form
</body>

</html>

i want the code to display all the details of a particular user not all the database

You need to receive variable

$track_num=$_POST['search']; 

And apply in your query .

$sql = "SELECT ctracking, cname, cemail, crport, cdport, clocation, cdestination FROM demo WHERE ctracking='$track_num'";

NOTE:your code is open to sql injection attack use prepared statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM