2

I am trying to switch from mysql to PDO and not ure how to go about this I get Fatal error: Call to undefined method PDO::prepared() in C:\wamp\www\Systems\insert_process.php on line 12 Looking to see if any one can give me a sample or point me in the right direction all tutorials are showing me how to insert data but not with a submit form.

<?php

$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "systems_requests";

$dbh = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

$add_member= $dbh->prepared("INSERT INTO requests(lanId, name, department, manager, request,  request_description, request_comments, status, comments, compUser, compDt) Values (?,?,?,?,?,?,?,?,?,?)");
$add_member->bindParam(1, $_POST["lanId"]);
$add_member->bindParam(2, $_POST["name"]);
$add_member->bindParam(3, $_POST["department"]);
$add_member->bindParam(4, $_POST["manager"]);
$add_member->bindParam(5, $_POST["request"]);
$add_member->bindParam(6, $_POST["request_description"]);
$add_member->bindParam(7, $_POST["request_comments"]);
$add_member->bindParam(8, $_POST["status"]);
$add_member->bindParam(9, $_POST["comments"]);
$add_member->bindParam(10, $_POST["compUser"]);
$add_member->bindParam(11, $_POST["compDt"]);



$dbh->close();

$email = $_POST["emailaddress"]; 

$to = "";
$subject = "Systems Request";
$headers = "From: "; 

$message = "LanID: " . $lanId . " 

" ."User Name: ".  $name ." 

". "Department: " . $department . " 

" ."Manager: ".  $manager . " 

". "User Request: " . $request . "

" ."User Request Description: ".  $request_description .  " 

" ."User Request comments: ".  $request_comments .  "

" ."Status: " .  $status .  " 
 
" ."Systems comments: ".  $comments .  " 
 
" ."Completed by: ".  $compUser ;

mail($to,$subject,$message,$headers); 


echo ("<br> <a href='http://a0319p528/dc399Homepage/'> DC399Homepage </a>");
?>
<html>
<body>
<br><br><br>

<h1 align="center">Systems Request Confirmation</h1>
<p align="center">Thank you, <?php echo $_POST["name"]; ?><br><br>
    Your request has been sent. Your request number is <?php echo $id;?><br>
    Please write this number down or print this page out.</p>

<div align="center">
    <h2>Request Information</h2>
    Date Request: <?php $date = new DateTime();
echo $date->format('m/d/Y H:i:s') . "\n";  ?><br>
    Manager: <?php echo $_POST["manager"]; ?><br>
    Location: <?php echo $_POST["department"];  ?><br>
    Request Issue: <?php echo $_POST["request"];  ?> <?php echo $_POST["request_description"];  ?><br>
    Request Comments: <?php echo $_POST["request_comments"];  ?><br>
</div>
<div align="center">
    <h2>Status Information</h2>
    Status: <?php echo $_POST["status"];  ?><br><br><br><br>
</div>
<div align="center"><button onClick="window.print()">Print this page</button></div>
</body>
</html> 
Donny
  • 738
  • 7
  • 23
  • Where does `execute()` come in? *There you go* ;) [**Read the docs**](http://php.net/pdo.prepared-statements) edit: and `prepared` remove the `d`. – Funk Forty Niner Nov 06 '14 at 19:44

3 Answers3

6

You've made an edit without marking it as an edit under your original question.
This is as per your originally posted question should anyone ask why (the answer).


It's prepare and not prepared which is the reason why you're getting the fatal error.

$add_member= $dbh->prepare("INSERT INTO requests ...

Plus, you're not executing

add:

$add_member->execute();

after

$add_member->bindParam(11, $_POST["compDt"]);

as per the docs

http://php.net/pdo.prepared-statements

<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute(); // <=== right there

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>

and

<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>

Edit and as per your comment:

"I am getting this error now Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\wamp\www\Systems\insert_process.php on line 25"

You have 11 binds but only 10 values

(?,?,?,?,?,?,?,?,?,?)

add one.

(?,?,?,?,?,?,?,?,?,?,?)
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I am getting this error now Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\wamp\www\Systems\insert_process.php on line 25 – Donny Nov 06 '14 at 20:00
  • I'll look into it, but first... you've made an edit with the added `execute()` without marking it as an **edit**. People will visit the question and my answer and will say "execute is in there, why the answer"? and stand at being downvoted because of it. Always mark your code/question as an "added" edit under your original post. @Donny – Funk Forty Niner Nov 06 '14 at 20:02
  • @Donny Reload my answer and near the bottom under **Edit**. – Funk Forty Niner Nov 06 '14 at 20:04
  • @Donny So Donny, how you coming along? – Funk Forty Niner Nov 06 '14 at 20:25
  • I was able to get it to work. Now I just got to get the email part to work. – Donny Nov 06 '14 at 20:44
  • @Donny That's great Donny, I'm glad to hear it. – Funk Forty Niner Nov 06 '14 at 20:44
  • @Donny Try posting a new question with just the mail codes, I may be able to help you, or others as well. – Funk Forty Niner Nov 06 '14 at 20:46
  • I will if i get stuck I think I may have it not sure yet. Also I am trying to do an update page that might be posted for a question for sure – Donny Nov 06 '14 at 20:47
  • @Donny Ok Donny, if and once I see it, I'll see what I can do, and many others stand at seeing it also. By hook or by crook, we'll get 'er done ;) – Funk Forty Niner Nov 06 '14 at 20:49
  • Nice to demonstrate how named placeholders makes this a whole lot less messy. – tadman Nov 06 '14 at 21:31
  • Yes indeed @tadman and easier to track/keep tabs on than question marks. – Funk Forty Niner Nov 06 '14 at 22:02
4

The right method name is prepare, so change to:

$add_member= $dbh->prepare("INSERT INTO requests(lanId, name, department, manager, request,  request_description, request_comments, status, comments, compUser, compDt) Values (?,?,?,?,?,?,?,?,?,?)");
Marcio Mazzucato
  • 8,841
  • 9
  • 64
  • 79
0

Yes you have to prepare fuction to build sql query and once query is ready or built properly it will execute and produce results.

Atul Jindal
  • 946
  • 8
  • 8