8 Replies - 605 Views - Last Post: 04 November 2013 - 10:41 AM Rate Topic: -----

#1 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 19
  • View blog
  • Posts: 764
  • Joined: 06-July 13

PDO problem

Posted 05 October 2013 - 03:41 AM

I am creating a new page system, and I have come across a problem; every time I try to submit, it comes out with this:

Quote

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in /****/new_page.php:216 Stack trace: #0 /****/new_page.php(216): PDOStatement->execute(Array) #1 {main} thrown in /****/new_page.php on line 216


As far as I can see, the parameter is defined, (below) - can someone tell me what I'm doing wrong?

<?php
			if (isset($_POST['submitBtn'])) { 	

		 $title     = (isset($_POST['title'])) ? ($_POST['title']) : '' ;
		 $text     = (isset($_POST['text'])) ? ($_POST['text']) : '' ;
		 $username = "".($pun_user['username'])."";
		 $date  =  "".date("d/m/Y")."";
		 $user_id = "".$pun_user['id']."";
		 $email = "".$pun_user['email']."";
		 $tags     = (isset($_POST['tags'])) ? htmlentities($_POST['tags']) : '' ;
		 $disabled     = (isset($_POST['disabled'])) ? htmlentities($_POST['disabled']) : '' ;
		 $disabled_message     = (isset($_POST['disabled_message'])) ? ($_POST['disabled_message']) : '' ;

		if (empty($disabled)) {
		 exit ('You must choose whether to disabled the page by default.This will mean only staff can access it.');
		 }			 
		if (empty($title)) {
		 exit ('You must enter a Page or Subject Title.');
		 }				
		if (empty($text)) {
		 exit ('You must enter your news.');
		 }
		if (empty($date)) {
		 exit ('Error fetching current date.Please try re-submitting.');
		 }						
			
		$sql = "INSERT INTO pages (page,title,username,userid,email,text,date,tags,disabled,disabled_message) VALUES (
			 :page,:title,:username,:userid,:email,:text,:date,:tags,:disabled,:disabled_message)";
		$ps = $pdo->prepare($sql);
		$ps->execute(array(':page'=>$title,
		':title'=>$title,
		':by'=>$username,
		':userid'=>$user_id,
		':email'=>$email,
		':text'=>$text,
		':date'=>$date,
		':tags'=>$tags,
		':disabled'=>$disabled,
		':disabled_message'=>$disabled_message));
?>
<h1>Results:</h1><br><br>The server has processed your request, and returned the following result.<br><br>Your Page has been added succesfully. - Please remember that if you have added disabled this page, only staff can view it.
<br><br><a href="http://www.strongholdnation.co.uk">Return to the Main Site</a>
<?php
 }
else
{?>
<h3>Required fields are marked with an asterisk (*)</h3><br><br>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        Page Title: *<br><br>
		<input name="title" size="40" /><br><br>
		Content: *<br><br><a href="upload.php?dir=pages" target="_blank">Upload Images for your Page</a><br><br>
		<p align="left"><textarea name="text" cols="50" rows="20"></textarea></p><br><br>
		<br><br>
		Disabled page? *<br><br><input type="radio" name="disabled" value="DISABLED">Disable page by default<br><br>
		<input type="radio" name="disabled" value="NOT_DISABLED" CHECKED>Do not disable this page by default<br><br>
		Disabled Message:<br><br>ONLY PLACE TEXT HERE IF YOU HAVE DISABLED THE PAGE ABOVE<br>
		<p align="left"><textarea name="disabled_message" cols="50" rows="10"></textarea></p><br><br>
		You may include tags to enhance your page's position in Google.These may be no longer than 100 characters.Please seperate these with a comma ",".<br><br>I.E. tag, tag2, tag3, <br><br><input class="formbox" type="text" name="tags" maxlength="100" size="20" onblur="textCounter(this,this.form.counter,100);" onkeyup="textCounter(this,this.form.counter,100);">
<input onblur="textCounter(this.form.recipients,this,100);" disabled  onfocus="this.blur();" tabindex="999" maxlength="3" size="3" value="100" name="counter">
<script type="text/javascript">
function textCounter( field, countfield, maxlimit ) {
 if ( field.value.length > maxlimit ) {
  field.value = field.value.substring( 0, maxlimit );
  field.blur();
  field.focus();
  return false;
 } else {
  countfield.value = maxlimit - field.value.length;
 }
}

</script>
		<br><br><input class="text" type="submit" name="submitBtn" value="Add My Page" class="text"/>
     </form> 
 <?php } ?>



It also appears to be the last variable it doesn't agree with. ($disabled_message)

This post has been edited by Dormilich: 04 November 2013 - 10:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PDO problem

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3319
  • View blog
  • Posts: 11,223
  • Joined: 12-December 12

Re: PDO problem

Posted 05 October 2013 - 03:51 AM

:username has change to :by.

BTW Could you not think of a sensible title for your question?

BTWW I wouldn't use date (or text) as field names. What date and what text do they represent?
Was This Post Helpful? 1
  • +
  • -

#3 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 19
  • View blog
  • Posts: 764
  • Joined: 06-July 13

Re: PDO problem

Posted 05 October 2013 - 03:58 AM

I couldn't really think of an actual question, so since it basically is PDO, I thought I would put that.

The date represents the current date (I haven't used the default_timezone_set yet though, I was just wanting to know it actually worked before I did that.

The text represents the content of the article written.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,136
  • Joined: 08-June 10

Re: PDO problem

Posted 05 October 2013 - 04:11 AM

wouldn’t it be clearer to name the fields created and article_content then?

PS. date fields should be of a DATE type, so cou can do date math in SQL.

PPS. $username = "".($pun_user['username']).""; the empty quotes are absolutely useless. even if the original value where not a string (very unlikely here) PHP automatically casts the data types when necessary.

PPPS. you can pass the current date using SQL, either by CURRENT_DATE or by NOW() (of course that requires a DATE type in the DB).
Was This Post Helpful? 0
  • +
  • -

#5 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 19
  • View blog
  • Posts: 764
  • Joined: 06-July 13

Re: PDO problem

Posted 05 October 2013 - 04:19 AM

Dormilich said:

wouldn’t it be clearer to name the fields created and article_content then?


They would be better field names for them.I'll change them.

How could I pass the current date using CURRENT_DATE or by NOW()?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,136
  • Joined: 08-June 10

Re: PDO problem

Posted 05 October 2013 - 05:29 AM

Quote

How could I pass the current date using CURRENT_DATE or by NOW()?

ex.
INSERT INTO mytable (created) VALUES ( NOW() );

or if you had defined the current date as default value
INSERT INTO mytable (some_text_field) VALUES ( :some_text );

Was This Post Helpful? 0
  • +
  • -

#7 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 19
  • View blog
  • Posts: 764
  • Joined: 06-July 13

Re: PDO problem

Posted 05 October 2013 - 05:34 AM

That does seem a lot easier, but would I be able to set the default timezone still?

I.E.

date_default_timezone_set('GMT');
date_default_timezone_set("Europe/London");
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3512
  • View blog
  • Posts: 10,136
  • Joined: 08-June 10

Re: PDO problem

Posted 05 October 2013 - 04:10 PM

Quote

That does seem a lot easier, but would I be able to set the default timezone still?

sure.
Was This Post Helpful? 0
  • +
  • -

#9 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 19
  • View blog
  • Posts: 764
  • Joined: 06-July 13

Re: PDO problem

Posted 04 November 2013 - 10:41 AM

Can a moderator please edit my post #1 of this topic and star the directory paths out?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1