gpt4 book ai didi

php - 尝试将 mysql_* 重写为 pdo

转载 作者:行者123 更新时间:2023-11-28 23:58:01 24 4
gpt4 key购买 nike

首先我应该说我开始学习 PDO 并尝试重写一些旧的我的代码。这是我在 jquery/ajax/pdo/mysql 评论系统的教程中找到的一个正在运行的评论系统。这是我正在尝试重写的 mysql_* 部分 -> 文件 submit.php 将评论提交到数据库。

$arr = array();
$validates = Comment::validate($arr);

if($validates)
{
/* Everything is OK, insert to database: */

mysql_query(" INSERT INTO comments(name,url,email,body)
VALUES (
'".$arr['name']."',
'".$arr['url']."',
'".$arr['email']."',
'".$arr['body']."'
)");

$arr['dt'] = date('r',time());
$arr['id'] = mysql_insert_id();

$arr = array_map('stripslashes',$arr);

$insertedComment = new Comment($arr);

/* Outputting the markup of the just-inserted comment: */

echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));

}
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}

这是我对这段代码的尝试。

$pdo = Database::connect();
$arr = array();
$validates = Comment::validate($arr);

if($validates)
{
/* Everything is OK, insert to database: */
$sql = $pdo->prepare("INSERT INTO comments ( name,url,email,body )
VALUES (:name, :url, :email, :body)");

$sql->execute(array(
':name' => $name,
':url' => $url,
':email' => $email,
':body' => $body

));
$arr['dt'] = date('r',time());
$arr['id'] = $pdo->lastInsertId();

$arr = array_map('stripslashes',$arr);

$insertedComment = new Comment($arr);

/* Outputting the markup of the just-inserted comment: */

echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));

}
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}

当我点击按钮 提交 时什么也没有发生。没有向数据库中插入任何内容。并且按钮变为非事件状态。

如果需要,我也可以显示 javascript 部分或表单。谢谢

更新:这是 comment.class.php,它也包含在 submit.php 中

class Comment
{
private $data = array();

public function __construct($row)
{
/*
/ The constructor
*/

$this->data = $row;
}

public function markup()
{
// this just output the comment on page nothing special some html
}

public static function validate(&$arr)
{

$errors = array();
$data = array();

// Using the filter_input function introduced in PHP 5.2.0

if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)))
{
$errors['email'] = 'Wrong email.';
}

if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL)))
{
// If the URL field was not populated with a valid URL,
// act as if no URL was entered at all:

$url = '';
}

// Using the filter with a custom callback function:

if(!($data['body'] = filter_input(INPUT_POST,'body',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
{
$errors['body'] = 'Please enter your comment.';
}

if(!($data['name'] = filter_input(INPUT_POST,'name',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
{
$errors['name'] = 'Please enter your name.';
}

if(!empty($errors)){

// If there are errors, copy the $errors array to $arr:

$arr = $errors;
return false;
}

// If the data is valid, sanitize all the data and copy it to $arr:

foreach($data as $k=>$v){
$arr[$k] = mysql_real_escape_string($v);
}

$arr['email'] = strtolower(trim($arr['email']));

return true;
}

private static function validate_text($str)
{

if(mb_strlen($str,'utf8')<1)
return false;

$str = nl2br(htmlspecialchars($str));

// Remove the new line characters that are left
$str = str_replace(array(chr(10),chr(13)),'',$str);

return $str;
}
}

最佳答案

不知道数组$arr = array();的来源,但是在insert查询之前赋值为null。所以这意味着,从字面上看,您没有向数据库中插入任何内容。所以好好检查你的阵列,也许它就像

$arr = array('name'=>'我的名字', 'url'=>'url', 'email'=>'我的邮箱', 'body'=>'评论');

如果您的数组没有任何问题,那么我认为您缺少数组的键。您可以将数组值存储到变量中或直接使用它们。所以使用 ,

$pdo = Database::connect();
$arr = array();
$validates = Comment::validate($arr);

if($validates)
{
/* Everything is OK, insert to database: */
$sql = $pdo->prepare("INSERT INTO comments ( name,url,email,body )
VALUES (:name, :url, :email, :body)");

$sql->execute(array(
':name' => $arr['name'],
':url' => $arr['url'],
':email' => $arr['email'],
':body' => $arr['body']

));
$arr['dt'] = date('r',time());
$arr['id'] = $pdo->lastInsertId();

$arr = array_map('stripslashes',$arr);

$insertedComment = new Comment($arr);

/* Outputting the markup of the just-inserted comment: */

echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));

}
else
{
/* Outputtng the error messages */
echo '{"status":0,"errors":'.json_encode($arr).'}';
}

希望对您有所帮助。

关于php - 尝试将 mysql_* 重写为 pdo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30975930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com