select query inside in select query php mysql - php

<?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.

Related

How do I delete specified address row?

My add_id is a primary key. I have displayed all the addresses from the same cus_id but with different add_id. I want to delete a specified row of address but when I press the delete button, the page refresh but no data was deleted. Please look at my codes, thank you.
These are the codes involved, and my db:
<?php
$cus_id = $_SESSION['id'];
//To show all the addresses with the same cus_id
$result2 = mysqli_query($connect, "SELECT * FROM customer_address WHERE cus_id='$cus_id'");
?>
<?php
if (isset($_GET['del'])) {
$add_id = $_GET["id"];
mysqli_query($connect, "DELETE FROM customer_address WHERE add_id='$add_id'");
}
?>
<?php
while ($row1 = mysqli_fetch_assoc($result2)) {
?>
<div class="addrow">
<div class="add_box">
<p id="name_row"><?php echo $row1['name']; ?> </p>
<p id="phone_row"><?php echo $row1['contact']; ?> </p>
<p id="add_row"><?php echo $row1['address']; ?></p>
</div>
<div class="btn_box">
<input type="button" name="editbtn" class="editbtn" value="Edit">
<input type="button" name="deletebtn" class="deletebtn" value="Delete">
<input type="button" name="defaultbtn" class="defaultbtn" value="Set As Default">
</div>
</div>
<?php
}
?>
<?php
if (isset($_GET['del'])) {
$add_id = $_GET["id"];
mysqli_query($connect, "DELETE FROM customer_address WHERE add_id='$add_id'");
echo ("<script>location.href = 'cus_address.php?msg=$msg';</script>");
}
?>
Firstly, you need to change from '$cus_id' to '".$cus_id."' because $cus_id is parameter.
<?php
$cus_id = $_SESSION['id'];
//To show all the addresses with the same cus_id
$sql = "SELECT * FROM customer_address WHERE cus_id='".$cus_id."'";
$result2 = mysqli_query($connect, $sql);
?>
When delete data, you need to add both cust_id and add_id on query follow as below:
<?php
if (isset($_GET['del']))
{
$cust_id = $_GET["id"];
$add_id = $_GET["add_id"];
mysqli_query($conn, "DELETE FROM customer_address WHERE add_id='".$add_id."' and cus_id='".$cust_id."'");
}
?>
Next, check data exist before looping and add "&add_id="
<?php
if(mysqli_num_rows($result2) > 0)
{
while($row1 = mysqli_fetch_assoc($result2))
{
?>
<div class="addrow">
<div class="add_box">
<p id="name_row"><?php echo $row1["name"]; ?> </p>
<p id="phone_row"><?php echo $row1["contact"]; ?> </p>
<p id="add_row"><?php echo $row1["address"]; ?></p>
</div>
<div class="btn_box">
<input type="button" name="editbtn" class="editbtn" value="Edit">
<input type="button" name="deletebtn" class="deletebtn" value="Delete">
<input type="button" name="defaultbtn" class="defaultbtn" value="Set As Default">
</div>
</div>
<?php
}
}else{
//Display... when no data have been found
}
?>

Display unlimited row by random ID selected by user (created by php)

I need to display row from different table with random id that what i make it php generate it when i add new car or new driver
For example :
In this image table record's for same driver and different Car
and in this image the records for random id for the car
I need to show every records by date for same driver and Which car he use
Note: In this case its show like this
and I need it show like this
and sorry for arabic database records tell me if there anything not understood
my code:
<?php
require 'config.php';
$query = "SELECT * FROM `driversuser`";
$result1 = mysqli_query($con, $query);
?>
<div class="container">
<div class="addingcar">
<form dir="rtl" action="#" method="post">
<div class="">
<b>عرض وردية سائق</b>
</div>
<br/>
<label>
<b>اختر السائق</b>
<select name="insdname" id="framework" class="align-right selectpicker" data-live-search="true">
<option style="text-align: right" value="">اختر السائق ...</option>
<?php while($row1 = mysqli_fetch_array($result1)):; ?>
<option style="text-align: right" value="<?php echo $row1[10]; ?>"><?php echo $row1[1]; ?></option>
<?php endwhile; ?>
</select>
</label>
<br />
<label>
<b>من</b>
<input type="date" name="datefrom">
</label>
<br />
<label>
<b>الي</b>
<input type="date" name="dateto">
</label>
<br /><br />
<input type="hidden" name="hidden_framework" id="hidden_framework" />
<input type="submit" name="submit" class="btn btn-info" value="عــــرض" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['insdname'];
$date_val = $_POST['datefrom'];
$dateto_val = $_POST['dateto'];
$report = mysqli_query($con, "SELECT * FROM `reports` WHERE Datefrom BETWEEN '$date_val' AND '$dateto_val' AND DriverCode = '$selected_val' ORDER BY Datefrom");
while ($datareport = mysqli_fetch_array($report)) {
echo $datareport['Datefrom']." ".$datareport['DateTo']." ".$datareport['Price']." ".$datareport['PriceTaken']." ";
$report2 = mysqli_query($con, "SELECT * FROM `reports` WHERE DriverCode = '$selected_val' GROUP BY CarCode");
while ($datareport2 = mysqli_fetch_array($report2)) {
$carcode = $datareport2['CarCode'];
}
$report3 = mysqli_query($con, "SELECT * FROM `cars` WHERE Car_ID = '$carcode'");
while ($datareport3 = mysqli_fetch_array($report3)) {
echo $datareport3['CarName']." ".$datareport3['CarModel']." ".$datareport3['CarNumber']." ".$datareport3['CarColor']." ";
echo '<br/>';
}};
}
?>
</div>
</div>
I don't have your database schema, but I tried to create one for testing. So, try this:
SELECT * FROM reports r1 INNER JOIN cars c1 ON r1.carcode = c1.car_id ORDER BY r1.date
r1 is a shortcut for reports table ( change it to whatever you like ).
c1 is a shortcut for cars table ( also, you can change it ).
You can read more about INNER JOIN.

Warning: Invalid argument supplied for foreach() HTML embedded

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'];?>

update multiple columns in mysql database using $_GET

I want to update 3 fields in a row in 3 columns but I don't know how to do it. I already searched google and searcedh here but couldn't find any solution for it. I want to change title, paragraph and category of a blog post using $_GET using this way:
<?php
$id = $_GET['id'];
?>
<div class="middle">
<div class="content" style="width:100%;">
<div class="context" style="width:100%">
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$txt = $_POST['txt'];
$query = ("UPDATE tbl_post SET title='$title' WHERE id=$id");
$query = ("UPDATE tbl_post SET txt='$txt' WHERE id=$id");
when I use only one of $_title or $_txt, it works. But I couldn't find a way to update both fields together and couldnt update category selection.
full code of update.php page :
<?php require_once("config.php"); ?>
<?php require_once("header.php"); ?>
<?php
$id = $_GET['id'];
?>
<div class="middle">
<div class="content" style="width:100%;">
<div class="context" style="width:100%">
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$txt = $_POST['txt'];
$query = ("UPDATE tbl_post SET title='$title' WHERE id=$id");
$query = ("UPDATE tbl_post SET txt='$txt' WHERE id=$id");
$query = ("UPDATE tbl_post SET cat='$cat' WHERE id=$id");
mysql_query($query,$con);
header("location:insert.php");
exit();
}
?>
<form action="" method="post">
<?php
$id = $_GET['id'];
$query = "SELECT * FROM `tbl_post` WHERE(id=$id)";
$res = mysql_query($query,$con);
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
?>
<p>عنوان مطلب</p>
<input type="text" name="title" style="width:200px; border:1px solid #8C8C8C" value="<?php echo $rows['title'] ?>">
<p>محتوای پست</p>
<textarea name="txt" style="width:300px"><?php echo $rows['txt'] ?></textarea>
<div class="clear"></div>
<?php } ?>
<p>دسته بندی</p>
<select name="cat" style="width:200px">
<?php
$query = "SELECT * FROM `tbl_cat` ORDER BY `id` ASC";
$res = mysql_query($query,$con);
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
?>
<option value="<?php echo $rows ['id'] ?>"><?php echo $rows ['name'] ?></option>
</li>
<?php } ?>
</select>
<input type="submit" name="submit" class="" value="ثبت در دیتابیس" style="width:200px; margin-top:15px;">
</form>
</div>
</div>
</div>
<?php require_once("footer.php"); ?>
and insert.php :
<?php require_once("config.php"); ?>
<?php require_once("header.php"); ?>
<div class="middle">
<div class="content" style="width:100%;">
<div class="context" style="width:100%">
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$cat = $_POST['cat'];
$txt = $_POST['txt'];
echo 'title = '.$title.'<br>'.'category ='.$cat.'<br>'.'txt = '.$txt;
$query = "INSERT INTO tbl_post(`title`,`txt`,`cat_id`) VALUES ('$title','$txt','$cat')";
mysql_query($query,$con);
header("location:insert.php");
exit();
}
?>
<form action="" method="post">
<p>عنوان مطلب</p>
<input type="text" name="title" style="width:200px; border:1px solid #8C8C8C;">
<p>دسته بندی</p>
<select name="cat" style="width:200px">
<?php
$query = "SELECT * FROM `tbl_cat` ORDER BY `id` ASC";
$res = mysql_query($query,$con);
while($rows = mysql_fetch_array($res,MYSQL_ASSOC)){
?>
<option value="<?php echo $rows ['id'] ?>"><?php echo $rows ['name'] ?></option>
</li>
<?php } ?>
</select>
<p>محتوای پست</p>
<textarea name="txt" style="width:300px"></textarea>
<div class="clear"></div>
<input type="submit" name="submit" class="" value="ثبت در دیتابیس" style="width:200px; margin-top:15px;">
</form>
</div>
</div>
</div>
<?php require_once("footer.php"); ?>
Combine all the fields into a single query:
$title = $_POST['title'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];
$query = "UPDATE tbl_post SET title='$title', txt = '$txt', cat = '$cat' WHERE id = $id";
Also, you should switch to parametrized queries instead of substituting into the SQL; this means using PDO or mysqli. Otherwise you need to escape the input data. See
How can I prevent SQL injection in PHP?

Update post from database

I'm trying to get my post to update just in case I make a mistake the first time around posting an article to my website.
Not sure what I'm doing wrong here.
Here is my update code:
<div class="row">
<?php
$post_title = "";
$description = "";
$id = $_GET['id'];
$result = mysql_query("SELECT title, description FROM htp_news WHERE id='$id'");
$post_title = mysql_result($result,0,"title");
$description = mysql_result($result,0,"description");
?>
<div class="row">
<form method="post" action="update-news.php">
<input type="hidden" name="ud_id" style="width: 100%" value="<? echo "$id"; ?>">
<div class="grid_12 botspacer60">
Title: <input type="text" name="ud_title" value="<?php echo "$post_title"; ?>">
<br /><br />
News Details:<br />
<textarea id="tiny_mce" name="ud_description" rows="8"><?php echo "$description"; ?></textarea>
</div>
<div class="grid_12">
<input type="submit" value="Update">
<input type="button" value="Cancel" onclick="window.location = '/admin'">
</div>
</form>
</div>
</div>
And here is my action page:
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/includes/database.php");
$ud_id = $_POST['ud_id'];
$ud_title = $_POST['ud_title'];
$ud_description = $_POST['ud_description'];
// Insert record into database by executing the following query:
$query="UPDATE htp_news SET title='$ud_title', description='$ud_description' "."WHERE id='$ud_id'";
mysql_query($query);
echo "The post has been updated.<br />
<a href='edit-delete-news.php'>Update another position.</a><br />";
mysql_close();
?>
I appreciate any guidance on the matter.
Add a space before of WHERE Clause in query.
Use below -
$query="UPDATE htp_news SET title='$ud_title', description='$ud_description' WHERE id='$ud_id'";
Try this you need quotes in query
$result = mysql_query("SELECT `title`, `description` FROM `htp_news` WHERE id='$id'");
$query="UPDATE htp_news SET `title`='".$ud_title."', `description`='".$ud_description."' "." WHERE `id`='".$ud_id."'";

Categories