Using form to upload and delete data from mysql? Not going too well

I’m now at a point where I can delete information from the database by hitting a button. I have to hit it twice though, not sure what’s happening there.

Even though I can successfully delete data with that minor glitch, I am unable to upload the data.

I’m trying to use the same controller file.

Controller:

<?php

// db connection

include $_SERVER['DOCUMENT_ROOT'] . '/hfsm/inc/db.php';

// get carousel info from db and display it on page to pick which one to delete

try {
	$sql = 'SELECT id, title, description, location, category FROM carousel';
	$result = $pdo->query($sql);
	}
	
catch (PDOExcepttion $e) {
	echo 'Cannot retrieve carousel data at this point. Sorry.' . $e->getMessage();
	}

$carousel_items = array();
while ($row = $result->fetch()) {
	$carousel_items[]=array (
							'id'=>$row['id'],
							'title' => $row['title'],
							'description' => $row['description'],
							'location' => $row['location'],
							'category' => $row['category']
							);
	
	}
	
include 'inc/carousel.php';

if (isset($_POST['action']) and $_POST['action'] == 'Delete') {
	try {
		$sql = 'DELETE FROM carousel WHERE id = :id';
		$s = $pdo->prepare($sql);
		$s->bindValue(':id', $_POST['id']);
		$s->execute();
		}
	 catch (PDOException $e) {
		 echo' Error deleting joke. Action not carried out at this point.';
		 }
	
	}
	
if (isset($_POST['action']) and $_POST['action'] == 'Submit') {
	try {
		$sql = 'INSERT INTO carousel SET 
		title = :title,
		description = :description,
		location = :location';
		
		$s = $pdo->prepare($sql);
		$s->bindValue(':title', $_POST['title']);
		$s->bindValue(':description', $_POST['description']);
		$s->bindValue(':location', $_POST['location']);
		$s->execute();
		}
	 catch (PDOException $e) {
		 echo' Error uploading info. Action not be carried out at this point.';
		 }
	
	}

the include file

    <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/hfsm/inc/helpers.inc.php'; ?>
    
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Manage Home Page</title>
      </head>
      <body>
        <h1>Manage Home Page</h1>
        
        <p><a href="?add">Add New Post To Carousel</a></p>
        
        <div class="cms-carousel-add">
        
    	<form action"" method="post">
    	<ul>
        <li>
        <label for="name"> Title: </label>
        <input type="text" name="title" id="title">
        </li>
    
        <li>
        <label for="title"> Description: </label>
        <textarea name="body" id="body"></textarea>
        </li>
    
        <li>
        <label for="title"> Location: </label>
        <input type="text" name="location">
        </li>
        
        <li>
        <input type="submit" value="Submit">
    	</li>
    	</ul>
    	</form>
    
    
        
        
        </div><!-- end cma carousel add-->
        
        <div class="cms-carousel-delete">
        <ul>
          <?php foreach ($carousel_items as $carousel_item): ?>
          <li>
              <form action="" method="post">
                <div>
                  <?php htmlout($carousel_item['title']); ?>
                  <input type="hidden" name="id" value="<?php
                      echo $carousel_item['id']; ?>">
                  <input type="submit" name="action" value="Edit">
                  <input type="submit" name="action" value="Delete">
                </div>
              </form>
            </li>
          <?php endforeach; ?>
        </ul>
        </div><!-- end carousel delete-->
        
        <p><a href="..">Return to CMS home</a></p>
      </body>
    </html>

Any inputs?

does the page reload between those two hits?

Ahh ok. So it is just 1 hit. When I refresh, it’s gone. How do I get the page to refresh automatically? I’m clearly missing a lot here.

The carousel items are being collected before the delete occurs. They are being removed from the db but only after they are in memory ready to be displayed. The solution is to simply put the delete block of code before the code that fetches the carousel items from the db. Do the same for insert/update block as well.

As for the cause of the second issue the SQL is not correct.

INSERT INTO carousel (title,description,location) VALUES (:title,:description,:location);

Yes, thanks a lot. I queried the carousel items after the delete query in the controller and it’s working well now. Managed to correct the other half as well. Appeared to be a case of me missing the form data.