www.webdeveloper.com
Results 1 to 4 of 4

Thread: help w/pdo

  1. #1
    Join Date
    Apr 2013
    Posts
    61

    help w/pdo

    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 Code:
    <?php

    $db 
    = new PDO(
        
    'mysql:dbname=homedb;hostname=localhost',
        
    'root',    'cookie'
    );
    $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;
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    PDO::prepare() returns a PDOStatement object, which is what you then execute, so it would be something like:
    PHP Code:
    $stmt $db->prepare($sql);
    $stmt->execute(array(':taxrate' => $rate)); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2013
    Posts
    61
    I get :
    Notice: Undefined variable: rate in C:\xampp\htdocs\invoice\taxform.php on line 139
    Notice: Undefined variable: id in C:\xampp\htdocs\invoice\taxform.php on line 139
    Success!
    with this code:
    <?php
    // Create the database connection as a PDO object:
    try

    { $db_options = array(
    PDO::ATTR_EMULATE_PREPARES => false // important! use actual prepared statements (default: emulate prepared statements)
    , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION // throw exceptions on errors (default: stay silent)
    , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC // fetch associative arrays (default: mixed arrays)
    );
    $db = new PDO('mysql:host=localhost;dbname=homedb;charset=utf8', 'root', 'cookie', $db_options); }

    catch (PDOException $e)
    { // Report the Error!
    echo "Something is not right, check your php.ini settings or code<br>";}

    $query = 'UPDATE numbers SET taxrate = :taxrate WHERE id = :id';
    // Prepare the Statement:
    $stmt = $db->prepare($query);
    // execute the statement:
    $result = $stmt->execute(array(':taxrate' => $rate, ':id' => $id)); // primary key in the table column.

    if ($result) { echo "Success!"; }

    else { echo "Not again"; } // Failure
    ?>

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Well, where are those variables defined? Or should you be using post fields, instead (e.g. $_POST['taxRate'] instead of $rate)? (In whichever case, you may still need to add some checks to make sure they are set before trying to use them -- just standard defensive coding.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles