Jump to content

Cannot Update MySQLI unless I upload Image


Moorcam
Go to solution Solved by Barand,

Recommended Posts

Hi folks,

This has been wrecking my brain. I did do a google a few times to see if I can find a solution but nothing unfortunately.

I want to be able to update the details on a page without having to reupload a new image each time. But if I don't open a new image for upload, I cannot update any of the other details.

Below is the code and form etc for this particular thing...

Please note this is just a project and will not be going live. I know there are vulnerabilities and I will work on those at a later stage. Thanks for any help with this current issue.

<?php
include_once('includes/header.php');



if(isset($_POST['new']) && $_POST['new']==1){

if (isset($_POST['submit'])) {
    if(!empty($_FILES['image']['name'])) {
  	// Get image name
  	$image = $_FILES['image']['name'];

$image = mysqli_real_escape_string($con, $_FILES['image']['name']);
$slide_text = mysqli_real_escape_string($con, $_POST['slide_text']);
$youtube = mysqli_real_escape_string($con, $_POST['youtube']);
$vid_text = mysqli_real_escape_string($con, $_POST['vid_text']);


  	// image file directory
  	$target = "uploads/".basename($image);
  	if($_POST['image'] = ""){
    $sql = "UPDATE slide SET slide_text='".$slide_text."', image='".$image."', youtube='".$youtube."', vid_text='".$vid_text."'"; 
  	}
  else{
    $sql = "UPDATE slide SET slide_text='".$slide_text."', youtube='".$youtube."', vid_text='".$vid_text."'"; 
  }
     $result = mysqli_query($con, $sql);

  	if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
  		$msg = "Image uploaded successfully";
  	}else{
  		$msg = "Failed to upload image";
  	}
  	
     if(!$result){ 
        die('Error: ' . mysqli_error($con));
     } else{ 
            
       $message = ' - <i class="fa fa-check success"> Record Updated!</i>';

    } 
    }
}
}
$sql = "SELECT * FROM slide";
$result = $con->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
        <!-- Header-->

        <div class="breadcrumbs">
            <div class="col-sm-4">
                <div class="page-header float-left">
                    <div class="page-title">
                        <h1>Slide Show</h1>
                    </div>
                </div>
            </div>            <div class="col-sm-8">

            </div>
        </div>

        <div class="content mt-3">
            <div class="animated fadeIn">
                <div class="row">

                 <div class="col-lg-12">
                    <div class="card">
                      <div class="card-header"><strong>Image </strong><small>Slide</small></div>
                      <div class="card-body card-block">
                            <form role="form" method="post" action"" enctype="multipart/form-data">
                                <input type="hidden" name="new" value="1" />
                            <div class="modal-body">
                                
                        <div class="row form-group">
                          <div class="col-6">
                                <div class="form-group"><label for="image" class=" form-control-label">Image</label>
                                <input type="file" id="image" name="image" value="<?php echo $row['image']; ?>" class="form-control">
                                </div>
                                </div>
                          <div class="col-6">
                           <div class="form-group"><label for="name" class=" form-control-label">Uploaded Image</label>
                          <img src="uploads/<?php echo $row['image']; ?>" width="150" height="150" class="img-fluid hover-shadow" />
                          </div>
                          </div>
                          </div>

                        <div class="row form-group">
                          <div class="col-6">
                                <div class="form-group"><label for="youtube" class=" form-control-label">Video</label>
                                <input type="text" id="youtube" name="youtube" value="<?php echo $row['youtube']; ?>" placeholder="Enter Video URL" class="form-control">
                                
                                </div>
                                </div>
                          <div class="col-6">
                                <div class="form-group"><label for="vid_text" class=" form-control-label">Video Text</label>
                                <input type="text" id="vid_text" name="vid_text" value="<?php echo $row['vid_text']; ?>" placeholder="Video Text" class="form-control">
                                
                                </div>
                          </div>
                          </div>
                          
                        <div class="form-group"><label for="slide_text" class=" form-control-label">Text Overlay</label>
                        <textarea is="slide_text" name="slide_text" class="form-control"><?php echo $row['slide_text']; ?></textarea>
                        </div>
                        
                            <div class="modal-footer">
                                <button type="submit" name="submit" id="submit" class="btn btn-primary">Confirm</button>
                            </div>
                            </form>
                  </div>
                </div>
            </div><!-- .animated -->
        </div><!-- .content -->
<?php
}
}
?>

    </div><!-- /#right-panel -->

    <!-- Right Panel -->


    <script src="assets/js/vendor/jquery-2.1.4.min.js"></script>
    <script src="assets/js/popper.min.js"></script>
    <script src="assets/js/plugins.js"></script>
    <script src="assets/js/main.js"></script>


    <script src="assets/js/lib/data-table/datatables.min.js"></script>
    <script src="assets/js/lib/data-table/dataTables.bootstrap.min.js"></script>
    <script src="assets/js/lib/data-table/dataTables.buttons.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.bootstrap.min.js"></script>
    <script src="assets/js/lib/data-table/jszip.min.js"></script>
    <script src="assets/js/lib/data-table/pdfmake.min.js"></script>
    <script src="assets/js/lib/data-table/vfs_fonts.js"></script>
    <script src="assets/js/lib/data-table/buttons.html5.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.print.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.colVis.min.js"></script>
    <script src="assets/js/lib/data-table/datatables-init.js"></script>

 <script src="https://cdn.tiny.cloud/1/sw6bkvhzd3ev4xl3u9yx3tzrux4nthssiwgsog74altv1o65/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>

  <script>
    tinymce.init({
      selector: 'textarea',
      plugins: 'advlist autolink lists link image charmap print preview hr anchor pagebreak',
      toolbar_mode: 'floating',
   });
  </script>
  
    <script type="text/javascript">
        $(document).ready(function() {
          $('#customer-table').DataTable();
        } );
    </script>


</body>
</html>

As you can see I am trying to use an If clause if the image field in the form is empty then I just want to update the other details. Else, if I fill the image field with a file, then update the lot.

  	if($_POST['image'] = ""){
    $sql = "UPDATE slide SET slide_text='".$slide_text."', image='".$image."', youtube='".$youtube."', vid_text='".$vid_text."'"; 
  	}
  else{
    $sql = "UPDATE slide SET slide_text='".$slide_text."', youtube='".$youtube."', vid_text='".$vid_text."'"; 
  }

This doesn't work.

Any ideas, besides give up? :D

 

Link to comment
Share on other sites

12 minutes ago, Barand said:

What exactly does that mean? What happens, or doesn't happen.

Hi mate,

Nothing happens. No errors etc.

If I just update some text and click submit, nothing happens. But, if I load a new image into the file input and click update, any changes to text are successful.

What I am trying to achieve is for me to be able to change text without having to upload a new image each time.

Link to comment
Share on other sites

21 minutes ago, Barand said:

Both updates happen only when


if(!empty($_FILES['image']['name'])) { ...

image.thumb.png.e47f79c382cc4201137e30e1cd20249c.png

DOH!!!

Makes sense and works.

  	// image file directory
  	$target = "uploads/".basename($image);
  	    if(!empty($_FILES['image']['name'])) {
    $sql = "UPDATE slide SET slide_text='".$slide_text."', image='".$image."', youtube='".$youtube."', vid_text='".$vid_text."'"; 
  	}
  else{
    $sql = "UPDATE slide SET slide_text='".$slide_text."', youtube='".$youtube."', vid_text='".$vid_text."'"; 
  }
     $result = mysqli_query($con, $sql);

Thanks man. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.