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

Twig loop for nested comment in a Symfony-Doctrine blog
Using wildcard * between words in ldap_search
Invalid Configuration – yii\base\InvalidConfigException The directory is not writable by the Web process?
Laravel Schema::drop($table) return value
How to use multi Auth in laravel 5.2 [closed]
passing variable to another page for sql query
Regular expression for adding character at the end if not found
Laravel secure action function
Is this basic file upload check secure enough?
Non-static method error in PHP PEAR Email
Laravel post JSON using CSRF Protection
TTFB too high on one computer, not on another
Get multiple substrings from a string in PHP [closed]
PHP - Page needs Twice a Submit to show Changes [duplicate]
How do I combine these two queries to calculate rank change?
How can I limit each user to just one like per page?

Categories

HOME
jboss
mod-rewrite
plesk
yaml
ontouchlistener
exchange-server
redmine
writefile
kivy
filesize
hdfs
theano
websocket
wagtail
bellman-ford
hystrix
lda
locationmanager
closures
rpmbuild
susy-sass
codefluent
renderscript
multi-upload
mod-wsgi
tracing
introspection
ipa
jsonresult
xenapp
infopath2010
windows2012
cucumber-junit
cloudera-manager
orchardcms-1.8
jfxtras
xvfb
cheat-engine
sspi
best-buy-api
resampling
data-uri
directwrite
cloudwatch
ol3-google-maps
renderman
watir-webdriver
business-rules
openerp-6
mifos
elastic4s
tablespace
always-on-top
uibinder
rails-engines
android-async-http
textblock
stack-smash
apache-commons-digester
gitlab-omnibus
featuretoggle
espresso
modern.ie
prism-4
python-curses
phonegap-facebook-plugin
n-tier-architecture
encryption-symmetric
symja
dnsjava
nesper
libgcc
code-complexity
redpitaya
jmenubar
xsd2code
multiple-dispatch
xcode4.5
popup-blocker
prefuse
boost-foreach
haiku
getopts
servlet-container
django-pagination
turbine
mygeneration
jdownloader
multiple-users
getimagesize
rational-unified-process
yahoo-maps
efs
watchpoint
gui-designer
rendering-engine
jvm-bytecode
synthesizer

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App