scuttzz Posted September 16, 2014 Share Posted September 16, 2014 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 Quote Link to comment Share on other sites More sharing options...
Solution CroNiX Posted September 16, 2014 Solution Share Posted September 16, 2014 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. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted September 16, 2014 Share Posted September 16, 2014 Also, since you are using bound query parameters (good!) in your query, you shouldn't do this: $_POST = array_map( 'stripslashes', $_POST ); Quote Link to comment Share on other sites More sharing options...
scuttzz Posted September 16, 2014 Author Share Posted September 16, 2014 Thank you very much for the sharp eyes.. It works now.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.