我需要帮助用PDO将数据输入数据库表 [英] I need help entering data into database table with PDO
本文介绍了我需要帮助用PDO将数据输入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将数据输入数据库&得到这些消息:
警告:第23行的C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ C中的字符串偏移'number1':第23行的C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\第23行\testing \pdocalc.php
警告:第23行的C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b
警告:第23行C:\ xampp \ htdocs \ testing \ pdocalc.php中的非法字符串偏移'总计'
致命错误:调用成员函数准备()第24行的C:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ p $ p>
<?php
function _NewDB()
{
尝试
{
$ databaseName = root; $ databaseUser = homedb; $ databasePass = 曲奇跨度>;
$ 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 ;
}
// 结束连接内容
$ dbh = _NewDB();
$ fieldList = array( purpose , number1, opvalue, number2 , total);
if(!$ _ POST)exit( 没有通过POST提交的数据!);
$ data = json_encode($ _ POST,true);
foreach($ fieldList as $ field)
if(!$ data [$ field])exit( 没有为'提交数据。 $ field 。 '!);
$ stmt = $ dbh-> prepare( INSERT INTO计算器(用途,number1,opvalue,number2,total)VALUES(:目的,:number1,:opvalue,:number2,:total));
$ stmt->执行($ _ POST); // 第22行
echo ' 在计算器表中成功添加了新行!';
// 下面用PDO从表中提取行/结果
// $ stmt = $ dbh-> prepare(SELECT * FROM calculator);
// $ stmt-> execute();
// $ rows = $ stmt-> fetchAll(PDO :: FETCH_ASSOC);
header( refresh:3; url ='http://localhost/testing/pdocalc.html');
?>
解决方案
databaseName = root;
>
databaseUser = homedb;
databasePass = cookie ;
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
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'");
?>
解决方案 databaseName = "root";
databaseUser = "homedb";
databasePass = "cookie";
这篇关于我需要帮助用PDO将数据输入数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文