I am trying to make the form search by ID, firstname, and lastname. I want the user to type in either one in the search field and get the results from the database.
Here is the actual form I am using:
<form action="form.php" method="post">
<input type="text" name="term" />
<input type="submit" value="Submit" />
</form>
And here is the form.php
<?php
$db_hostname = 'localhost';
$db_username = 'test';
$db_password = 'test';
$db_database = 'test';
// Database Connection String
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_database, $con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>SpeedZone Data Search</title>
<style type="text/css">
table { border-collapse:collapse; }
table td, table th { border:1px solid black;padding:5px; }
tr:nth-child(even) {background: #ffffff}
tr:nth-child(odd) {background: #ff0000}
</style>
</head>
<body>
<form action="form.php" method="post">
<input type="text" name="term" />
<input type="submit" value="Search" />
</form>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname LIKE '%".$term."%' or id = ".$term;
$r_query = mysql_query($sql);
echo "<table border='1' cellpadding='5'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Address</th> <th>City</th> <th>State</th> <th>Zip</th> <th>Phone</th> <th>DL</th> <th>Email</th> <th>Car and Controller</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
while ($row = mysql_fetch_array($r_query)){
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['firstname'] . '</td>';
echo '<td>' . $row['lastname'] . '</td>';
echo '<td>' . $row['address'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['st'] . '</td>';
echo '<td>' . $row['zip'] . '</td>';
echo '<td>' . $row['phone'] . '</td>';
echo '<td>' . $row['dl'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['carcont'] . '</td>';
echo '<td>Edit</td>';
// echo '<td>Delete</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
}
?>
</body>
</html>
Where I currently have this: It is only searching the ID. I want to be able to type in the ID, or the firstname, or the lastname, or first and last if possible.
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname LIKE '%".$term."%' or id = ".$term;
I think there are a few things I will need to change but I am confused and have lost myself in it and cannot solve it. Please help.
You need to put quotes around $term when you compare it with id. Otherwise, you'll get a syntax error if it's not a number.
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or lastname LIKE '%".$term."%' or id = '".$term."'";
Also, this assumes you don't use 0 as an id. When a number is compared to a string, the string is converted to a number, and all non-numeric strings get converted to 0 and they'll match that id. If that's a problem, you should check whether $term is a number first. If it's not a number, use a query that doesn't include the id check:
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%$term%' or lastname LIKE '%$term%'";
if (is_numeric($term)) {
$sql .= " or id = $term";
}
Your query looks coorect to me.
Try using braces to separate out OR conditions
$sql = "SELECT * FROM rcrentals WHERE firstname LIKE '%".$term."%' or (lastname LIKE '%".$term."%') or (id = ".$term);
Related
I want to search the data from mysql and need to display it in a table. Here searching of data is successful but the data is not getting displayed in table, instead it is displaying in a straight line. Here am using $output method. My codings are
form.php
<html dir="ltr" lang="en-US" >
<head>
<meta charset="UTF-8" />
<title>Search</title>
</head>
<body>
<h1>Seacrh By Name</h1>
<form action="search.php" method="get">
<label>Name:
<input type="text" name="keyname" />
</label>
<input type="submit" value="Search" />
</form>
</body>
</html>
search.php
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "xx"; //server
$db = "xx"; //database name
$user = "xx"; //dabases user name
$pwd = "xx"; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM customer_details WHERE id LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "id: " . $row['id'] . " ";
$output .= "name: " . $row['name'] . " ";
$output .= "Telephone: " . $row['Telephone'] . " ";
$output .= "E_mail: " . $row['E_mail'] . " ";
$output .= "visa_category: " . $row['visa_category'] . " ";
$output .= "other_category: " . $row['other_category'] . " ";
$output .= "passport_no: " . $row['passport_no'] . " ";
$output .= "remarks: " . $row['remarks'] . " ";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
How can i display my data's in html table.
Your $output is only outputting the data itself. Make it also output an HTML table.
echo '<table>
<tr>
<th>ID</th>
<th>Name</th>
</tr>';
while ($row = mysqli_fetch_array($results)) {
echo '
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['name'].'</td>
</tr>';
}
echo '
</table>';
By the way, don't scroll down straight to the code, see the warnings about when/how to use tables as well.
<?php
include("include/connect.php");
$emp_id = $_POST['emp_id'];
$sqlStr = "SELECT * FROM `employee` ";
//echo $sqlStr . "<br>";
$result = mysql_query($sqlStr);
$count = mysql_num_rows($result);
echo '<table>
<tr>
<th>Employee ID</th>
<th>Name</th>
<th>date of joining</th>
<th>date of living in service</th>
<th>salary</th>
</tr>';
while ($row = mysql_fetch_array($result)) {
echo '
<tr>
<td>'.$row['emp_id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['DOJ'].'</td>
<td>'.$row['DLS'].'</td>
<td>'.$row['salary'].'</td>
</tr>';
}
echo '
</table>';
?>
Script Page is working nicely. When I select the multiple options in next dashboard page, no records display. Please fix this problem. I think the selected value cannot recognize in dashboard page
Script.php
<?php include("connection.php") ?>
<form id="script" name="script" action="dashboard.php" method="post">
<strong>Choose Script Name : </strong><select name="script[]" id="select3" multiple=multiple style="margin: 20px;width:300px;">
<?php
$result = $conn->query("select script_name from script_details ORDER BY script_name");
while ($row = $result->fetch_assoc()) {
unset($script_name);
$script_name = $row['script_name'];
echo '<option value="' . $id . '">' . $script_name . '</option>'; // Generated From database
}
?>
</select>
<input type="submit" name="submit" id="button" value="View Dashboard" />
</form>
Dashboard.php
<table border="1">
<tr align="center">
<th>Number </th> <th>Script Name</th> <th> Date</th>
</tr>
<?php
include("connection.php");
$select = $_POST['script'];
$selects = "SELECT * FROM script_details where script_name='$select'";
$result = $conn->query($selects);
echo "<table>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"] . "</td><td>" . $row["script_name"] . "</td></tr>" . "</td><td>" . $row["date"] . "</td></tr>";
}
echo "</table>";
[This is script page Image. Selecting option from script_details database. Field name : script_name.][1]?>
This is Dashboard page. when selecting script2, script3 option. Doesnot show record for selected items.
Firstof all your code is sql vulnerable
In Scrip you didn't define values of options in <select> tag. define value first and for this you need to fetch is from database
Script.php
<?php include("connection.php") ?>
<form id="script" name="script" action="dashboard.php" method="post">
<strong>Choose Script Name : </strong>
<select name="script[]" id="select3" multiple=multiple style="margin: 20px;width:300px;">
<?php
$result = $conn->query("select id, script_name from script_details ORDER BY script_name");
while ($row = $result->fetch_assoc()) {
unset($script_name);
$script_name = $row['script_name'];
$id = $row['id'];
echo '<option value="' . $id . '">' . $script_name . '</option>'; // Generated From database
}
?>
</select>
<input type="submit" name="submit" id="button" value="View Dashboard" />
</form>
In dashboard do proper markup
Dashboard.php
<table border="1">
<tr align="center">
<th>Number </th> <th>Script Name</th> <th> Date</th>
</tr>
<?php
include("connection.php");
$select = $_POST['script'];
$ids = "'" . implode("','", $select) . "'";
$selects = "SELECT * FROM script_details WHERE id IN ($ids)";
$result = $conn->query($selects);
while ($row = $result->fetch_assoc()) {
echo "<tr>"
. "<td>" . $row["id"] . "</td>"
. "<td>" . $row["script_name"] . "</td>"
. "<td>" . $row["date"] . "</td>"
. "</tr>";
}
?>
</table>
I would approach it in the following way:
$scriptsArr = $_POST['script'];
$scriptsStr = implode(',', $scriptsArr);
$selects = "SELECT * FROM script_details where script_name IN ($scriptsStr)";
I've split it to few variables so you can understand the process.
Hope I could help!
I hope your understand is not safe at all, I would suggest you will read a bit more about prepared statements:
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
I'm trying to update the rank column in the users table in MySQL using PHP, but when I try to change the values and press the update button, only the last one of the table rows is actually being updated. Here is an image of what the PHP table looks like on the webpage:
Here is the code:
<?php
include '../db/connect.php';
$con = $MySQLi_CON;
if (!$con){
die("Can not connect: " . mysql_error());
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE users SET rank='$_POST[rank]' WHERE user_id='$_POST[hidden]'";
$con->query($UpdateQuery);
}
$result = $MySQLi_CON->query("SELECT * FROM users")
or die(mysql_error());
echo "<table border=1>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Rank</th>
</tr>";
echo "<form action='test3.php' method='post'";
while($record = $result->fetch_array()){
echo '<tr>';
echo '<td>' . $record['user_id'] . '</td>';
echo '<td>' . $record['username'] . '</td>';
echo '<td>' . $record['email'] . '</td>';
echo '<td>' . '<input type="number" name="rank' . [$record['user_id']] . '" />';
echo '<td>' . '<input type="hidden" name="hidden" value="' . $record['user_id'] . '"</td>';
echo '<td>' . '<input type="submit" name="update" value="update"' . '</td></tr>';
}
echo "</table>";
for($_POST['rank'] as $user_id=>$rank){
$UpdateQuery = "UPDATE users SET rank='$rank' WHERE user_id='$user_id'";
$con->query($UpdateQuery);
}
$con->close();
The problem is all rank of each row is using the same name rank and also the hidden, the browser will override all rank and hidden value with the latest element.
change <input type="number" name="rank" /> to <input type="number" name="rank[$record['user_id']]" />
receive in php with $_POST['rank'] in array format and loop each rank.
for($_POST['rank'] as $user_id=>$rank){
$UpdateQuery = "UPDATE users SET rank='$rank' WHERE user_id='$user_id'";
$con->query($UpdateQuery);
}
I am trying to add CSS to my form but not sure how to do this. The form is created in php and MySQL, in browser it looks like: http://gyazo.com/5d099ead9bd6ea83859a5114b2438748
I need to allign the text and drop downs so they are in the equal throughout and add some spacing. Anyone help with CSS for this?
html currently:
<div class="wrap">
<img src="/images/logo.png" alt="Highdown logo" />
<h1>Entry form</h1>
</div>
css currently:
.wrap {
position: relative;
}
The form is produced with this:
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\" action=\"save.php\"> ";
while ($row = $event_result->fetch_assoc()) {
echo $row['Name']. ' ';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo "<select name ='". $row['Name']."'>";
while ($row1 = $student_result->fetch_assoc()) {
echo '<option value="" style="display:none;"></option>';
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select> <br />";
}
}
}
echo '<input type="submit" value ="Submit">';
echo '<input type="reset" value ="Reset">';
echo '<input type="button" value = "Add student" onclick="location.href=\'http://localhost/sportsday/addstudent.php\'">';
echo '<input type="button" value = "Delete student">';
echo "</form>";
}
Use
<form>
<table>
<tr> //1st Table row
<td></td> //Table column data
<td></td> //table column data
</tr> //1st row ends
<tr> // 2nd Table row
<td></td> //Table column data
<td></td> //table column data
</tr> //2nd row ends
</table>
</form>
This will give you a better layout of the form.
This should work i did not try as i dont have the database
//Query to display all events
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\" action=\"save.php\"> ";
echo '<table>';
echo '<tr>';
echo '<td>';
while ($row = $event_result->fetch_assoc()) {
echo $row['Name']. ' ';
echo '</td>';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Username = '" . $_SESSION['Username'] . "'")) {
if ($student_result->num_rows) {
echo '<td>';
echo "<select name ='". $row['Name']."'>";
while ($row1 = $student_result->fetch_assoc()) {
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select> <br />";
echo '</td>';
echo '</tr>';
}
}
}
echo '</table>';
echo '<input type="submit" value ="Submit">';
echo '<input type="reset" value ="Reset">';
echo '<input type="button" value = "Add student" onclick="location.href=\'http://localhost/sportsday/addstudent.php\'">';
echo '<input type="button" value = "Delete student">';
echo "</form>";
}
?>
you can directly write in css
form {
⋮ declare css
}
or give name to form
form[name="value"]{
⋮ declare css
}
or add any class or id on form
#formid{
⋮ declare css
}
.formclass{
⋮ declare css
}
First , check your database...
May be there is Another Issue not related to Tabular Output.
So , First remove Table Tag..and check whether its working ?
Then try in HTML TABLE TAG
Otherwise give me sample database .sql File and complete PHP code in google drive or on shared drive.
So that I can check and identify where is problem ?
Hi there im in the process of making a page to approve and deny names in a database, I currently have it so it finds an pulls the pending requests im looking for now i wanna know how to update the row once i click approve or deny.
the values it needs to update is the NameState column to either ("approved") or ("REJECTED")
there can be any where from 1 to 100 names at a time.
Thanks Ryan
$DB_HOST = "IP";
$DB_USER = "user";
$DB_PASS = "Pass";
$DB_DTBS = "DB";
mysql_connect($DB_HOST, $DB_USER, $DB_PASS) or die(mysql_error()); // Connect to database server(localhost) with username and password.
mysql_select_db($DB_DTBS) or die(mysql_error()); // Select registration database.
$search = mysql_query('SELECT * FROM TABLE WHERE `NameState` = \'(\"PENDING")\'') or die(mysql_error());
$match = mysql_num_rows($search);
if($match > 0){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Current Name</th>
<th>Requested Name</th>
<th>Approve or Deny</th>
</tr>";
while($row = mysql_fetch_array($search)) {
echo "<tr>";
echo "<td>" . $row['object_id'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['WishName'] . "</td>";
echo '<td> <form method="post" action=<?php echo Approve:<input type="radio" value="Approve" name="name"> Deny:<input type="radio" value="Deny" name="name"><br /> </td>';
echo "</tr>";
}
echo "</table>";
echo '<input type="Submit" value="Submit" name="Submit"> </form>';
} else {
echo("No Names to Approve");
}
?>