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 23Warning: Illegal string offset 'number1' in C:\xampp\htdocs\testing\pdocalc.php on line 23Warning: Illegal string offset 'opvalue' in C:\xampp\htdocs\testing\pdocalc.php on line 23Warning: Illegal string offset 'number2' in C:\xampp\htdocs\testing\pdocalc.php on line 23Warning: Illegal string offset 'total' in C:\xampp\htdocs\testing\pdocalc.php on line 23Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\testing\pdocalc.php on line 24After a lot of research I haven't resolved this.<?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'");?> 解决方案 这篇关于我需要帮助用PDO将数据输入数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 14:09