INSERT problem

Post here if you need help with SQL.
Post Reply
wistex
Posts: 13
Joined: Fri Jun 17, 2011 10:07 pm

INSERT problem

Post by wistex »

Could someone take a look at this code and help pinpoint the issue? I can view the data but am unable to "add" or "edit" the data.
-- phpMyAdmin SQL Dump
-- version 3.2.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 21, 2011 at 08:08 PM
-- Server version: 5.1.41
-- PHP Version: 5.3.1

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `haeleys_news`
--

-- --------------------------------------------------------

--
-- Table structure for table `events`
--

CREATE TABLE IF NOT EXISTS `events` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `location` varchar(100) NOT NULL,
  `date` date NOT NULL,
  `time` varchar(8) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `events`
--

INSERT INTO `events` (`id`, `title`, `location`, `date`, `time`) VALUES
(1, 'Carnegie Hall', 'New York City', '2011-07-22', '7:00pm');
Add_Event.php
<?php
include_once('resources/init.php');

if(isset($_POST['title'], $_POST['location'], $_POST['date'] , $_POST['time'])){
	$errors = array();
	
	$title 			= trim($_POST['title']);
	$location 		= trim($_POST['location']);
	$date 			= trim($_POST['date']);
	$time 			= trim($_POST['time']);

	if(empty($title)){
		$errors[] = 'You need to supply a title.';
	}else if ( strlen($title) > 100) {
		$errors[] = 'The title may not be longer than 100 characters.';
	}
	
	if (empty($location)){
		$errors[] = 'You need to supply location.';
	}
	
	if (empty($date)){
		$errors[] = 'You need to supply a date.';
	}
	
	if (empty($time)){
		$errors[] = 'You need to supply a time.';
	}
	
	if (empty($errors)) {
		add_event($title, $location, $date, $time);
		
		$id = mysql_insert_id();
		
		header("Location: index.php?id={$id}");
		die();
	}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../news/css/style.css">
<title>Add Event</title>
</head>
<body>

    <div class="event">
    <h1>Add Event</h1>
        <nav>
            <ul>
                <li><a href="../news/admin.php">Admin Area</a></li>
                <li><a href="edit_event.php">Edit Event</a></li>                
            </ul>
        </nav>
    <p> </p>
<?php
	if (isset($errors) && ! empty($errors)) {
		echo '<ul><li>', implode('</li><br /><li>', $errors), '</li></ul>';
	}
?>    
        <form action="" method="post">
          <div>
            <label for="title">Title</label>
              <input name="title" type="text" value="<?php if(isset($_POST['title'])) echo $_POST['title']; ?>" size="45">
            </div>
            <div>
                <label for="location">Location</label>
                <input type="text" name="location" value="<?php if(isset($_POST['location'])) echo $_POST['location']; ?>" size="45">
            </div>
            <div>
                <label for="title">Date</label>
                <input type="text" name="date" value="<?php if(isset($_POST['date'])) echo $_POST['date']; ?>" size="45">
            </div>
            <div>
                <label for="title">Time</label>
                <input type="text" name="time" value="<?php if(isset($_POST['time'])) echo $_POST['time']; ?>" size="45">
            </div>
            <div>
                <input type="submit" value="Add Event">
            </div>
        </form>
    </div>
</body>
</html>
Edit_Event.php
<?php
include_once('resources/init.php');

$post = get_events($_GET['id']);

if(isset($_POST['title'], $_POST['location'], $_POST['date'], $_POST['time'])){
	$errors = array();
	
	$title 		= trim($_POST['title']);
	$location 	= trim($_POST['location']);
	$date 		= trim($_POST['date']);
	$time 		= trim($_POST['time']);
	

	if(empty($title)){
		$errors[] = 'You need to supply a title.';
	}else if ( strlen($title) > 255) {
		$errors[] = 'The title may not be longer than 255 characters.';
	}
	
	if (empty($location)){
		$errors[] = 'You need to supply a location.';
	}
	
	if (empty($date)){
		$errors[] = 'You need to supply a date.';
	}
	
	if (empty($time)){
		$errors[] = 'You need to supply a time.';
	}	
	
	if ( empty($errors)) {
		edit_post($_GET['id'], $title, $location, $date, $time);
		
	
		header("Location: index.php?id={$post[0]['post_id']}");
		die();
	}
	
}
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="../news/css/style.css">
<title>Edit Event</title>

<link rel="stylesheet" type="text/css" href="../news/css/style.css">
</head>

<body>
    <div class="event">
    <h1>Add Event</h1>
        <nav>
            <ul>
                <li><a href="../news/admin.php">Admin Area</a></li>
                <li><a href="edit_event.php">Edit Event</a></li>                
            </ul>
        </nav>
    <p> </p>
<?php
	if (isset($errors) && ! empty($errors)) {
		echo '<ul><li>', implode('</li><br /><li>', $errors), '</li></ul>';
	}
?>

    <form action="" method="post">
        <div>
            <label for="title">Title</label>
            <input type="text" name="title" value="<?php echo $post[0]['title']; ?>">
        </div>
        <div>
            <label for="title">Location</label>
            <input type="text" name="location" value="<?php echo $post[0]['location']; ?>">
        </div>
        <div>
            <label for="title">Date</label>
            <input type="text" name="date" value="<?php echo $post[0]['date']; ?>">
        </div>
        <div>
            <label for="title">Time</label>
            <input type="text" name="time" value="<?php echo $post[0]['time']; ?>">
        </div>
        <div>
        	<input type="submit" value="Edit Event">
        </div>
    </form>
</div>
</body>
</html>
Index.php
<?php
include_once('resources/init.php');

$posts = get_events((( isset($_GET['id'])) ? $_GET['id'] : null));

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../news/css/style.css">
<title>Appearance Events</title>
</head>
<body>
<!--<nav>
	<ul>
    	<li><a href="event_index.php">Home</a></li>
		<li><a href="add_event.php">Add Event</a></li>
        <li><a href="admin.php">Admin Area</a></li>
    </ul>
</nav>-->
<h1>Appearance Events</h1>    
<?php /*?><?php
foreach ( $posts as $post ){
	if ( ! category_exists('name', $post['name'])){
		$post['name'] = 'Uncategorized';
	}
	?>
	<h2><a href="index.php?id=<?php echo $post['post_id'];?>"><?php echo $post['title']; ?></a></h2>
	<p>Posted on<?php echo date('d-m-Y h:i:s', strtotime($post['date_posted'])); ?>
	in <a href="category.php?id=<?php echo $post['category_id']; ?>"><?php echo $post['name']; ?></a>
	</p>
	<div><?php echo nl2br($post['contents']); ?></div>
	
	<menu>
		<ul>
			<li><a href="delete_post.php?id=<?php echo $post['post_id']; ?>">Delete This Post</a></li>
			<li><a href="edit_post.php?id=<?php echo $post['post_id']; ?>">Edit This Post</a></li>
		</ul>
	</menu>
	<?php
}
?>
<hr /><?php */?>
<?php

$per_page = 3;

$pages_query = mysql_query("SELECT COUNT(`id`)FROM `events`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;

if ($pages >= 1 && $page <=$pages) {
	for ($x = 1; $x<=$pages; $x++) {
		//echo 
		echo ($x == $page) ? '<span class="activetabs"><a href="?page='.$x.'">'.$x.'</a></span>' : '<span class="tabs"><a href="?page='.$x.'">'.$x.'</a></span> ';
	}
}

$query = mysql_query("SELECT `title`, `location`, `date`, `time` FROM `events` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)) {
	echo '<h2>', $query_row['title'] ,'</h2>';
	echo '<h3>', $query_row['location'] ,'</h3>';
	echo '<h3>', $query_row['date'] ,'</h3>';
	echo '<h3>', $query_row['time'],'</h3>';
	
}
?>
</body>
</html>
Thank you for your help.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: INSERT problem

Post by jacek »

What happens when you try to add an event ?
Image
wistex
Posts: 13
Joined: Fri Jun 17, 2011 10:07 pm

Re: INSERT problem

Post by wistex »

Interestingly enough, nothing. ;) I receive no errors, nor is anything added to the DB.

BTW...I love your tutorials!
wistex
Posts: 13
Joined: Fri Jun 17, 2011 10:07 pm

Re: INSERT problem

Post by wistex »

Here are the functions
<?php
function add_event($title, $contents, $date, $time){
	$title 			= mysql_real_escape_string		($title);
	$location 		= mysql_real_escape_string		($location);
	$date 			= mysql_real_escape_string		($date);
	$time 			= mysql_real_escape_string		($time);
	
	mysql_query("INSERT INTO `events` SET 
					`id` 			= {$id},
					`title` 		= '{$title}',
					`location` 		= '{$location}',
					`date`			= '{$date}',
					`time`			= '{$time}'");
}

function edit_event($id, $title, $contents, $date, $time){
	$id				=(int) 							$id;
	$title 			= mysql_real_escape_string		($title);
	$location 		= mysql_real_escape_string		($location);
	$date 			= mysql_real_escape_string		($date);
	$time 			= mysql_real_escape_string		($time);	
	
	mysql_query("UPDATE `events` SET
					`id` 			= {$id},
					`title` 		= '{$title}',
					`location` 		= '{$location}',
					`date`			= '{$date}',
					`time`			= '{$time}'
				WHERE `id` = {$id}");
}

function delete($table, $id){
	$table = mysql_real_escape_string($table);
	$id = (int)$id;
	mysql_query("DELETE FROM `{$table}` WHERE `id` = {$id}");
}

function get_events($id = null){
	$events = array();
	
	$query = 	"SELECT `events`.`id` AS `post_id`,
						`title`, `location`, `date`, `time`
				FROM `events`";
				
	if ( isset($id)) {
		$id = (int) $id;
		$query .= " WHERE `events`.`id` = {$id}";
	}
	
	$query .= " ORDER BY `events`.`id` DESC" or die();
				
	$query = mysql_query($query);
	
	while( $row = mysql_fetch_assoc($query)) {
		$events[] = $row;
	}
	
	return $events;
}
?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: INSERT problem

Post by jacek »

wistex wrote:BTW...I love your tutorials!
:D :D
wistex wrote:Interestingly enough, nothing. ;) I receive no errors, nor is anything added to the DB.
Try adding
echo mysql_error();
after what ever query is failing and see if that tells you what is wrong.
Image
Post Reply