Using a form to conduct a database search - php

I'm trying to use a simple form to run a database search. I'm not getting a database connection error so I'm pretty certain that's not the problem. I checked my target page for the echo display, but I'm not receiving any out put.
<!DOCTYPE HTML>
<html lang = "en">
<head>
<title>Tech Order Department.html</title>
<meta charset = "UTF-8" />
</head>
<body>
<h3>Search Contacts Details</h3>
<p>You may search either by first or last name</p>
<form method="post" action="Search.php" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
//connect to the database
$servername = "localhost";
$username = "XXXXXXXXX"; // removed
$password = "XXXXXXXXX"; // removed
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//-select the database to use
$mydb=mysql_select_db("oldga740_SeniorProject");
//-query the database table
$sql="SELECT ID, FirstName, LastName FROM Staff WHERE FirstName LIKE '%" . $name . "%' OR LastName LIKE '%" . $name ."%'";
//-run the query against the mysql query function
$result=mysql_query($sql);
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$FirstName =$row['FirstName'];
$LastName=$row['LastName'];
$ID=$row['ID'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "" .$FirstName . " " . $LastName . "</li>\n";
echo "</ul>";
}
}
else{
echo "<p>Please enter a search query</p>";
}
}
}
?>
</body>
</html>

Related

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.

Wordpress PHP FIle not running

I have a custom search form on my website with this code:
<center>
<form action="orderstatus.php" method="POST">
<input type="text" name="OrderLineNumber" value="">
<br>
<input type="submit" value="Submit">
</form>
</center>
And although I call orderstatus.php to action . When I press the submit button I get this Url : blablabla.de/action_page.php?OrderLineNumber=+23563623
This means instead of running orderstatus.php it's running action_page.php
My orderstatus.php is trying to print One Select Row from database like this :
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "*******";
$username = "*******";
$password = "*******";
$dbname = "********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM OrderStatus";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc() & fetch_assoc() = $_POST["name"] ) {
echo "<br> Order Line Number: ". $row["OrderLineNumber"]. " - Date Started: ". $row["Date Started"]. " " . $row["Status"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

Display data from database in HTML page [duplicate]

This question already has an answer here:
Show data pulled from database, based on html form input and display in html page
(1 answer)
Closed 6 years ago.
I would like to display data from my database on page load, but I don't know how and I didn't found any functional way. Inserting works fine.
Here is my HTML code for data inserting:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
</head>
<body>
<form action="insert.php" method="post">
<p>
<label for="snapname">Name:</label>
<input type="text" name="snapname">
</p>
<p>
<label for="age">Age:</label>
<input type="text" name="age">
</p>
<input type="submit" value="odeslat">
</form>
</body>
</html>
PHP for connect and insert data to database:
<?php
$servername = "localhost";
$username = "admin";
$password = "***";
$dbname = "db1";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Could not connect to server: " . $conn->connect_error);
}
$first_name = mysqli_real_escape_string($conn, $_POST['snapname']);
$last_name = mysqli_real_escape_string($conn, $_POST['age']);
$Jmeno = $_POST['snapname'];
$Vek = $_POST['age'];
$sql = "INSERT INTO snapy (ID, username, age, date)
VALUES (0, '$Jmeno', '$Vek', CURRENT_TIMESTAMP)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Can someone help me with displaying data in HTML file?
You need to run a SELECT-request. Since you are using MySQLi you want to use something like:
$sql = "SELECT * FROM snapy";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["username"]. " " . $row["age"]. " yo<br>";
}
} else {
echo "0 results";
}
Found here: http://www.w3schools.com/php/php_mysql_select.asp

Why this code inserts blank?

I´m trying to create a form connected to a database but when I fill out the form and I refer to the table in phpMyAdmin I see that it have entered a blank record instead of form data. I´m using PhpStorm.
I think all this code is correct...
That is the form of the .html:
<form id="form1" name="form1" method="post" action="index.php">
<label for="userSignUp">Email</label>
<input type="text" name="userSign" id="userSignUp" />
<label for="passwordSignUp">Password</label>
<input type="password" name="passwordSign" id="passwordSignUp" />
<input type="submit" name="Submit" id="Submit" value="Submit" />
</form>
I have the following .php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$db_selected = mysqli_select_db($conn, $dbname);
$userSignUp = ""; // If I substitute "" with characters at this time the table is well updated
$passwordSignUp = ""; // Same as before
if(isset($_POST['userSign'])){
$userSignUp = $_POST['userSign'];
}
if (isset($_POST['passwordSign'])) {
$passwordSignUp = $_POST['passwordSign'];
}
$sql = "INSERT INTO test.person (FirstName, Password) VALUES ('$userSignUp', '$passwordSignUp')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();

Exporting php in a same file as html to use css (PHP issue!)

my name is Luis, well Im trying to make a file in html, css and php which gets information from the database and outputs it. But it has to be in the same page as the html so I can edit with CSS... thank you all!
<html>
<head>
</head>
<body>
<script>
</script>
<form action="<?php include 'file.php';?>" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>
File in PHP:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "test";
$namer = $_POST['prestador'];
$cities = $_POST['cidade'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,address,city FROM tb_prestadores WHERE name = '$namer' AND city = '$cities'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row['name']. " Endereço - : " . $row['address']." Cidade :".$row['city']."<br>";
}
} else {
echo "It is going to else";
}
$conn->close();
?>
You are including / inserting the file wrongly.
What you should do:
1) Copy your html code and paste it at the bottom of your php code i.e. after the ending '?>' tag. Your code should look like this:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "test";
$namer = $_POST['prestador'];
$cities = $_POST['cidade'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,address,city FROM tb_prestadores WHERE name = '$namer' AND city = '$cities'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row['name']. " Endereço - : " . $row['address']." Cidade :".$row['city']."<br>";
}
} else {
echo "It is going to else";
}
$conn->close();
?>
<html>
<head>
<!-- include your css here-->
</head>
<body>
<script>
</script>
<form action="" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>
Btw, this is not the 'optimal' way to code applications. Please look into Smarty or some other templating language as well as MVC once you get the hang of php and html basics. Good luck!
To begin with, your <?php include 'file.php';?> is miss-placed and should not be in the action tag of your form rather it should be right before it, like so:
<html>
<head></head>
<body>
<?php include 'file.php';?>
<form action="" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>

Categories