Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to enter data into database & get these messages:
Warning: Illegal string offset 'purpose' in C:\xampp\htdocs\testing\pdocalc.php on line 23

Warning: Illegal string offset 'number1' in C:\xampp\htdocs\testing\pdocalc.php on line 23

Warning: Illegal string offset 'opvalue' in C:\xampp\htdocs\testing\pdocalc.php on line 23

Warning: Illegal string offset 'number2' in C:\xampp\htdocs\testing\pdocalc.php on line 23

Warning: Illegal string offset 'total' in C:\xampp\htdocs\testing\pdocalc.php on line 23

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\testing\pdocalc.php on line 24
After a lot of research I haven't resolved this.


PHP
<?php 
       function _NewDB()
 {
       try  
 {
 $databaseName = "root";$databaseUser = "homedb";$databasePass = "cookie";
 $dbh = new PDO('mysql:host=localhost;dbname='.$databaseName,      $databaseUser, $databasePass);
         $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 } 
catch (PDOException $e)
          {error_log("PDOException: " . $e);return false;}
    return $dbh;
 }
   // End connection stuff            
  $dbh = _NewDB();
  $fieldList = array("purpose", "number1", "opvalue", "number2", "total" );             
  if(!$_POST) exit("No data submitted via POST!");
      $data = json_encode($_POST, true);                
       foreach($fieldList as $field) 
 if(!$data[$field]) exit("No data submitted for '" . $field . "'!");     
            $stmt = $dbh->prepare("INSERT INTO calculator (purpose, number1, opvalue, number2, total) VALUES(:purpose, :number1, :opvalue, :number2, :total)");
            $stmt->execute($_POST);                     // line 22
            echo 'Sucessfully added a new row to the calculator table!';
    // Below pulling rows/results from the table with PDO            
            //$stmt = $dbh->prepare("SELECT * FROM calculator");
            //$stmt->execute();
            //$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
header( "refresh:3;url='http://localhost/testing/pdocalc.html'");
?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900