Jump to content

swopping description and date in mysql database


scuttzz

Recommended Posts

hi all,

 

Firstly I am new to the php language,

hopefully this is not a silly question or a no brainer.

 

I have looked over my code.. and for some reason when I insert data from a cms into a mySql database thers two fields that swop around..

 

HERES THE CODE IM WORKING WITH :

<?php

//if form has been submitted process it
if(isset($_POST['submit'])){

		$_POST = array_map( 'stripslashes', $_POST );

		//collect form data
		extract($_POST);

		//very basic validation
		if($title ==''){
			$error[] = 'Please enter the title.';
		}


		if(!isset($error)){

			try {

				//insert into database
				$stmt = $handler->prepare('INSERT INTO event_calendar (title,event_date,description) VALUES (:title, :description, :event_date)') ;
				$stmt->execute(array(
					':title' => $title,
					':description' => $description,
					':event_date' => date('Y-m-d')
				));

				//redirect to index page
				header('Location: index.php?action=added');
				exit;

			} catch(PDOException $e) {
			    echo $e->getMessage();
			}

		}

	}

	//check for any errors
	if(isset($error)){
		foreach($error as $error){
			echo '<p class="error">'.$error.'</p>';
		}
	}
	?>
	<form action='' method='post'>

		<p><label>Title</label>	<input type='text' name='title' value='<?php if(isset($error)){ echo $_POST['title'];}?>'></p>

		<p><label>Description</label><br />
		<textarea name='description' cols='50' rows='5'><?php if(isset($error)){ echo $_POST['description'];}?></textarea></p>

		<p><label>Date of Event : (y-m-d) :</label><input name="event_date" type="date" value='<?php if(isset($error)){ echo $_POST['event_date'];}?>'></p>

		<p><input type='submit' name='submit' value='Submit'></p>

	</form>


Could anyone please just look through it.. My database structure is simple.. id, title, description, event_date

Thanks in advance

 

It's being swapped because that's what you are telling it to do.

INSERT INTO event_calendar (title,event_date,description)
VALUES (:title, :description, :event_date)')

Your insert is saying (in order), title, event_date, description

 

but your VALUES (in order) are :title, :description, :event_date.

 

Swap the :description and :event_date placeholders in the VALUE.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.