help w/PDO

Discussion in 'PHP' started by ataloss, May 1, 2014.

  1. #1
    Hello, can someone help with this code.


    Fatal error: Call to undefined method PDO::execute() in C:\xampp\htdocs\invoice\taxform.php on line 136
    <?php

    $db = new PDO(
    'mysql:dbname=homedb;hostname=localhost',
    'root', ' '
    );
    $db->prepare('
    INSERT INTO numbers (
    taxrate
    ) VALUES (
    :taxRate
    )
    ');
    $db->execute(array( ':taxrate' => $rate )); // error points to this
    template_header('Tax Rate ' . (
    $outputRate = htmlspecialchars($_POST['taxRate'])
    ) . ' Set');
    echo '
    <p>
    Rate successfully set to ',$outputRate, '
    </p>';
    template_footer();
    die;
    ?>
     
    Solved! View solution.
    ataloss, May 1, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    You didn't assign the "prepare" to a statement -- you don't do "execute" on your PDO object, you do it on the PDOstatement returned BY the prepare.

    <?php
    $db = new PDO(
     'mysql:dbname=homedb;hostname=localhost',
     'root', ' '
    );
    
    $statement =  $db->prepare('
    	INSERT INTO numbers (
    		taxrate
    	) VALUES (
    		:taxRate
    	)
    ');
    
    $statement->execute([':taxrate' => $rate]);
    
    template_header('Tax Rate ' . (
    	$outputRate = htmlspecialchars($_POST['taxRate'])
    ) . ' Set');
    
    echo ' 
    	<p>
    		Rate successfully set to ', $outputRate, '
    	</p>'; 
    
    template_footer();
    ?>
    Code (markup):
    See?

    Also if you're on PHP 5.4/newer, you don't need to waste time saying "array" anymore :D
     
    deathshadow, May 1, 2014 IP
  3. #3
    Helge Sverre, May 8, 2014 IP