Multi Query Usage

Hey😊

Trying to use this within my code: PHP mysqli multi_query() Function but had trouble adding to code.

I want text to be placed within this table Comment (Id, CommentId, Comment) so every time a form is submitted new words are shown. Images change but everything else remains the same.

Form:

<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
 <label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" /><br>
   <label>Words:</label><br/>      
 <input type="text" id="commentid" name="commentid">
  <br><br>
   <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>       
  <input type="submit" value="Submit" name="submit">
</form>

DB Connect(want to place extra sql in here):

<?php
 require_once "db.php";
    $sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";
    $result = mysqli_query($conn, $sql);
 
//  Loop thru comments and display all of them
while($row = mysqli_fetch_array($result)) {
    printf("%s (%s)\n", $row["username"], $row["commentid"],$row["imageId"]);
  ?>

Result code:

<div class="row">
           <div class="card h-100">
               <h5 class="card-content"><?php echo $username;?> </h5>
               <p class="card-content"><?php echo $commentid;?> </p>
               <div class="card-footer">
      <div class="card-footer">
 
    </div>
  </div>
</div>

  </div>
             </div>
          
     
            <div class="row">
            <div class="card h-100">
              <div class="card-header">
               <h5 class="card-content"><?php echo $username;?> </h5>
              <p class="alert-content"><?php echo $commentid;?> </p></div>
                     <img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" class="card-img-top" alt="...">
              <div class="card-footer">
    </div>

Thank ya❤️

You want new words shown and new images? Randomly or by last posted? If you want last posted, you need to add a datetime field and save the time when the comment is posted. Then, sort the list by the datetime field. If you want to randomly show an image with comments, just use the random feature inside your query. Don’t sort by image, but, just randomly grab a row of data.

Not 100% sure we know what you need…

1 Like

@ErnieAlex the image upload works fine…the issue is the text is repeated. I want it to be where a user’s post differ per form submission. Goal is to have my version of this image to work properly.

@ErnieAlex

I added the multi query and got this error:
Fatal error: Uncaught Error: Call to a member function multi_query() on null in /homepages/23/d815730660/htdocs/home.php:345 Stack trace: #0 {main} thrown in /homepages/23/d815730660/htdocs/home.php on line 345

<?php
 require_once "db.php";
  if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";
$sql .= "SELECT commentid FROM comment ORDER BY commentid DESC";

//this is where the error lies
if ($mysqli -> multi_query($sql)) {
  do {
    // Store first result set
    //  Loop thru comments and display all of them
    if ($result = $mysqli -> store_result()) {
      while ($row = $result -> fetch_row()) {
         printf("%s (%s)\n", $row["username"], $row["commentid"],$row["imageId"]); 
      }
     $result -> free_result();
    }
    // if there are more result-sets, the print a divider
    if ($mysqli -> more_results()) {
      printf("-------------\n");
    }
     //Prepare next result set
  } while ($mysqli -> next_result());
}

$mysqli -> close();
 ?>

I added the comment query so connect the DB table to the text input form. Want both to work at the same time

Forget about using a multi_query. That’s not what you are doing. You are querying to get the image data and any (zero or more) related comments for each image id. You do this using a LEFT JOIN query.

As to the error about $mysqli being a null value (there’s probably an error about it being undefined as well.) Your connection code is using $conn, not $mysqli.

1 Like

@phdr

Question: As you can see in my earlier post I have both image and text upload within one form. Im having a hard as hell getting them to cooperate and function as one. Ive been wrecking my brain for days on this and cant seem to get it right :frowning_face:

If you are using multi-query, you need to end the first query with a semi-colon. Like this:

$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC;";
$sql .= "SELECT commentid FROM comment ORDER BY commentid DESC";

Otherwise, the function does not know when one ends and the next starts.
Nobody uses multi_query functions. Too hard to debug!

1 Like

If someone is uploading an image, why would they be submitting a comment at the same time and for their own image?

1 Like

@phdr

Add a description to their image. Like with FB or IG users can post a caption along with the image. Thats all…I feel Im making things more complex than they should.

@ErnieAlex I added but nothing changed much

@ErnieAlex & @phdr

Maybe I should create seprate forms for both just to keep things simple…what do you both think? :blush:

Well, normally. for posts, you keep them in just one table. With a link to the image stored in a folder somewhere. And, when you list them like a profile, you get all the info at one time, one simple query. Then, you just display them and already have the link to the image. Does this make sense?

1 Like

A description/caption for an image is not a comment. It’s unique/one-time data for the image that would be stored in column(s) in the row in the image database table.

1 Like

@ErnieAlex

It makes lot of sense…basically it seems that what I wanted is causing a clash and that’s not cool.

@phdr

See Im still learning so I wouldn’t never considered this.

Keeping it simple and split the forms…Thank y’all for the insight and help :heart:

Sponsor our Newsletter | Privacy Policy | Terms of Service