I have this following PHP code in search.php to gather information from the form in the same file. It's a search engine and when I put something into the search bar I get only one row even when I should get more than one row.
Here is php script:
<?php
$error = "";
$con=mysqli_connect('localhost', 'root', '');
$db=mysqli_select_db($con, 'vodici');
if(isset($_POST['button'])){ //trigger button click
$search=$_POST['search'];
$query=mysqli_query($con,"select distinct * from users where meno like '%{$search}%' || priezvisko like '%{$search}%' || mesto like '%{$search}%' || kraj like '%{$search}%' || rok_narodenia like '%{$search}%' || email like '%{$search}%' ");
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_array($query)) {
$div = "<div style='border: 1px solid black; border-radius: 5px;'>".$row['id'];
$vysledok = "<h2 style='text-decoration: underline; padding-left: 2%;'>".$row['meno']." ".$row['priezvisko']."</h2>";
$mail = "<p style='padding-left : 2%; font-size: 11px;'><strong>Rok narodenia:</strong> ".$row['rok_narodenia']." <br><strong>E-mail: </strong> ".$row['email']."</p>";
$mesto = "<p style='padding-left: 2%'><strong>Mesto:</strong> ".$row['mesto']." <strong>Kraj:</strong> ".$row['kraj']."</p>";
$div_end = "</div>";
}
}else{
$error = "Nič sme nenašli :/ <br><br>";
}
}
mysqli_close($con);
?>
Here is form:
<form class="search-form" action="" method="post">
<div class="input-group">
<input name="search" type="text" class="form-control" placeholder="Vyhľadať...">
<div class="input-group-append">
<button class="btn btn-secondary" type="submit" name="button"><i class="fa fa-search"></button></i>
</div>
</div>
</form>
<p><?php if(isset($_POST['button']) && $error == "") {
while($row = mysqli_fetch_array($query)) {
echo $div,$vysledok,$mail,$mesto,$div_end;
}
}?></p>
<p><?php if(isset($_POST['button']) && $error !== "") {echo $error;}?></p>
What can I do here? I am out of ideas
Thanks
You only added one row. You need to add each row to the generated html.
$result = '';
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_array($query)) {
$div = "<div style='border: 1px solid black; border-radius: 5px;'>".$row['id'];
$vysledok = "<h2 style='text-decoration: underline; padding-left: 2%;'>".$row['meno']." ".$row['priezvisko']."</h2>";
$mail = "<p style='padding-left : 2%; font-size: 11px;'><strong>Rok narodenia:</strong> ".$row['rok_narodenia']." <br><strong>E-mail: </strong> ".$row['email']."</p>";
$mesto = "<p style='padding-left: 2%'><strong>Mesto:</strong> ".$row['mesto']." <strong>Kraj:</strong> ".$row['kraj']."</p>";
$div_end = "</div>";
$result .= $div. $vysledok. $mail.$mesto. $div_end ;
}
}else{
$error = "Nič sme nenašli :/ <br><br>";
}
echo $result;
}
In compensation to wakeels answer, that is correct, I would like to ask you to use prepared statements like in my example below
$sql="
SELECT `id`,`meno`,`priezvisko`,`rok_narodenia`,`email`,`mesto`,`kraj`
FROM `users`
WHERE
`meno` LIKE '%?%'
OR `priezvisko` LIKE '%?%'
OR `mesto` LIKE '%?%'
OR `kraj` LIKE '%?%'
OR `rok_narodenia` LIKE '%?%'
OR `email` LIKE '%?%'
;"
$stmt = $con->prepare($sql);
$stmt->bind_param("s", $search);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $div,$vysledok,$mail,$mesto,$div_end;
}
Related
When i keep search field blank and hit on search button, then show all results from mysql database, Why.... here is my php code....
I want to create it, when i keep search field blank and click search button... have to show error "no search result" and want to create disallowed white spacing search and need extra one HTML button for all Entries. By one click so that i get all entries......
please help me....
<?php
$con=mysql_connect('localhost', '1093913', 'tanim1996');
$db=mysql_select_db('1093913');
if(isset($_POST['button'])){ //trigger button click
$search=$_POST['search'];
$query=mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_array($query)) {
echo "<tbody>";
echo "<tr>";
echo "<td data-label='Student ID'>".$row['student_id']."</td>";
echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
echo "<td data-label='District'>".$row['district']."</td>";
echo "</tr>";
echo "</tbody>";
}
}else{
echo "<div class='error-text'>No results</div><br><br>";
}
}else{ //while not in use of search returns all the values
$query=mysql_query("select * from iconic19");
while ($row = mysql_fetch_array($query)) {
}
}
mysql_close();
?>
Its Html Code
<form id="nbc-searchblue1" method="post" enctype="multipart/form-data" autocomplete="off">
<input id='wc-searchblueinput1' placeholder="Search Iconic..." name="search" type="search" autofocus>
<br>
<input id='nbc-searchbluesubmit1' value="Search" type="submit" name="button">
<div class="view-all"> Show all</div>
</form>
Its css Code..
.view-all a {
background: red;
padding: 10px;
border-radius: 4px;
color: #fff;
text-decoration: none;
}
<?php
$con=mysql_connect('localhost', '1093913', 'tanim1996');
$db=mysql_select_db('1093913');
if(isset($_POST['button'])){ //trigger button click
$numRows = 0;
if(!empty($_POST['search'])) {
$search = mysql_real_escape_string($_POST['search']);
$query = mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");
$numRows = (int)mysql_num_rows($query);
}
if ($numRows > 0) {
while ($row = mysql_fetch_array($query)) {
echo "<tbody>";
echo "<tr>";
echo "<td data-label='Student ID'>".$row['student_id']."</td>";
echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
echo "<td data-label='District'>".$row['district']."</td>";
echo "</tr>";
echo "</tbody>";
}
} else {
echo "<div class='error-text'>No results</div><br><br>";
}
} else { //while not in use of search returns all the values
$query = mysql_query("select * from iconic19");
while ($row = mysql_fetch_array($query)) {
}
}
mysql_close();
?>
What I have done was creating a new variable $numRows with a default value of 0. If your search is empty there is no query to the database. I escaped your $search variable.
BTW: Please change to mysqli, the mysql extension is no longer supported in newer php versions.
Just check if $_POST['search'] is blank then display your message else execute your query.
I just want that if a user enters the name of item in text box then particular details from database should be displayed on the same page in a table below. Right now its working for one item. Now if a user wants to select multiple items and want to show record of each selected item on the page until a submit button is not pressed. Here is my code. Any help will be appreciated.
<html>
<head>
<title>Sales</title>
<script>
function search(string){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("search").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "sales_search.php?s="+string, true);
xmlhttp.send(null);
}
</script>
</head>
I have not included the CSS code to shorten it.
<?php
include "connection.php";
function sales_result()
{
$submit = $_GET['finish'];
if(isset($_GET['mname']) && $_GET['mname'] != '')
{
$name = $_GET['mname'];
$sql = "SELECT * FROM `medicine_item_record` WHERE `medicine_item_name` LIKE '%$name%'";
$result = mysqli_query($GLOBALS['link'],$sql);
$row = mysqli_fetch_assoc($result);
while($check =='submit')
{
echo strtoupper($row['company_name']);
}
}
}
?>
<body>
<div style="text-align: center; width: 500px; margin: 0 auto;">
<h1>Sales</h1><span style="font-family: tahoma, sans-serif, arial; margin-left: 150px; font-size: 13px;"></span><br/><br/>
<table border=1>
<tr><td><input type="text" placeholder="Type to search.." onkeyup="search(this.value)" id="text" ></td>
<td><input type="text" placeholder="Enter Quantity" name="quantity" id="text"></td>
<td><input type ="submit" name="finish" value="finish"></td>
</tr>
</table>
<div id="search">
</div>
</div>
<table border=1 width='100%' align= 'center'>
<tr><?php sales_result() ?> </tr>
</table>
</body>
</html>
code for sales_search.php is as follows:
<?php
include "connection.php";
if(isset($_GET['s']) && $_GET['s'] != '')
{
$s = $_GET['s'];
$sql = "SELECT * FROM `medicine_item_record` WHERE `medicine_item_name` LIKE '%$s%'";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result))
{
$name = $row['medicine_item_name'];
echo "<div style='' id='searchtitle'>"."<a style='font-family: verdana; text-decoration: none; color: black;' href='sales.php?mname=$name'>" . $name . "</div>";
}
}
?>
Not sure what you mean by Multiple Items. Taking a stab that you mean if the user enters different search terms? For example: "depressor, swab". You would need to parse this in some way before building your Query. If your separator (my example it's the comma) is in the string, explode it and then make your query using a loop:
<?php
include "connection.php";
if(isset($_GET['s']) && $_GET['s'] != ''){
$search_str = trim($_GET['s']);
if(strpos($search_str, ",")){
// Multiple Search Terms found
$s = explode($search_str, ",");
$sql = "SELECT * FROM `medicine_item_record` WHERE `medicine_item_name` LIKE '%" . mysqli_real_escape_string(trim($s[0]), $con) . "%'";
for($i=1;$i<count($s);$i++){
$sql .= " OR `medicine_item_name` LIKE '%" . mysqli_real_escape_string(trim($s[$i]), $con) . "%'";
}
$sql .= ";";
} else {
$sql = "SELECT * FROM `medicine_item_record` WHERE `medicine_item_name` LIKE '%" . mysqli_real_escape_string($search_str, $con) . "%';";
}
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result)){
$name = $row['medicine_item_name'];
echo "<div style='' id='searchtitle'>"."<a style='font-family: verdana; text-decoration: none; color: black;' href='sales.php?mname=$name'>" . $name . "</div>";
}
}
?>
Edit after your comment:
That is happening in the response of your AJAX call:
document.getElementById("search").innerHTML = xmlhttp.responseText;
This line of code replaces the content in that element. You will want to append the response:
var current = document.getElementById("search").innerHTML;
document.getElementById("search").innerHTML = current + xmlhttp.responseText;
I'm creating an eBook account on my website: where customers can have a library of ebooks and every time an eBook is downloaded, I need to add 1 to the database for the client to be able to see the number of downloads. I can not figure out the line of code I would need for this and I have done research but nothing can help with this specific query. This is what I have so far, which pulls the information from a sql table and display in html table:
<p>List of Publications</p>
<?php
//connect
$query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<td> <?php if ($numrows > 0){
while($row = mysql_fetch_array($query))
{
?>
<a href="<?php echo $row['Link']; ?>" target="_blank">
<img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></a> <?php
}
} else
echo "Wrong Query";
?>
</tr>
You could use JQuery
ebookDownloads.php
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="javascript/text">
// JavaScript Document
function Downloaded(id) {
var data = {'id':id};
$.post( "downloaded.php", data);
}
</script>
</head>
<p>List of Publications</p>
<?php
//connect $query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<?php
if ($numrows > 0){
while($row = mysql_fetch_array($query)) {
?>
<td><img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></td>
<?php
}
} else {
echo "Wrong Query";
}
?>
</tr>
</table>
downloaded.php
if(isset($_POST['id']) && is_numeric($_POST['id'])) {
$sql = "UPDATE table SET downloaded=downloaded+1 WHERE id = $_POST['id']";
}
EDIT: I have not tested this code. Also the user might not have javascript enabled in which case it wouldn't count the download, so if you need to count the downloads (e.g they pay for so many) you could make sure they have javascript enabled before showing the downloads.
Update: another method using a new tab and PHP to record the download then present it
<p>List of Publications</p>
<?php
//connect $query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<?php
if ($numrows > 0){
while($row = mysql_fetch_array($query)) {
?>
<td><img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></td>
<?php
}
} else {
echo "Wrong Query";
}
?>
</tr>
</table>
<?php
######## download.php #######################
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$query = "SELECT * FROM ebooks WHERE id = $_GET['id']";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
$sql = "UPDATE table SET downloaded=downloaded+1 WHERE id = $_POST['id']"; // update downloaded
while($row = mysql_fetch_array($query)) {
header('Content-Type: application/pdf');
header('Content-disposition: attachment;filename='.$row['name'].'pdf');
readfile($row['link']); // should be an absolute path
}
}
}
?>
I made some little translator.
And if word exist in database all is ok. But if word didnt exist in database, word isnt printed. I add some code to print if result is empty but didnt work.
Any idea why word that not exist in database isnt printed?
Here is code:
<p><form method="post" action="prevedi.php">
<textarea id="prevedi" name="prevedi" style="margin: 2px; height: 137px; width: 380px;">
<?php
echo htmlentities($_POST['prevedi']);
?>
</textarea>
<textarea id="prevod" disabled="disabled" name="prevod" style="margin: 2px; height: 137px; width: 380px; border: 0px;" readonly>
<?
if (isset($_POST['prevedi'])) {
//Kreci
$prevedi = htmlentities($_POST['prevedi']);
$prevedi = explode(" ",$prevedi);
foreach ($prevedi as $word) {
$slovo = $word[0];
$result = mysqli_query($con,"SELECT * FROM $slovo WHERE srpski='$word'");
if (!empty($result)) {
while($row = mysqli_fetch_array($result))
{
$prevod .= $row['romski']." ";
}
}
else
{
$prevod .= "".$word." ";
}
}
echo $prevod;
//Kraj isset
}
?>
</textarea><br>
<input name="translate" type="submit" value="Translate"/>
</form>
</p>
Because your result is not empty. You should check if $result number of rows is > 0
if ($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)){
$prevod .= $row['romski']." ";
}
}else{
$prevod .= $word." ";
}
i think you have to initialize $prevod before assigning it the way you did.
$prevod = "";
if (!empty($result)) {
while($row = mysqli_fetch_array($result))
{
$prevod .= $row['romski']." ";
}`enter code here`
}
else
{
$prevod .= "".$word." ";
}
}
echo $prevod;
I do not even know how to google this one...imagine it is something stupid...but any help would be great...
passing a variable when submitting a form...when echo the $_POST it is good...but when i put it into a php variable it is duplicated
<?
//list transactions by month
if ($_POST['m']=="yes"){
$table = $_POST['month'];
$_SESSION['table']=$_POST['month'];
$conn = mysql_connect("localhost", "mss_records", "3205") or die(mysql_error());
mysql_select_db('store_records', $conn) or die(mysql_error());
$result = mysql_query("SELECT * FROM $table");
while($row = mysql_fetch_array($result))
{
$id=$row['transaction'];
$date=$row['date'];
$time=$row['time'];
$paid=$row['payment'];
$total=$row['total'];
echo '<style type="text/css">
<!--
.list {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 12px;
color: #000;
padding: 2px;
border: 2px solid #009;
}
.view {
width: 100px;
}
-->
</style>
<div class="list">
<p><span style="color: #900">Transaction #</span>'.$id.'
<span style="color: #900">Date:</span>'.$date.'
<span style="color: #900">Time:</span>'.$time.'<span style="color: #900">
Paid By:</span>'.$paid.' <span style="color: #900">Total:</span>'
.number_format($total, 2).'
<form name="form1" method="post" action="find.php">
<label>
<input type="submit" name="view" id="view" value="'.$id.'">
</label>
</form>
</p>
</div>
<p></p>';
}
}
//view transaction after viewing by month
if (isset($_POST['view'])){
$conn = mysql_connect("localhost", "mss_records", "3205") or die(mysql_error());
mysql_select_db('store_records', $conn) or die(mysql_error());
$table = $_SESSION['table'];
echo "this is the number ".$_POST['view'];
$post=$_POST['view'];
echo "this is the post ".$post;
$result = mysql_query("SELECT * FROM $table WHERE transaction = '$post'")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$items=$row['transaction'];
}
echo $items;
}
?>
after the user goes through the first selection and on the second window the output is...
this is the number 46this is the $post 4646
Your query is mysql_query("SELECT * FROM $table WHERE transaction = '$post'"). Therefore the value of $items=$row['transaction']; is also going to be 46. When you echo out everything without line breaks, it smashes everything together.
POST is not duplicating anything, you are just echoing $items directly after it.
Try this:
$table = $_SESSION['table'];
echo "this is the number ".$_POST['view']."<br /> \n";
$post=$_POST['view'];
echo "this is the post ".$post."<br /> \n";
$result = mysql_query("SELECT * FROM $table WHERE transaction = '$post'")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$items=$row['transaction'];
}
echo $items;
}