PHP Simple Search Form - php

I'm new to PHP and would like to start a simple query system by just searching identification number and it will print out full name, date of birth, gender and identification number.
The first file name of the page is search.php.
Inside of the file contains
<html>
<body>
<form action="result.php" method="get">
ID Number: <input type="text" name="idnumber"> <input type="submit" value="Search"><br>
</form>
<p>Input ID number</p>
</body>
</html>
After that, I created a database named users and with following columns (id, full_name, date_of_birth, gender, identification_number)
What code should I write into result.php to let the search result of identification number match and found it will print all the details of the person and the search keyword must 100% match.
Example of the scenario.
In database has John Doe, 20 September 1990, Male, 900920A44.
Search keyword "900920A44" will print out corresponding user's information but if search keyword "900920A" will not print out anything because other user might had a "900920A55" identification number.
Files:
search.php & result.php
Please advise.

try this
FORM
<html>
<body>
<form action="result.php" method="get">
ID Number: <input type="text" name="idnumber"> <input type="submit" name="form_submit" value="Search"><br>
//PUT THE NAME FOR SUBMIT BUTTON
</form>
<p>Input ID number</p>
</body>
</html>
result.php
//database connection
global $conn;
$servername = "localhost"; //host name
$username = "username"; //username
$password = "password"; //password
$mysql_database = "dbname"; //database name
//mysqli prepared statement
$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());
mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");
if(isset($_GET['form_submit']))
{
$IDNUMBER =$_GET['idnumber'];
$stmt = $conn->prepare("select * from your_table_name_here where identification_number=? ");
$stmt->bind_param('s',$IDNUMBER);
$stmt->execute();
$val = $stmt->get_result();
$row_count= $val->num_rows;
if($row_count>0)
{
$result =$val->fetch_assoc();
print_r($result);
}
else
{
echo "identification_number not Match";
}
$stmt->close();
$conn->close();
}

Use this.
login.php
<html>
<body>
<p>Input ID number</p>
<form action="result.php" method="get">
ID Number: <input type="text" name="idnumber"> <input type="submit" value="Search"><br>
</form>
</body>
</html>
result.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "users";
mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database) or die("Could not select database");
$idmark = $_GET['idnumber'];
$sql = "select * from student where identification_number = '".$idmark."'";
$result = mysql_query($sql);?>
<table width="70%" border="1px solid black">
<tr style="background-color:green; color:white">
<th><strong>ID</strong></th>
<th><strong>Name</strong></th>
<th><strong>DOB</strong></th>
<th><strong>GENDER</strong></th>
<th><strong>Identification No</strong></th>
</tr>
<?php if (mysql_num_rows($result)>0) { $i=0;
while($row = mysql_fetch_assoc($result)) { ?>
<tr class="<?php echo ($i%2) ? 'even' : 'odd' ?>">
<td><?php echo $row['id'];?></td>
<td><?php echo $row['full_name'];?></td>
<td><?php echo $row['date_of_birth'];?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['identification_number'];?></td>
</tr>
<?php $i++;}}
?>

Related

Why will my input form not find a matching user to my search inside of my table?

I am trying to create a PHP search that looks through my table (users) and finds the user that matches the name they searched for and displays it on the screen. But the program won't display the user I searched up, and I don't know why. The variables all check out, and I didn't misspell anything in the code or table. My ifelse statement tells me that there is no query result, even though the user in the table and the user I searched are identical. I am using PHPMyAdmin to manage the tables and see changes (if there are any) to the table. The result I wanted was for the program to display the user and email on the page. I can't find a solution, so if you can please tell me!
addnone.php
<?php
include_once 'includes/db_connect.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>SCIENCE FAIR</title>
<link rel="stylesheet" href="style.css">
<section class="container grey-text">
<form class="white" action="addnone.php" method="POST">
<tr>
<label>First Name:</label>
<td><input type="text" name="firstname" placeholder="First Name"></td></br>
</tr>
<div class="center">
<td colspan="2"><input type="submit" name="submit" value="Search"></td>
</div>
</form>
<div class="box">
<?php
if (isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$sql = "SELECT * FROM users WHERE name = '%$firstname%'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if ($queryResult > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<div>
<p>".$row['name']."<p>
<p>".$row['email']."<p>
</div>";
}
} else {
echo "No users with name $firstname!";
}
}
?>
</div>
</section>
</html>
db_connect.php
<?php
$dbServername = "localhost";
$dbUsername = "scifair";
$dbPassword = "password";
$dbName = "scifair";
// connect to database
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
// check connection
if(!$conn){
echo 'Connection error: ' . mysqli_connect_error();
}
?>
Use "LIKE" Operator
$sql = "SELECT * FROM users WHERE name LIKE '%$firstname%'";

How make query and show data

I try to do input number form and make search in page grapht.php. For example I write code:
<form action="grapht.php" method="post">
Name: <input type="text" name="number" autocomplete="off"><br>
<input type="submit">
</td>
</form>
<? $number= $_POST["number"]; ?>
And then make query my MySQL table:
$shipmin = 1;
$shipmax=$number;
$uzklausimas ="SELECT * FROM MyGuests WHERE id >= '$shipmin' AND id<= '$shipmax'";
$minmax=mysqli_query($conn,$uzklausimas);
while($ru=mysqli_fetch_assoc($minmax)){
echo "$ru[id] <br>";
}
But my code doesn't work. Maybe somebody could give me advice how need solve this my problem?
Do you initialize your db connection ($conn) before this line ?
$minmax=mysqli_query($conn,$uzklausimas);
if I write line $shipmax=45; it is working well, but I have static date from 1 to 45. If I write $shipmax=$number; It is not work.
all my code
<?php
$servername = "localhost";
$username = "xxxxx";
$password = "xxxx";
$dbname = "xxxxx";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$shipmin = 1;
$shipmax=$number;
$uzklausimas ="SELECT * FROM MyGuests WHERE id >= '$shipmin' AND id<='$number'";
$minmax=mysqli_query($conn,$uzklausimas);
while($ru=mysqli_fetch_assoc($minmax)){
echo "$ru[id] <br>";
}
mysqli_close($conn);
?>
<form action="forum.php" method="post">
Name: <input type="text" name="name" autocomplete="off"><br>
<input type="submit">
</td></form>
<? $number= (int)$_POST["name"]; ?>
<?php echo $number; ?>
<br>
Page where you could see it is http://ortex.lt/forum.php

how to return values from mysql table with ajax and php

I am new to programing. I have one form to create Groups. It has two text fields Code Id and Code description. After submitting it showed me that the Code Id which i entered is already exist and if not it add one record in MySQL table. What I want that when I leave the Id text field at the same time with onchange event and Ajax to search table and alert if the Id already exit and at the same time fill name text box with description of that Code Id. How to do that? My code is
HTML file
...
</style>
<body>
<H1>Create Grup</h1>
<br>
<form action="creategrup.php" method="post">
<p>
<label for="codigo">Grup Id:</label>
<input type="text" required="required" autofocus="autofocus"
maxlength="4" name="codigo" id="codigo">
</p>
<p>
<label for="nombre">Grupo description:</label>
<input type="text" required="required" name="nombre" id="nombre"">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>
And PHP is
<?php
include 'connectdb.php';
$nombre=$_POST['nombre'];
$codigo=$_POST['codigo'];
$sql = "select codigogrupo,nombregrupo from grupo where
codigogrupo='$codigo'";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) >0) {
echo "<p><h1><b>Grup Id $codigo allready exist....</h1></b></p><br>";
echo "<a href='creategrup.html'>Go Back</a>";
}
else {
mysqli_query($conn, "insert into grupo(codigogrupo,nombregrupo)
values('$codigo','$nombre')");
if(mysqli_affected_rows($conn)>0){
echo "<p><h1><b>Grup $nombre added</h1></b></p>";
echo "<a href='creategrup.html'>Go Back</a>";
} else {
echo "Grup not added<br>";
echo mysqli_error ($conn);
}
}
?>
Connectdb.php
<?php
$servername = "server name";
$username = "user name";
$password = "password";
$dbname = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// echo "Connected successfully";
?>
Please help me.
You are getting already exist message so that may be confirm that there is another data with same id. For further assistance please knock me

WordPress - Select query on specific page

I want to select query in specific page. I created table in database:
First, I added phpexec on plugin for using PHP on page. Then, I tested Select query and it was ok. Finally, I want to create form for checking Serial Number. Here is my code:
<html>
<head>
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"><br><br>
Serial Number: <input type="Text" Name="Num1"><p>
<input type="Submit" value="Calculate">
</form>
<phpcode>
<?php
if (count($_POST) > 0 && isset($_POST["Num1"])
{
$servername = "localhost";
$username = "******";
$password = "*******";
$dbname = "******";
$serialNum = $_POST["Num1"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Install FROM SN WHERE serial = $serialNum";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["Install"];
}
} else {
echo "no result";
}
$conn->close();
}
?>
</phpcode>
</body>
</html>
Actual output is:
How to solve this issue?
Your form is broken... Try it this way
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>Serial Number: <input type="Text" Name="Num1" value=""></p>
<input type="Submit" value="Calculate">
</form>
hi maybe you can try with this
is a little example and check your values and id, names for values
and sintaxis for html and php
<html>
<head>
<title></title>
</head>
<body>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<br><br>
Serial Number: <input type="text" name="Num1" id="Num1">
<br><br>
<input type="submit" name="submit" id="submit" value="Calculate">
<br><br>
</form>
<phpcode>
<?php
if(isset($_POST["submit"]))
{
function Conect()
{
if (!($link=mysql_connect("localhost","username","password")))
{
echo "error to conect to database.";
exit();
}
if (!mysql_select_db("databasename",$link))
{
echo "Error to select database.";
exit();
}
return $link;
}//end function Conect
$serialNum = mysql_real_escape_string($_POST["Num1"]);
$query="SELECT Install FROM SN WHERE serial = $serialNum";
$action=mysql_query($query,$link) or die("Error: ".mysql_error());
if(mysql_num_rows($action) > 0)
{
?>
<table border="1">
<tr COLSPAN=2 BGCOLOR="#6D8FFF">
<td>INSTALL</td>
</tr>
<?php
while($row=mysql_fetch_array($action))
{
echo "<tr>".
"<td>".$row["Install"]."</td>".
"</tr>";
}//end while
}
else
{
echo "don't exist recordsfor list ";
}//end if
mysql_close($link);
}//end if
?>
</phpcode>
</body>
</html>
good luck ..!!

No data insertion when button pressed

When i press the submit button to insert record, it pulls out no error but when i check the database i find no records submitted too. please what could be wrong with my script. just started with php
<?php
if (isset($_POST['submitted'])){
include('Connections/connect.php');
$term= $_POST['term'];
$details= $_POST['details'];
$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";
$newrecord ="Inserted Successfully";
}
?>
connect.php
<?php
$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_mydb, $username_mydb, $password_mydb) or trigger_error(mysql_error(),E_USER_ERROR);
?>
HTML
<form id="form1" name="form1" method="post" action="page1.php">
<p>
<label for="term"></label>
<input type="text" name="term" id="term" />
</p>
<p>
<label for="details"></label>
<input type="text" name="details" id="details" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
<input name="submitted" type="hidden" value="submitted" />
</p>
</form>
<p>
<?php
$newrecord
?>
There is a lot wrong with your code
Let's take it step by step:
<?php
if (isset($_POST['submitted'])){
include('Connections/connect.php');
$term= $_POST['term'];
$details= $_POST['details'];
You are not escaping here. When I'm a bad man I could destroy your application
Read more about escaping here: How can I prevent SQL injection in PHP?
$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";
You are defining a query here but you do not do anything with this query
read about executing query's on the php documentation page: http://php.net/manual/en/mysqli.query.php
$newrecord ="Inserted Successfully";
You are defining a variable $newrecord here but it does not have a function here. Add echo $newrecord; to echo the value of the variable $newrecord: http://php.net/echo
}
?>
Then you are not using the correct variables in your connect.php
<?php
$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_mydb, $username_mydb, $password_mydb) or trigger_error(mysql_error(),E_USER_ERROR);
?>
You are defining $hostname_speedapp and using $hostname_mydb in your mysqli_connect change that to $hostname_speedapp etc.. changing your connection string to:
$mydb= mysqli_connect($hostname_speedapp, $username_speedapp, $password_speedapp)
You are not selecting a database in your connectionstring. You are defining a variable with your database name called: $database_speedapp but you never use it.
Change your connectionstring to: $mydb= mysqli_connect($hostname_speedapp, $username_speedapp, $password_speedapp, $database_speedapp) and you should be good to go
add this
$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";
if (mysqli_query($mydb, $sql))
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($mydb);
}
EDIT 01
$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_mydb, $username_mydb, $password_mydb) or trigger_error(mysql_error(),E_USER_ERROR);
and top of page1.php
include("connect.php");
You dont even have an insert query in your script.
$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_mydb, $username_mydb, $password_mydb) or trigger_error(mysql_error(),E_USER_ERROR);
$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";
if (mysqli_query($mydb, $sql)) {
$newrecord ="Inserted Successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($mydb);
}
mysqli_close($mydb);
use this:
` $sql = "INSERT INTO people (term,details) VALUES ($term,$details)";
$result=mysqli_query($mydb,$sql);`
Connect.php
<?php
$hostname_speedapp = "localhost";
$database_speedapp = "mydb";
$username_speedapp = "root";
$password_speedapp = "password";
$mydb= mysqli_connect($hostname_speedapp, $username_speedapp, $password_speedapp,$database_speedapp) or trigger_error(mysql_error(),E_USER_ERROR);
?>
page1.php
if (isset($_POST['submit'])){
include('Connections/connect.php');
$term= $_POST['term'];
$details= $_POST['details'];
$sql = "INSERT INTO people (term,details) VALUES ('".$term."' , '".$details."')";
if ($mydb->query($sql) === TRUE) { //can use connected database $mydb
$newrecord = "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $mydb->error;
}
}
?>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="term"></label>
<input type="text" name="term" id="term" />
</p>
<p>
<label for="details"></label>
<input type="text" name="details" id="details" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
<p>
<?php
if(isset($newrecord)){
echo "<h3>$newrecord</h3>";
}
?>
I think you need to quote the submitted data values in your sql query:
Change the following:
$sql = "INSERT INTO people (term,details) VALUES ($term,$details)";
to
$sql = "INSERT INTO people (term,details) VALUES ('$term','$details')";

Categories