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>';
?>
Related
I'm trying to make a dropdown list that allows users to select a parts that they need, so after selecting all they need and submit it should go to MySQL database. But after selecting submit nothing is inserting into my database.
My code and connection:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "userregistration";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo 'good connection';
?>
<form action="trying.php" method=" POST">
<table border=" 1">
<thead>
<tr>
<th>Component</th>
<th>Item Name</th>
<th>Price </th>
</tr>
</thead>
<tbody>
<tr>
<td>CPU</td>
<td>
<?php
//Retrieving CPU table
$query = $conn->query("SELECT * FROM cpu");
echo '<select name="cpu" class="cpu" onChange = $("#cpuprice").val($(this).find("option:selected").attr("cpuprice"))>';
while ($obj = mysqli_fetch_assoc($query)) {
echo '<option cpuprice = ' . $obj['price'] . ' cpuname=' . $obj['cpuname'] . ' >' . $obj['cpuname'] . '</option> /n';
}
echo '</select>';
?>
</td>
<td>
<output id="cpuprice" disabled value="">
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>GPU</td>
<td>
<?php
//Retrieving GPU table
$query = $conn->query("SELECT * FROM gpu");
echo '<select name="gpu" class ="gpu" onChange = $("#gpuprice").val($(this).find("option:selected").attr("gpuprice"))>';
while ($obj = mysqli_fetch_assoc($query)) {
echo '<option gpuprice = "' . $obj['price'] . '" gpuname = "' . $obj['gpuname'] . '">' . $obj['gpuname'] . '</option>';
}
echo '</select>';
?>
</td>
<td>
<output class="form-control prc" id="gpuprice" disabled value="">
</td>
</tr>
</tbody>
</table>
<input class="submit" type="submit" />
</form>
I tried this but it doesn't work, after cliking submit it echo adding error and nothing inserted into my database
<?php
if (!empty($_POST["cpu"]) && !empty($_POST["gpu"])) {
$cpu = isset($_POST["cpu"]);
$gpu = isset($_POST["gpu"]);
$qstr = "INSERT INTO trycombuild(cpuname, gpuname) VALUES ('$cpu' , '$gpu')";
$query = mysqli_query($conn, $qstr);
} else
echo 'adding error';
?>
I tried to echo the $cpu and $gpu and it says undefined variable
I also tried this:
if (!empty($_POST['cpu']) && !empty($_POST['gpu'])) {
$cpu = $_POST['cpu'];
$gpu = $_POST['gpu'];
$qstr = $conn->prepare("INSERT INTO trycombuild (cpuname, gpuname) VALUES (?, ?)");
$qstr->bind_param("ss", $cpu, $gpu);
$qstr->execute();
$sqtr->close();
}
Your html is not correct. remove unnecessary space and try
<tbody>
<tr>
<td>CPU</td>
<td>
<?php
//Retrieving CPU table
$query = $conn->query("SELECT * FROM cpu");
echo '<select name="cpu" class="cpu" onChange = $("#cpuprice").val($(this).find("option:selected").attr("cpuprice"))>';
echo "<option>---select your CPU---</option>/n";
while ($obj = mysqli_fetch_assoc($query)) {
echo '<option cpuprice = ' . $obj['price'] . ' cpuname=' . $obj['cpuname'] . ' >' . $obj['cpuname'] . '</option> /n';
}
echo '</select>';
?>
</td>
<td>
<output id="cpuprice" disabled value="">
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>GPU</td>
<td>
<?php
//Retrieving GPU table
$query = $conn->query("SELECT * FROM gpu");
echo '<select name="gpu" class ="gpu" onChange = $("#tpuprice").val($(this).find("option:selected").attr("gpuprice"))>';
echo "<option>---select your GPU---</option>";
while ($obj = mysqli_fetch_assoc($query)) {
echo '<option gpuprice = "' . $obj['price'] . '" gpuname = "' . $obj['gpuname'] . '">' . $obj['gpuname'] . '</option>';
}
echo '</select>';
?>
</td>
<td>
<output class="form-control prc" id="tpuprice" disabled value="">
</td>
</tr>
</tbody>
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
<p><?php include 'header.php'; ?></p>
<div align="justify">
<td>Name:<input type="text" name="password" ></td> <!-- database -->
<td> Rank:<select>
<!--<option value="volvo">//Database</option>
<option value="saab">Saab</option>
-->
<?php
require ("dbfunction.php");
$con = getDbConnect();
<td> <input type="checkbox" name="vehicle" value="Bike">Group by Rank</td> <!-- database -->
<td> <input type="checkbox" name="vehicle" value="Bike">Include previous service terms</td> <!-- database -->
</div>
<p><table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Start Date</th>
<th>End Date</th>
<th>Watchkeeping</th>
<th>Active</th>
<th></th>
<th></th>
</tr> <!-- database -->
<tr> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if (!mysqli_connect_errno($con)) {
$queryStr = "SELECT * " .
"FROM crewlist";
}
$result = mysqli_query($con, $queryStr);
while ($row = mysqli_fetch_array($result)) {
//echo "<div><a href=http://localhost/poshproject/crewlisting.php?crew_name={$row["crew_id"]}>";
echo "<tr>.<th>" . $row["crew_name"] . "<br></br>" . "</th>";
echo "<th>" . $row["crew_rank"] . "</th>";
echo "<th>" . $row["start_date"] . "</th>";
echo "<th>" . $row["end_date"] . "</th>";
echo "<th>" . $row["watchkeeping"] . "</th>";
echo "<th>" . $row["active"] . "</th>";
echo "<td>Edit";
//echo "<td><center><button type=\"submit\" name=\"Delete\" value="' . $row['crew_id'].'"/>Delete</button></center></td>";
echo "<td>Delete";
}
?>
<!--
<td><center><button type="submit" value="Edit">Edit</button></center></td>
<td><center><button type="submit" value="Delete">Delete</button></center></td>-->
</form></tr>
</tr>
</table>
---------------------delete.php---------------------
<?php
//print_r($_GET);
include 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$queryStr = "SELECT * FROM crewlist";
}
$result = mysqli_query($con, $queryStr);
while ($row = mysqli_fetch_array($result)) {
if (!mysqli_connect_errno($con)) {
$sqliQueryStr = "DELETE FROM `posh`.`crewlist` WHERE crew_id = ". $row['crew_id'] . "";
}
mysqli_query($con, $sqliQueryStr);
header('Location: crewlisting.php');
mysqli_close($con);
//echo "user has been deleted";
}
?>
Delete function only works on first row of database. When I delete the rows that are not the first, it deletes the first row instead. Not sure where the error is when I've tried pretty much everything.
I think you are wrong on delete.php file. Put below code in your delete.php file.
---------------------delete.php---------------------
<?php
include 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$sqliQueryStr = "DELETE FROM `posh`.`crewlist` WHERE crew_id = " . $_GET['id'];
mysqli_query($con, $sqliQueryStr);
}
header('Location: crewlisting.php');
mysqli_close($con);
Change your delete query as below
$sqliQueryStr = "DELETE FROM `posh`.`crewlist` WHERE crew_id = " . $_GET['id'] . "";
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);
Im a newbie in PHP and I want to create a simple webpage app for my website, I was able to produce this page base on a tutorial here.
<?php
$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*****", $con);
$result = mysql_query("SELECT * FROM products");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>classification</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['classification'] . "</td>";
echo "<td><input type='checkbox' name='{number[]}' value='{$row['prodID']}' /></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<?php
?>
<html>
<head>
</head>
<form name="form1" method="post" action="result_page.php">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
<body>
</body>
</html>
but my problem is how to create a result_page.php to show the selected entries or data base on the selected checkbox so i can create a comparison page. I have this as being my result_page.php but nothing is showing up. I know Im doing something wrong but I cant find out.
<?php
error_reporting(E_ALL);
$host = 'localhost';
$user = '******';
$pass = '******';
$dbname = '******';
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
$sql = "SELECT * FROM products WHERE prodID IN (";
foreach ($_POST['number'] as $product) $sql .= "'" . $product . "',";
$sql = substr($sql,0,-1) . ")";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result))
{
echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</td></tr>\n";
do {
printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["1"], $myrow["2"], $myrow["3"]);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
?>
A quick glance, the section that generates the output is not correct. You have looped two times for no apperant reason.
while ($myrow = mysql_fetch_array($result)) //<========remove this line
{ //<========remove this line
echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</td></tr>\n";
do {
printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["1"], $myrow["2"], $myrow["3"]);
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
} //<========remove this line
This is done by human parse, but should serves as a starting point.
And to recap tadman, no this is not a good tutorial. And normally you won't need to do printf for the output.