Jump to content

swopping description and date in mysql database


scuttzz
Go to solution Solved by CroNiX,

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

 

Link to comment
Share on other sites

  • Solution

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.

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.