Jump to content

Cannot Edit Or Add To Database, But Pages Seems To Work And Cycle Through?


Recommended Posts

I am building a database using a book called novice to ninja, i even used the books code and the same problem, I have an admin section where i can add, edit or delete the information, but only delete works, and i'm getting no errors. It cycles through as if it works but it doesnt change the database.

 

Any thoughts on what would cause this?

if (isset($_GET['add']))
{
 $pageTitle = 'New Band';
 $action = 'addform';
 $name = '';
 $email = '';
 $id = '';
 $button = 'Add Band';

 include 'form.html.php';
 exit();
}

if (isset($_GET['addform']))
{
 include $_SERVER['DOCUMENT_ROOT'] . '/paranoiddb/includes/db.inc.php';

 try
 {
$sql = 'INSERT INTO band SET
	name = :name,
	email = :email';
$s = $pdo->prepare($sql);
$s->bindValue(':name', $_POST['name']);
$s->bindValue(':email', $_POST['email']);
$s->execute();
 }
 catch (PDOException $e)
 {
$error = 'Error adding submitted author.';
include 'error.html.php';
exit();
 }

 header('Location:.');
 exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'Edit')
{
 include $_SERVER['DOCUMENT_ROOT'] . '/paranoiddb/includes/db.inc.php';

 try
 {
$sql = 'SELECT id, name, email FROM band WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
 }
 catch (PDOException $e)
 {
$error = 'Error fetching author details.';
include 'error.html.php';
exit();
 }

 $row = $s->fetch();

 $pageTitle = 'Edit Band';
 $action = 'editform';
 $name = $row['name'];
 $email = $row['email'];
 $id = $row['id'];
 $button = 'Update band';

 include 'form.html.php';
 exit();
}

if (isset($_GET['editform']))
{
 include $_SERVER['DOCUMENT_ROOT'] . '/paranoiddb/includes/db.inc.php';

 try
 {
$sql = 'UPDATE band SET
	name = :name,
	email = :email
	WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->bindValue(':name', $_POST['name']);
$s->bindValue(':email', $_POST['email']);
$s->execute();
 }
 catch (PDOException $e)
 {
$error = 'Error updating submitted author.';
include 'error.html.php';
exit();
 }

 header('Location: .');
 exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'Delete')
{
 include $_SERVER['DOCUMENT_ROOT'] . '/paranoiddb/includes/db.inc.php';

 // Get jokes belonging to author
 try
 {
$sql = 'SELECT id FROM video WHERE bandid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
 }
 catch (PDOException $e)
 {
$error = 'Error getting list of jokes to delete.';
include 'error.html.php';
exit();
 }

 $result = $s->fetchAll();

 // Delete joke category entries
 try
 {
$sql = 'DELETE FROM videocategory WHERE videoid = :id';
$s = $pdo->prepare($sql);

// For each joke
foreach ($result as $row)
{
  $videoId = $row['id'];
  $s->bindValue(':id', $videoId);
  $s->execute();
}
 }
 catch (PDOException $e)
 {
$error = 'Error deleting category entries for joke.';
include 'error.html.php';
exit();
 }

 // Delete jokes belonging to author
 try
 {
$sql = 'DELETE FROM video WHERE bandid = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
 }
 catch (PDOException $e)
 {
$error = 'Error deleting jokes for author.';
include 'error.html.php';
exit();
 }

 // Delete the author
 try
 {
$sql = 'DELETE FROM band WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
 }
 catch (PDOException $e)
 {
$error = 'Error deleting author.';
include 'error.html.php';
exit();
 }

 header('Location: .');
 exit();
}

// Display author list
include $_SERVER['DOCUMENT_ROOT'] . '/paranoiddb/includes/db.inc.php';

try
{
 $result = $pdo->query('SELECT id, name FROM band ');
}
catch (PDOException $e)
{
 $error = 'Error fetching authors from the database!';
 include 'error.html.php';
 exit();
}

foreach ($result as $row)
{
 $bands[] = array('id' => $row['id'], 'name' => $row['name']);
}

include 'bands.html.php';

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.