PHP connection to a database (no output!) - php

I wrote a code to connect a html page to the database, but it does not search for all employees in the table. I'm not sure why the code seems to be correct. It does not give any error or warning.
Can anyone explain to me what is wrong?
<div>
<form id='searchform' action='index.php' method='get'>
<a href='index.php'>All employees</a> ---
Search by a last name:
<input id='search' name='search' type='text' size='20' value='<?php echo #$_GET['search']; ?>' />
<input id='submit' type='submit' value='Go!' />
</form>
</div>
<?php
// check if search view of list view
if (isset($_GET['search'])) {
$sql = "SELECT * FROM Employee WHERE LastName like '%" . #$_GET['search'] . "%'";
} else {
$sql = "SELECT * FROM Employee";
}
// execute sql statement
$stmt = oci_parse($conn, $sql);
oci_execute($stmt);
?>
<table style='border: 1px solid #DDDDDD'>
<thead>
<tr>
<th>Personal Number</th>
<th>Insurance Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Birth Date</th>
<th>Gender</th>
<th>Hire Date</th>
<th>Salary</th>
<th>Office ID</th>
</tr>
</thead>
<tbody>
<?php
// fetch rows of the executed sql query
while ($row = oci_fetch_assoc($stmt)) {
echo "<tr>";
echo "<td>" . $row['PersonalNumber'] . "</td>";
echo "<td>" . $row['InsuranceNumber'] . "</td>";
echo "<td>" . $row['FirstName'] . " " . $row['LastName'] . "</td>";
echo "<td>born on " . $row['BirthDate'] . "</td>";
echo "<td>" . $row['Gender'] . "</td>";
echo "<td>hired on " . $row['HireDate'] . "</td>";
echo "<td>" . $row['Salary'] . "</td>";
echo "<td>" . $row['OfficeID'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<div>There are found <?php echo oci_num_rows($stmt); ?> employees!</div>
<?php oci_free_statement($stmt); ?>
</body>

Related

table class not working after inserting sql database inside

I firstly did the table layout and made sure everything is working, after connecting the table to a database and trying to put the records inside it, everything worked perfectly, but the class didnt, i have now the boring table without the layout made.
<body>
<h1>Employees</h1>
<table class="responstable">
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>
the table class is "responstable"
table tag is defined in 2 places.try removing this.
echo "<table border='1'>
or remove the table tag at the top after h1 tag and add the class to the table tag defined in the echo as,
complete code
<body>
<h1>Employees</h1>
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1' class='responstable'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>

Display table vertically

I want to display table vertically. Now, It's displaying horizontally. Each
and every field is displaying. How can I display table vertically?
And one more thing I want to add one more function to the button. Current
date added into database when button
submit is clicked. For now this code is making my status 0 and not updating
date in database.
<html>
<head>
<meta name="description" content="website description" />
<meta name="keywords" content="website keywords, website keywords" />
<meta http-equiv="content-type" content="text/html; charset=windows-
1252" />
<link rel="stylesheet" type="text/css" href="style/style.css" />
<style>
<table>
{
border-style:solid;
border-width:2px;
border-color:pink;
}
</style>
</head>
<?php
include 'inc/head.php';
include 'connection.php';
$id = $_GET['id'];
$sql = "SELECT * FROM user WHERE id='$id'";
$query = mysqli_query($conn, $sql);
echo "<table border='6'>
<tr>
<th>Application Type</th> <br>
<th>First name</th><br>
<th>Sur name</th><br>
<th>title</th>
<th>Date of Birth</th>
<th>Gender</th>
<th>Nationality</th>
<th>Country of Birth</th>
<th>Applicant's Location</th>
<th>alias</th>
<th>Criminal </th>
<th>Country of passport</th>
<th>Passport number</th>
<th>Passport issue date </th>
<th>Passport expiry date</th>
<th>Address </th>
<th>City</th>
<th>State</th>
<th>Postal Code</th>
<th>Country</th>
<th>Email Address</th>
<th>Home Number</th>
<th>Business Number</th>
<th>mobile</th>
</tr>";
while($row = mysqli_fetch_assoc($query)) {
echo "<tr>";
echo " <td>" . $row['applicationtype'] . "</td>";
echo " <td>" . $row['firstname'] . "</td>";
echo " <td>" . $row['lastname'] . "</td>";
echo " <td>" . $row['title1'] . "</td>";
echo " <td>" . $row['dob'] . "</td>";
echo " <td>" . $row['gender'] . "</td>";
echo " <td>" . $row['nationality'] . "</td>";
echo " <td>" . $row['countryofbirth'] . "</td>";
echo " <td>" . $row['applicantlocation'] . "</td>";
echo " <td>" . $row['alias'] . "</td>";
echo " <td>" . $row['criminal'] . "</td>";
echo " <td>" . $row['countryofpassport'] . "</td>";
echo " <td>" . $row['passportnumber'] . "</td>";
echo " <td>" . $row['pid'] . "</td>";
echo " <td>" . $row['ped'] . "</td>";
echo " <td>" . $row['address'] . "</td>";
echo " <td>" . $row['city'] . "</td>";
echo " <td>" . $row['state'] . "</td>";
echo " <td>" . $row['postalcode'] . "</td>";
echo " <td>" . $row['country'] . "</td>";
echo " <td>" . $row['email'] . "</td>";
echo " <td>" . $row['homephone'] . "</td>";
echo " <td>" . $row['businessphone'] . "</td>";
echo " <td>" . $row['mobile'] . "</td>";
echo "</tr>";
}
echo "</table><br><br>";
if(isset($_POST['submit'])) {
echo $radio = $_POST['radio'];
$date_clicked = date('Y-m-d H:i:s');
$sql = "UPDATE user SET visastatus='$radio' AND currentdate='$date_clicked' WHERE id='$id'";
$query = mysqli_query($conn, $sql);
if($query) {
echo "<h4 style='color:green'>Action Performed Successfully....</h4>";
} else {
echo "<h4 style='color:red'>Failed.</h4>";
}
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6" style="margin-top:
8px;">
<label class="checkbox-inline tourist-rad">
<input type="radio" name="radio" id="success"
value="Successfull">Successfull
</label>
<label class="checkbox-inline tourist-rad">
<input type="radio" name="radio" id="decline" value="Declined">Declined
</label>
</div>
<div class="form-group">
<button type="submit" name="submit" value="submit" class="btn btn-
primary" style="float: right;">Update Menu Item</button>
</div>
</body>
</html>
How can I display the table vertically?
Put each heading in a new <tr>. Remember, <tr> stands for table row, while <td> is for each of the elements in that row. This will require some modification of your php code so that you can essentially print a new row, and each td within it.
Edit: To display it vertically as you like, you'd need to modify your existing while loop.
//Print the row
echo "<tr>";
//Print the row heading
<th>Application Type</th>
while($row = mysqli_fetch_assoc($query)) {
//Print each matching data type inside
echo " <td>" . $row['applicationtype'] . "</td>";
}
//Close the row
echo "</tr>";
You'll need to do this functionality for each of the headings you have. You could also use arrays along with nested loops to print all the data cleanly and easily without excess code.
If by "vertically" you mean you want to have 2 columns and 24 rows instead of 24 columns and 2 rows, this will do it for you:
echo "<table border='6'>";
while($row = mysqli_fetch_assoc($query)) {
echo "<tr><th>Application Type</th><td>{$row['applicationtype']}</td></tr>";
echo "<tr><th>First Name</th><td>{$row['firstname']}</td></tr>";
echo "<tr><th>Surname</th><td>{$row['lastname']}</td></tr>";
echo "<tr><th>Title</th><td>{$row['title1']}</td></tr>";
echo "<tr><th>Date of Birth</th><td>{$row['dob']}</td></tr>";
echo "<tr><th>Gender</th><td>{$row['gender']}</td></tr>";
echo "<tr><th>Nationality</th><td>{$row['nationality']}</td></tr>";
echo "<tr><th>Country of Birth</th><td>{$row['countryofbirth']}</td></tr>";
echo "<tr><th>Applicant's Location</th><td>{$row['applicantlocation']}</td></tr>";
echo "<tr><th>Alias</th><td>{$row['alias']}</td></tr>";
echo "<tr><th>Criminal</th><td>{$row['criminal']}</td></tr>";
echo "<tr><th>Country of Passport</th><td>{$row['countryofpassport']}</td></tr>";
echo "<tr><th>Passport Number</th><td>{$row['passportnumber']}</td></tr>";
echo "<tr><th>Passport Issue Date</th><td>{$row['pid']}</td></tr>";
echo "<tr><th>Passport Expiry Date</th><td>{$row['ped']}</td></tr>";
echo "<tr><th>Address</th><td>{$row['address']}</td></tr>";
echo "<tr><th>City</th><td>{$row['city']}</td></tr>";
echo "<tr><th>State</th><td>{$row['state']}</td></tr>";
echo "<tr><th>Postal Code</th><td>{$row['postalcode']}</td></tr>";
echo "<tr><th>Country</th><td>{$row['country']}</td></tr>";
echo "<tr><th>Email Address</th><td>{$row['email']}</td></tr>";
echo "<tr><th>Home Number</th><td>{$row['homephone']}</td></tr>";
echo "<tr><th>Business Number</th><td>{$row['businessphone']}</td></tr>";
echo "<tr><th>Mobile</th><td>{$row['mobile']}</td></tr>";
}
echo "</table>";
Additionally, if you are going to be handling such sensitive personal data, you MUST improve your query security starting with mysqli prepared statements using placeholders.

Return only searched rows in table and hide other

If I write something in search box and press search , it should only return matched rows and hide other rows.
Here is my code, it works perfects only issue is it gives me searched record + all record list of table.
What can I do to show only searched data in table.?
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page=5;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $num_rec_per_page;
$result= mysql_query("SELECT * FROM products LIMIT $start_from, $num_rec_per_page");
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%".$term."%' or price LIKE '%".$term."' or details LIKE '%".$term."'";
$r_query = mysql_query($sql);
if($r_query>1)
{
while ($row = mysql_fetch_array($r_query)){
echo "<tr bgcolor='red'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
}
else{
echo "Nothing should be displayed";
}
}
?>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr class='danger'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
?>
</table>
Just keep single while loop and run the query and search query as shown below it will solve the issue:
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1) {
while ($row = mysql_fetch_array($r_query)) {
echo "<tr bgcolor='red'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['details'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subcategory'] . "</td>";
echo "<td>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
} else {
echo "Nothing should be displayed";
}
?>
</table>

Having issue showing image returning from database with $.post() jquery and php

I'm retrieving a table from database as result of a search action, but when I try to display the result I see the table and the data, but the image returned in each row is not render, I think my problem is in the $('#search').html(data), I'm not sure please someone knows what is the problem?
this is the result
http://s9.postimg.org/mro5qn46n/search_result.jpg
****This is the search page, where result table is displayed****
<table align="center">
<tr>
<td>
<label for="criteria">Select Criteria</label>
</td>
<td>
<select name="select" id="criteria">
<option selected="true" style="display:none;"></option>
<option value="value1">name</option>
<option value="value2">apartment</option>
</select>
</td>
<td>
<input type="text" name="value" size="40" maxlength="60" id="value"\>
</td>
</tr>
<tr>
<td>
<input name="name-submit" type="button" id="submit" value="Search"\>
</td>
</tr>
<tr>
<td >
<div id="search"></div>
</td>
</tr>
</table>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
var criteria = $("#criteria option:selected").text();
var value = $("#value").val();
$.post("search_r.php",{criteria:criteria,value:value},function(data){
$('#search').html(data);
});
});
</script>
****This is the Page that calls $.post() in the main seach page ****
<?php
$criteria = $_POST['criteria'];
$value = $_POST['value'];
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE $criteria = '$value'";
$result = mysqli_query($con,$query);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src=get_image.php?id=".$row['0']." width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";
?>
***Here the get_image.php****
<?php
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$id = $_GET['id'];
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
header('Content-Type: image/jpg');
echo $picture['11'];
?>
You can chage the get_image.php file as this. Then this work will work.
<?php
function get_image($id){
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
return $picture['11'];
}
?>
Then use require_once(); function and in image src like this.echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";. In your code check how the src path will print and it will be print like as echo string, not as a execute the file.
echo
"<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
require_once('search_r.php');
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";

Deleting rows from table on web retrieved from database

I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
First off you can just implode() all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway

Categories