Warning: Invalid argument supplied for foreach() HTML embedded - php

I have made a basic search engine and I try, to fetch the results, on the same page; moreover, the results have been retrieved and stored in the associative array, but the embedded code in HTML shows only one record, of the results. `
<?php
require('Configuration/config.php');
require('Configuration/db.php');
//If the user clicks, on the button search, the execute the query
if (isset($_POST['search_btn'])) {
$search_query = $_POST['search'];
//$search_query = htmlspecialchars($_POST['search']);
//Create the query.
$query = "SELECT * FROM The_primary_arkivum WHERE
Name = '$search_query' OR
Address = '$search_query' OR
Category = '$search_query' OR
Country = '$search_query' OR
State = '$search_query'";
//Get the results.
$results = mysqli_query($conn, $query);
//Fetch the data, of the result, to an array.
$search_results = mysqli_fetch_all($results, MYSQLI_ASSOC);
//var_dump($search_results);
//var_dump($search_query);
//Free result
mysqli_free_result($results);
//Close the connection
mysqli_close($conn);
}
?>
<?php include('included/header.php'); ?>
<body>
<div class = "header">
<h2>Search</h2>
</div>
<form method="post" action="search_index.php">
<div class="input-group">
<label>Search</label>
<input type="text" name="search" value="<?php echo $search; ?>">
</div>
<div class="input-group">
<button type="submit" class="btn" name="search_btn">Search</button>
</div>
<?php foreach($search_results as $search_result) : ?>
<div class="mySlides fade">
<?php echo $search_result['Name']?>
<?php echo $search_result['Address']?>
<?php echo $search_result['Country']?>
</div>
<?php endforeach; ?>
</form>
<?php include('included/footer.php'); ?>
`

Your echo statements in your form do not have ending semicolons ;. Try starting there.
<?php echo $search_result['Name'];?>
<?php echo $search_result['Address'];?>
<?php echo $search_result['Country'];?>

Related

i want to add commenting to each article, so inside my"foreach"cycle I added commenting to each article, but "set a comment" function runs to all art

i want to add commenting to each article, so inside my"foreach"cycle I added commenting to each article, but "set a comment" function runs to all art
thats the code for making an article window
<?php $articles_qr = mysqli_query($connection, "SELECT * FROM `articles` ");
$articles = array();
while ( $art = mysqli_fetch_assoc($articles_qr))
{
$articles[] = $art;
}
?>
<?php foreach ($articles as $art)
{
?>
<section>
<div class="containerstuff">
<div class="stuffpic">
<img src="<?php echo "../static/imagespages/",$art['image'] ?>" class="pico">
</div>
<div class="article">
<h1><?php
echo $art['title']
?>
</h1>
<?php
echo $art['text'];
echo $art['id']
?>
</div>
</div>
<div class="scrollmenu">
<?php include "../includes/comments.php";?>
</section>
<?php
} ?>
thats the code comments window
<?php
date_default_timezone_set(timezone_identifier);
include_once '../comments.ink.php'
?>
<div class="containercom">
<img src="#" class="commpic">
<p class="comment"></p>
</div>
<div class="blockcom">
<form class='form' method='POST' action="<?php echo setComments($connection)?>">
<div class='form__group'>
<input type='hidden' name='page_id' value="<?php echo $art['id']?>" >
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='pubdate' value="<?php echo date('Y-m-d H:i:s')?>" >
<textarea name='text' class='form__control' placeholder ='Введите комментарий...' required=''></textarea>
</div>
<div class='form__group'>
<input type='submit' class='form__control2' name='commentSubmit'></input>
</div>
</div>
</form>
and thats the code for INSERT function
<?php
static $firstTime = true;
function setComments($connection) {
if(isset($_POST['commentSubmit'])){
$idcom = $_POST['page_id'];
$uid = $_POST['uid'];
$pubdate = $_POST['pubdate'];
$text = $_POST['text'];
$sql =
"INSERT INTO `comments` (page_id, uid, pubdate, text)
VALUES ('$idcom', '$uid', '$pubdate', '$text')";
$result = $connection->query($sql);
$firstTime = false;
}
}
so how can i make insert only for 1 article (so when i add it now, there are appears as many comments as many articles i have in database)
I think you should use ajax to append a new comment which is the widely used solution, the way u r doing will become difficult to handle for you.
I haven't found the solution, so I just selected another way.
For each article I placed a button to post a comment which will send user to the page with this article and for this button for "href" I wrote php code (href = "comments.php?id=<?php echo $art['id']"?>) and for this page I use $_GET to select articles only for this id. Then I just placed there comments-function that I wrote so it works alright now because function works only for 1 argument

select query inside in select query php mysql

<?php
$query = "select * from comments t1
inner join users t2 on t1.user_id = t2.UserId
where usercomplain_id='$id'";
$run =mysqli_query($mysqli,$query);
while($row=mysqli_fetch_array($run))
{
$commentid = $row['comment_id'];
$comment = $row['comment'];
$username = $row['UserName'];
$userid1 = $row['UserId'];
$date = $row['CDate'];
$ageDate = time_elapsed_string($date);
?>
<div class="jumbotron" style="border:3px solid #2FAB9B; background-color:#68C8C6;">
<div class="row">
<div class="col-md-10">
<?php echo $comment; ?>
</div>
<div class="col-md-2">
<?php echo $ageDate; ?>
</div>
</div>
<br>
<label>Comment by <?php echo $username; ?></span></label><br>
<h5><b>Reply on this post</b></h5>
<?php
$query = "select * from Reply";
$run = mysqli_query($mysqli,$query);
?>
<a class="reply" data-role="<?php echo $commentid; ?>">Reply</a>
<br>
<br>
<div style="width:63%; display:none;" class="replyForm" data-role="<?php echo $commentid; ?>">
<form method="post">
<textarea name="comment[<?php echo $commentid; ?>]" cols="100" rows="4"></textarea><br>
<br>
<input type="submit" name="reply" class="btn btn-primary" style="float:right" value="reply">
</form>
</div>
</div>
<script>
It is a simple comment system in which after each comment I want to display replies on that particular comment using select inside a select query is returning only first record is there is any method to display those reply
Your second query, which in inside the while loop, is over writing the result set of the first as both use the handle $run
This one
<?php
$query = "select * from Reply";
$run = mysqli_query($mysqli,$query);
?>
Not quite sure if you even use the result of this query actually
But if you do change $run to say... $run1 and at least it will not destroy $run while still inside the while loop that is using $run.

can i use while loop inside another while loop in php

I'm creating a comment system in my project. I want each posted question to have a comment. I was able to post the comment, but I am having a problem displaying all comment to it's respective answers. I was able to display only one row but not the row of the comment. I try to use a while loop nested inside the while that echo's each question, but it hangs. When I use if it only displays the first row of the comment of each question.
So my question is how can I display them all?
<div class="answer">
<?php
include 'db.php';
$sql = "select * from answers where questionrid IN(select id from question where id='$qid')";
$result = mysqli_query($con,$sql);
if($result){
while($ro = mysqli_fetch_assoc($result)){
?>
<div class="a_view">
<pre>
<?php
echo $ro["answer"];
?>
</pre>
</div>
<div class="ans_comment">
<?php
if($ro["id"]){
$id = $ro["id"];
$sqli = "SELECT * FROM comments WHERE answerid='$id'";
$query = mysqli_query($con,$sqli);
$row = mysqli_fetch_assoc($query);
$num = mysqli_num_rows($query);
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
}
?>
</div>
<div class="add"><div class="coment">add a comment</div> <div id="coment">
<form class="cform" method="post" action="acomment.php">
<textarea type="text" name="comment" class="tcomment" placeholder="add your comment here,your is
required to give correction or more information about the problem"></textarea><br><br>
<input type="hidden" value="<?php echo $ro["id"]; ?>" name="userid">
<input type="submit" value="Post your comment">
</form>
</div></div>
<?php
}
}else{
echo "no record";
}
?>
<?php
$con->close();
?>
This is the section that made it hang
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
when I use if, it only echos one row.
Instead of
while($row){
do it just like you're doing in the while loop above
while($row = mysqli_fetch_assoc($query)){
The way you have it now, $row is never changing, and therefore always evaluates to true, leaving you stuck in your loop.

why Isn't my form working

I'm trying to use a listbox form to query the database but it's not showing anything. The idea is that I've queried the database to fill the form with the names of suburbs and then, selecting a suburb will query the database again to return the names of parks in that suburb. When I use the search form it doesn't return anything.
this is the form:
<p>Select Suburb to search</p>
<form method="post" action="suburb_search.php" id="search">
<select>
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
<input type="submit" name="search" value="Search" />
</form>
</div>
This is where it should use the results of the form to query the database but its not working:
<?php
$searchRequest = False;
if (isset($_GET['suburb'])){
$search = $_GET['suburb'];
$sql2 = "SELECT * FROM park_list WHERE suburb=$search";
$result2 = $db->query($sql2);
if($message){
echo "<p>$message</>";
} else {
?>
<div class="form">
<?php
while ($row2 = $result2->fetch_assoc()){
?>
<div class="results">
<h2><?php echo $row2['park_name'];?></h2>
<?php
}
}
} ?>
give a name for your select element as
<select name="suburb">
<?php while ($row = $result->fetch_assoc()) { ?>
<option value="suburb"> <?php echo $row['suburb']?></option>
<?php }
} ?>
</select>
and you are giving form method as POST but accepting data in GET in your php code, change this
if (isset($_GET['suburb'])){
to
if (isset($_POST['suburb'])){

PHP search with PDO

I am trying to convert search function from one tutorial from mysql into PDO. I need to search inside of my table. With my limited knowledge I was able to do this. However, there are still some things I cannot resolve. What I am missing?
When searching no matter what I search I still get no result.
ORIGINAL
if(isset($_GET['keywords'])){
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query(
"SELECT name, description
FROM cars
WHERE name LIKE '%{$keywords}%' OR description LIKE '%{$keywords}%'
");
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results.
</div>
<?php
if($query->num_rows){
while ($r = $query->fetch_object()) {
?>
<div class="result">
<?php echo $r->name; ?>
</div>
<?php
}
}
}
?>
PDO version/my version
<?php
include 'inc/header.php';
if(isset($_GET['keywords'])){
$keywords = $_GET['keywords'];
$cars = $db->prepare("SELECT name, description FROM cars WHERE name LIKE '%{$keywords}%' OR description LIKE '%{$keywords}%'
");
$cars->execute();
?>
<div class="result-count">
Found <?php echo $cars->rowCount(); ?> results.
</div>
<?php
if($cars->rowCount()){
while($r = $cars->fetchObject()){
?>
<div class="result">
<?php echo $r->name; ?>
</div>
<?php
}
}
}
?>
Html
<form action="search.php" method="get">
<label>
<input type="text" name="keywords" value="SEARCH" autocomplete="off">
</label>
<input type="submit" value="Search">
</form>

Categories