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

error 7#7: *1 upstream prematurely closed connection while reading response header from upstream
PHP taking user input and formatting output (dates)
how to get the date for promotion from mysql using php / dynamic price
page/2/ Error 404 not custom query
Auto one-to-one model in Laravel
Warning: PHP Startup: Unable to load dynamic library apc.so
Schema describes boolean, string found instead when I create list through Mailchimp API
Braintree PHP SDK cURL exception
Shortcut for echo “<pre>”;print_r($myarray) inside foreach laravel
load default page from sub folder index page using .htaccess php
Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) [duplicate]
How to remove items with certain length from array in php
PHP running multiple scripts concurrently
Laravel can't grab inputs from form to send out email
Find array key where value contains a specific value
Redirecting duplicated pages in WordPress template

Categories

HOME
amazon-web-services
nam
flask
events
shinyapps
xcode8.1
mousemove
initialization
biztalk
header
biztalk-2010
openflow
vue-resource
ontology
bittorrent
supercollider
save
sharepoint-online
roku
hystrix
lc3
yeoman-generator-angular
clish
smartgwt
pushdown-automaton
asp.net-core-1.0
web-deployment
clr
coreldraw
maven-surefire-plugin
amazon-sns
pyopencl
prompt
iwebbrowser2
amazon-cloudtrail
copy-protection
tilemill
windows2012
delayed-job
actframework
android-collapsingtoolbar
nest
assertions
text-decorations
sspi
widestring
grails-spring-security
plane
minikube
rightnow-crm
sony-future-lab-n
nonlinear-functions
watir-webdriver
mura
mongodb-aggregation
annotatorjs
jsf-2.3
zynq
conan
jtag
static-methods
verisign
vimperator
as3-api
openmrs
seadragon
ioctl
ajax4jsf
smart-tv
stack-smash
ruby-on-rails-4.1
srv-record
css-paged-media
wlanapi
uitest
vdm-sl
actionpack
erlog
wicked-gem
python-curses
cakephp-2.2
nesper
visual-c++-2012
ecos
category-theory
android-2.3-gingerbread
sql-function
pageviews
webaii
visual-c++-2005
acitree
objective-c-runtime
ldif
ofstream
chefspec
significant-digits
onconfigurationchanged
stretch
border-box
pcspim
platform-independent
dropshadow
wcf-callbacks
mvcrecaptcha
infopath-2007
drawimage
net-use
mediacontroller
django-pagination
suphp
fxruby
ning

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App