gpt4 book ai didi

php - 从 $_POST 更新 BLOB 字段使 BLOB 值为 0 字节长

转载 作者:行者123 更新时间:2023-11-30 22:32:45 24 4
gpt4 key购买 nike

我在更新现有 MySql 记录中的 BLOB 字段时遇到了一个奇怪的行为(就我所经历的而言是奇怪的)。当我更新它时,blob 值变为空 - 该字段不是 NULL,但字段中的 blob 长度为 0 字节。

extract($_POST);
$bio = $_POST['bio']
$cleanBio = addslashes($bio);
$sql = 'UPDATE people
SET biography = :bio
WHERE client_index = :id';
$db->query($sql);
$db->bind(array('bio', $cleanBio));
$db->bind('id', $id);
$db->execute();

我测试了一些东西:

  1. 我手动插入了传记(例如:在 phpmyadmin 或终端等的直接 sql 查询中)- 成功!

  2. 我在页面本身上创建了一个变量,其内容与 POST 变量中的内容相同(但不是从 POST 变量分配它 - 只是在页面上手动执行以进行测试)- 成功!

  3. 我使用 $_POST 变量将变量放入查询本身(相对于绑定(bind))- 失败...

  4. 我将传记数据设为一个简短的简单句子(例如:“我喜欢披萨”),但在 $_POST 变量中传递了它 - 失败...

记录的原始插入(在 blob 中有数据)成功插入。只是更新导致了这个问题。

有人知道是什么原因造成的吗?谢谢!

更新:无论字段是 blob 还是 varchar,这似乎都会发生...

提交数据的表单:

<form name=bioForm id=bioForm action=bioTest2.php method=POST>
<textarea name=bio id=bio class=textinput>
</textarea>
<input type=submit class=generalButton value="Save">
</form>

这是 $db 类的精简版本:

class Database2 {
private $dbh;
private $errorMsg;
private $stmt;

public function __construct() {
$dsn = "mysql:host=" . MYHOST . ";dbname=" . MYDB . ";charset=utf8";
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try {
$this->dbh = new PDO($dsn, MYUSER, MYPASS, $options);
}
catch(PDOException $e) {
$this->errorMsg = $e->getMessage();
}
}
public function query($sql) {
$this->stmt = $this->dbh->prepare($sql);
}
public function bind($valuesArray) {
foreach($valuesArray AS $placeholder=>$value) {
$this->stmt->bindValue($placeholder, $value);
}
}
public function resultSet() {
$this->stmt->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->stmt->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function execute() {
$this->stmt->execute();
}
}

UPDATE: Below, there's a record from the mysql log. It looks like the query is running twice (though I'm only calling it once - have triple-checked it!). The second one (91) has the empty value for bio. Any idea what might be causing this?

89 Query UPDATE people SET biography = 'this is a test string' WHERE client_index = 6

91 Query UPDATE clients SET biography = '' WHERE client_index = 6

最佳答案

FORM 方法有问题请检查表单方法确保它是 POST 方法。

或者使用 $_REQUEST 而不是 $_POST 它会起作用

您的 SQL 查询没有任何问题。

extract($_REQUEST);
$bio = $_REQUEST['bio']
$cleanBio = mysql_real_escape_string($bio);
$sql = 'UPDATE people
SET biography = :bio
WHERE client_index = :id';
$db->query($sql);
$db->bind(array('bio' => $cleanBio , 'id' => $id));
$db->execute();

关于php - 从 $_POST 更新 BLOB 字段使 BLOB 值为 0 字节长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33428557/

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