php


my while loop is in php its too much its out of control


This is my code in which my while is going out of order and I don't know what to do with it.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form action="process.php" method="post">
<input type="Search" name="search">
<br>
<input type="submit" name="submit">
</form>
<?php
$search_result = $_GET['result'];
while($search_result)
echo $search_result;
?>
</body>
And the problem is that the above while loop is iterating for the whole life and I can not stop it. What's the matter with it.
process.php
<?php
$conn = new mysqli('localhost', 'root', '', 'posts');
if(isset($_POST['submit']))
{
$search = htmlentities($_POST['search']);
$query = $conn->query("SELECT * FROM posts WHERE heading LIKE'%".$search."%'");
while ($row = $query->fetch_assoc()) {
$search1 = $row['heading'];
header("location:index.php?result='".$search1."'");
}
} else {
header('location:index.php');
}
?>
There are some problems, but you're actually pretty close to getting this working. Others have mentioned the SQL injection vulnerability in the comment, and it's a valid concern that you should address, but I'll just focus on the logic errors in this answer. There are really just two main problems.
First, you aren't getting all of your data in process.php.
Here, you're iterating your query results, but you're only going to get a header with the value of the last one.
while ($row = $query->fetch_assoc()) {
$search1 = $row['heading'];
header("location:index.php?result='".$search1."'"); // overwrites the location header
}
You need to accumulate the headings during the loop and call header once after the loop.
while ($row = $query->fetch_assoc()) {
$result['headings'][] = $row['heading']; // append the row value to an array instead
}
header('Location: index.php?' . http_build_query($result));
(http_build_query will convert the array to a string you can use in the URL.)
Second, you're using the wrong kind of loop in index.php.
Back in index.php, instead of
while($search_result) echo $search_result;
You can use a foreach loop (after checking that the array you're looking for really exists).
if (isset($_GET['headings'])) {
foreach($_GET['headings'] as $heading) {
echo $heading;
}
}
while($search_result) would only work if you were popping items off of $search_result until it became empty, or if you had a break based on some predictable condition.
Be careful not to try to pass too much data in the URL like this. There is a limit to how much it can take. If you're fetching much data, it would probably be better to include process.php and output the query results in the same page rather than submitting your form to process.php and redirecting back to index.php with the results in the query string.

Related Links

How to make my CMS multilingual
how to Write a query to fetch data as follows in mysql?
MySQL - How to run multiple update queries in one line?
how to send data onClick() to another php for processing using post or get?
using php to upload mp3 file to a directory on server and store path of the uploaded file in mysql database
How to overwrite Woocommerce template using Avada Child Theme
how to mock parent::xxx method
Beanstalkd failover for queues in Laravel
Wordpress term list of a post
Inserting one input value from two database column name
Google API Server to Server Authentication returns an empty array?
OOP PHP: Is it okay to store configuration strings in constants? [closed]
setBackgroundColor doesn't work (jpg to png)
PHP: Match the final parethesized word in a string?
Can't Find PHP Script as Running Process on Linux Operating System?
show value of current friday in textbox

Categories

HOME
jboss
entity-framework
svn
class
matrix
applescript
scroll
activex
activemq
vsts-build
malware
gentelella
google-search-console
anchor
hystrix
grocery-crud
angular4
cuba-platform
squid
pywinauto
exe
fosuserbundle
google-sites
robolectric
opam
jitsi
liferay-6.2
batch-rename
restier
high-availability
webviewclient
beego
hdmi
xdebug
sap-lumira
matlab-app-designer
testlink
aurelia-cli
unification
extbase
flowchart
grunt-contrib-watch
data-uri
icepdf
fluid-mac-app-engine
latexml
permission-denied
moinmoin
nomad
doctrine-extensions
linq-to-excel
absolute
wysihtml5
karabiner
visual-format-language
sonatype
mobile-angular-ui
dex
rcharts
business-rules
tablespace
gridbaglayout
voronoi
pack
helm
perceptron
largenumber
ora-04091
pythonxy
ngcordova
clarion
text-align
createfile
microsoft-expression-web
squeezebox
communicate
webautomation
thruway
profiles
typesafe-stack
strdup
application-blocks
jmenubar
jquery-transit
node.js-stream
reentrancy
office-app
gprof
instance-variables
sfinae
telerik-ajax
server-name
qtextbrowser
drawimage
will-paginate
firefly-mv
servlet-container
ios-4.2
ppc
wmd-editor
external-sorting
bindable-linq
webclient.uploaddata

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile