Multiple query don't get executed - php

I have a multiple query that doesn't work. When I press submit on the former page, I get to a blank page and nothing is being inserted in the table "answer_det" in my database and the text "Information stored successfully" doesn't appear. What do I do wrong?
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
if(mysqli_connect_errno())
{
echo mysqli_connect_error();
}
$pid5 = $_POST['pid4'];
$aid1 = $_POST['a1'];
$aid2 = $_POST['a2'];
$aid3 = $_POST['a3'];
$aid4 = $_POST['a4'];
$answ1 = $_POST['ans1'];
$answ2 = $_POST['ans2'];
$answ3 = $_POST['ans3'];
$answ4 = $_POST['ans4'];
$point1 = $_POST['pointset1'];
$point2 = $_POST['pointset2'];
$point3 = $_POST['pointset3'];
$point4 = $_POST['pointset4'];
$que = "INSERT INTO answer_det VALUES('$pid5','','$aid1','$answ1','$point1')";
$que .= "INSERT INTO answer_det VALUES('$pid5','','$aid2','$answ2','$point2')";
$que .= "INSERT INTO answer_det VALUES('$pid5','','$aid3','$answ3','$point3')";
$que .= "INSERT INTO answer_det VALUES('$pid5','','$aid4','$answ4','$point4')";
$run = mysqli_multi_query($mysqli,$que);
if($run)
{
echo "<br>Information stored successfully";
}
else
{
echo mysql_error();
}
?>

In case you just need to insert these data, there is no need for multiple query, Use this for running your code as one query:
$que = "INSERT INTO answer_det VALUES('$pid5','','$aid1','$answ1','$point1'), ";
$que .= "('$pid5','','$aid2','$answ2','$point2'), ";
$que .= "('$pid5','','$aid3','$answ3','$point3'), ";
$que .= "('$pid5','','$aid4','$answ4','$point4');";
$run = mysqli_query($mysqli, $que);

In addition to my comment (added semicolons):
$que = "INSERT INTO answer_det VALUES('$pid5','','$aid1','$answ1','$point1');";
$que .= "INSERT INTO answer_det VALUES('$pid5','','$aid2','$answ2','$point2');";
$que .= "INSERT INTO answer_det VALUES('$pid5','','$aid3','$answ3','$point3');";
$que .= "INSERT INTO answer_det VALUES('$pid5','','$aid4','$answ4','$point4');";
$run = mysqli_multi_query($mysqli,$que);
See the PHP manual where it clearly reads:
Executes one or multiple queries which are concatenated by a
semicolon.

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

PHP code does not get id to update database

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);
}
?>

Update form with two file fields

I cannot get my two file upload fields working with my update form. I'm able to get create_form to upload the files to my server and input info into the SQL database, but I can't get the edit to take without receiving an error. Files don't upload and info doesn't update in SQL. Please help!
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php
session_start();
if($_SESSION["login_user"] != true) {
echo("Access denied!");
exit();
}
?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/validation_functions.php");?>
<?php find_selected_event_page(); ?>
<?php
if (!$current_event) {
// page ID was missing or invalid or
// page couldn't be found in database
redirect_to("manage_content.php");
}
?>
<?php
if (isset($_POST['submit'])) {
// Process the form
// validations
$required_fields = array("visible");
validate_presences($required_fields);
if (empty($errors)) {
// Perform Update
$id = $current_event["id"];
$visible = mysql_prep($_POST["visible"]);
$homepage = mysql_prep($_POST["homepage"]);
$fa_id = mysql_prep($_POST["fa_id"]);
$title = mysql_prep($_POST["title"]);
$caption = mysql_prep($_POST["caption"]);
$url = mysql_prep($_POST["url"]);
$month = mysql_prep($_POST["month"]);
$date = mysql_prep($_POST["date"]);
$year = mysql_prep($_POST["year"]);
$summary = mysql_prep($_POST["summary"]);
$full_text = mysql_prep($_POST["full_text"]);
$image = rand(1000,100000)."-".$_FILES['image']['name'];
$image_loc = $_FILES['image']['tmp_name'];
$image_size = $_FILES['image']['size'];
$image_type = $_FILES['image']['type'];
$image_folder="images/";
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$file_folder="files/";
$final_image=str_replace(' ','-',$new_image_name);
$final_file=str_replace(' ','-',$new_file_name);
if($_FILES) {
unlink("images/".$current_event['image']);
move_uploaded_file($image_loc,$image_folder.$final_image);
unlink("files/".$current_event['file']);
move_uploaded_file($file_loc,$file_folder.$final_file); }
else
{
// if no image selected the old image remain as it is.
$final_image = $current_event['image']; // old image from database
$fine_file = $current_event['file']; // old image from database
}
$query = "UPDATE `events` SET ";
$query .= "`visible` = '{$visible}', ";
$query .= "`homepage` = '{$homepage}', ";
$query .= "`fa_id` = '{$fa_id}', ";
$query .= "`title` = '{$title}', ";
$query .= "`caption` = '{$caption}', ";
$query .= "`url` = '{$url}', ";
$query .= "`month` = '{$month}', ";
$query .= "`date` = '{$date}', ";
$query .= "`year` = '{$year}', ";
$query .= "`summary` = '{$summary}', ";
$query .= "`full_text` = '{$full_text}', ";
$query .= "`image` = '{$final_image}', ";
$query .= "`image_type` = '{$image_type}', ";
$query .= "`image_size` = '{$image_new_size}' ";
$query .= "`file` = '{$final_file}', ";
$query .= "`file_type` = '{$file_type}', ";
$query .= "`file_size` = '{$file_new_size}' ";
$query .= "WHERE `events`.`id` = {$id} ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection)) {
// Success
echo "<pre>".$query."</pre>";
$_SESSION["message"] = "Item updated.";
redirect_to("manage_content.php");
} else {
// Failure
//$_SESSION["message"] = "Item creation failed.";
//redirect_to("new_news.php");
echo "Error: " . $query . "<br>" . $result->error;
}
}
} else {
// This is probably a GET request
} // end: if (isset($_POST['submit']))
?>
The error I get is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'file = '', file_type = '', file_size = '' WHERE events.id = 1 LIMIT 1' at line 1
update
$query .= "`image_size` = '{$image_new_size}' ";
to
$query .= "`image_size` = '{$image_new_size}' ,";
so your final query
$query = "UPDATE `events` SET ";
$query .= "`visible` = '{$visible}', ";
$query .= "`homepage` = '{$homepage}', ";
$query .= "`fa_id` = '{$fa_id}', ";
$query .= "`title` = '{$title}', ";
$query .= "`caption` = '{$caption}', ";
$query .= "`url` = '{$url}', ";
$query .= "`month` = '{$month}', ";
$query .= "`date` = '{$date}', ";
$query .= "`year` = '{$year}', ";
$query .= "`summary` = '{$summary}', ";
$query .= "`full_text` = '{$full_text}', ";
$query .= "`image` = '{$final_image}', ";
$query .= "`image_type` = '{$image_type}', ";
$query .= "`image_size` = '{$image_new_size}', ";
$query .= "`file` = '{$final_file}', ";
$query .= "`file_type` = '{$file_type}', ";
$query .= "`file_size` = '{$file_new_size}' ";
$query .= "WHERE `events`.`id` = {$id} ";
$query .= "LIMIT 1";

executing a SELECT query and check if any row is returned and if empty resultset do an INSERT

been following this php/mysql tutorial i found online and now i can't get this query to work. The issue i believe is with using the insert query after null resultset is returned from the select query.
$new_start_no = isset($_POST['start_no']) ? $_POST['start_no'] : '';
$new_end_no = isset($_POST['end_no']) ? $_POST['end_no'] : '';
$new_job_no = isset($_POST['job_no']) ? $_POST['job_no'] : '';
$query = "SELECT COUNT (*)";
$query .= "FROM jobs";
$query .= "WHERE job_no=$new_job_no";
$query .= "AND start_no <= $new_end_no";
$query .= "AND end_no >= $new_start_no";
$result = mysqli_query($connection, $query);
$num_rows = mysqli_num_rows($result);
if (is_null) {
$query = "INSERT INTO jobs (";
$query .= " user_id, start_no, end_no, start_date, client_name, card_type, job_no, job_quantity, end_date, shred_option";
$query .= ") VALUES (";
$query .= " '{$user_id}', '{$start_no}', '{$end_no}', '{$start_date}', '{$client_name}', '{$card_type}', '{$job_no}', '{$job_quantity}', '{$end_date}', '{$shred_option}'";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result) {
// success
$_SESSION["message"] = "job created.";
redirect_to("manage_job.php");
} else {
// failure
$_SESSION["message"] = "job creation failed.";
redirect_to("new_job.php");
}
}
Your query always returns exactly 1 row, since it uses COUNT(*) to count the matching rows. You need to retrieve the row and get the count from it. You should assign an alias to the count so you can retrieve it. Then you need to test the count, not is_null.
$query = "SELECT COUNT (*) AS ct";
$query .= "FROM jobs";
$query .= "WHERE job_no=$new_job_no";
$query .= "AND start_no <= $new_end_no";
$query .= "AND end_no >= $new_start_no";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$num_rows = $row['ct'];
if ($num_rows == 0) {
// INSERT code
} else {
$_session["message"] = "job no is not available";
}

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