PHP code does not get id to update database - php

My code below is not getting the id to update the database.
The id is on the page URL coming from another page's form.
No errors display on screen but my database does not update.
Am i missing something?
<?php
if (isset($_POST['submit'])) {
$id = $_POST["id"];
$product_name = $_POST["product_name"];
$visible = $_POST["visible"];
$query = "UPDATE products SET ";
$query .= "product_name = '{$product_name}', ";
$query .= "visible = {$visible} ";
$query .= "WHERE id = $id ";
$result = mysqli_query($connection, $query);
}
?>

Thanks for your help guys. I found the issue!
As you've said Kiko, I've tried to echo anywhere inside the loop and the problem kept going on.
Now i've changed the code in the beginning and it solved the problem.
Here's the solution:
<?php
if($_GET['id']){
$id = $_GET["id"];
$product_name = $_POST["product_name"];
$visible = $_POST["visible"];
$query = "UPDATE products SET ";
$query .= "product_name = '{$product_name}', ";
$query .= "visible = {$visible} ";
$query .= "WHERE id = $id ";
$result = mysqli_query($connection, $query);
}
?>

Related

Image not showing up

I was trying to make a website where image can be uploaded to the database and be edited on the website like replacing the image. Whenever I try to replace the picture in the website, it gets replaced in the database but it doesn't show up in the website.
This is my PhP code.
if(isset($_POST['update_post'])){
$post_author = $_POST['post_author'];
$post_title = $_POST['post_title'];
// post_category from input
$post_category_id = $_POST['post_category'];
$post_status = $_POST['post_status'];
$post_image = $_FILES['post_image']['name'];
$post_image_temp = $_FILES['post_image']['name'];
$post_content = $_POST['post_content'];
$post_tags = $_POST['post_tags'];
move_uploaded_file($post_image_temp, "../images/$post_image");
if(empty($post_image)) {
$query = "SELECT * FROM posts WHERE post_id = $the_post_id ";
$select_image = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_image)){
$post_image = $row['post_image'];
}
}
$query = "UPDATE posts SET ";
$query .= "post_title = '{$post_title}', ";
$query .= "post_category_id = '{$post_category_id}', ";
$query .= "post_date = now(), ";
$query .= "post_author = '{$post_author}', ";
$query .= "post_status = '{$post_status}', ";
$query .= "post_tags = '{$post_tags}', ";
$query .= "post_content = '{$post_content}', ";
$query .= "post_image = '{$post_image}' ";
$query .= "WHERE post_id = {$the_post_id} ";
$update_query = mysqli_query($connection, $query);
confirm($update_query);
}
This the my database
Database Image
This is my website
Website Image
As you can see in the database, I have the images there but in my website, some pictures are not showing up. I want all of them to show up. Please help

How do i change the URL id?

Hi how do i change the URL id if the id is above max id in the db?
If i get the id from a database and use it in a read more button to make people read the data in a new page with the id as different url query?
--The real question
How do i make sure that if there only are 4 news in the db that if you write etc
newsTest.php?id=5 in the browser the browser will not execute or go back to max page?
//read more button
Læs mere
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages >= $thisVar){
echo "alt er godt";
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;
$result = $dbCon->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_object();
$id = $row->id;
$heading = utf8_encode($row->heading);
$subheading = utf8_encode($row->subheading);
$description = utf8_encode($row->description);
$created = $row->created;
$author = utf8_encode($row->author);
$output .= $id . "<br>" . $heading . "<br>" . $description;
};
// udskriv output til bruger
echo $output;
};
} else {
$_GET['id'] = $total_pages ;
echo "nothing";
}
?>
I am very new to php
Check if total_pages is less than id then show show total_pages id.
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
if(isset($_GET['id']) && !empty($_GET['id'])){
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages < $thisVar){
$thisVar=$total_pages
$id = $thisVar;
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;

MySQL Multi Query store result second query

I am querying from two different tables in the database here and I can store the values from the first query but how do I store the information from the second query?
$query = "SELECT * ";
$query .= "FROM user_account ";
$query .= "WHERE user_id = $user_id ";
$query .= "SELECT * ";
$query .= "FROM user_profile ";
$query .= "WHERE user_id = $user_id ";
if (mysqli_multi_query($mysqli, $query)) {
do {
if ($result = mysqli_store_result($mysqli)) {
while ($row = mysqli_fetch_row($result)) {
$Firstname = $row['Firstname'];
$Lastname = $row['Lastname'];
$Email = $row['Email'];
$Birthday = $row['Birthday'];
$Address = $row['Address'];
$Zip = $row['Zip'];
$City = $row['City'];
$State = $row['State'];
$Country = $row['Country'];
$Avatar = $row['Avatar']; Will be added later
$Phone = $row['Phone'];
$Website = $row['Website'];
$Member_level = $row['Member_level'];
}
mysqli_free_result($result);
}
if (mysqli_more_results($mysqli)) {
}
while (mysqli_next_result($mysqli)) ;
}
}
Just the usual way
$query = "SELECT * FROM user_account WHERE user_id = $user_id ";
$account = $mysqli->query($query)->fetch_assoc();
$query = "SELECT * FROM user_profile WHERE user_id = $user_id ";
$profile = $mysqli->query($query)->fetch_assoc();
as simple as that.
I am really wondering why PHP users are inclined to writing SO MUCH code and to using so much intricate ways for the most trifle operations
On a side note, in your particular case you can write a single JOIN query.
In your code, why are you using mysqli_free_result function because it will frees the memory associated with a result object so when while loop run it will not show any result.
$query = "SELECT * FROM user_account WHERE user_id = $user_id ";
$query .= "SELECT * FROM user_profile WHERE user_id = $user_id ";
if (mysqli_multi_query($mysqli, $query)) {
do {
if ($result = mysqli_store_result($mysqli)) {
while ($row = mysqli_fetch_assoc($result)) {
var_dump($row); //for checking result
// Now use array to show your result
}
} }while (mysqli_next_result($mysqli)) ;
You should always free your result with mysqli_free_result(), when
your result object is not needed anymore.

How to make a filter with condition in PHP MYSQL

how can I add a condition in my SELECT statement where whenever the count is one the one with the concatinated condition will be used. This is my code.
$done = "SELECT * FROM tbl_accepted WHERE status ='done'" ;
if(isset($_POST['search'])){
$search_term = mysqli_real_escape_string($db, $_POST['searchbox']);
$done .= "AND acceptCarOwner = '{$search_term}'" ;
$done .= "AND renter = '{$search_term}'";
}
$donequery = mysqli_query($db, $done);
What I want to happen is when a user search something on one field if the $search_term is not in the AND acceptCarOwner = '{$search_term}' condition it will go to the next condition which is AND renter = '{$search_term}'
You can filter your data by concatenating your coditions. Give it a try.
$where = " `status` = 'done' ";
if(isset($_POST['search'])){
$search_term = mysqli_real_escape_string($db, $_POST['searchbox']);
$where.= " AND `acceptCarOwner` = '{$search_term}'" ;
$where.= " AND `renter` = '{$search_term}'";
}
$done = "SELECT * FROM tbl_accepted WHERE ". $where ;
$donequery = mysqli_query($db, $done);
you use $_POST['search'] and then use $_POST['searchbox'] in next line
$done = "SELECT * FROM tbl_accepted WHERE status ='done'" ;
if(isset($_POST['search'])){
$search_term = mysqli_real_escape_string($db, $_POST['search']);
$done .= "AND acceptCarOwner = '$search_term'" ;
$done .= "AND renter = '$search_term'";
}
$donequery = mysqli_query($db, $done);

How to show multiple items from mySQL database

Hi I am building a blog using html and php and have run into a problem with my sql. In my blog I would like to show all the comments that have been put in by users in the comments section that have the same article ID. In my database I am saving these parameters via $_POST and a query ID, ArticleID, Comments. However only the last comment that has been inserted in the database with that articleID is showing up.
this is the code that I am using. Can anyone help me please?
if(isset($_POST['submit']))
{
$comment = htmlentities($_POST["comment"]);
$articleID = $_GET['artId'];
$query = "INSERT INTO tbl_comments (comment, ArticleID) VALUES ('$comment', $articleID)";
$result = mysqli_query($connection, $query) or die("Error in query: ". mysqli_error($connection));
}
$query1 = "SELECT * FROM tbl_comments WHERE ArticleID = $artId";
$result1 = mysqli_query($connection, $query1) or die("Error in query: ". mysqli_error($connection));
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
if(isset($comment))
{
echo "<div class='comments'>";
if (isset($comment))
{
echo "<div class='commentName'>";
echo $comment;
echo "</div>";
}
change
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
to:
$comment = '';
while($row = mysqli_fetch_assoc($result1))
$comment .= $row['comment'] . '<br/>';

Categories