gpt4 book ai didi

php - 如果 PDO INSERT 查询中的值为空,则如何避免错误?

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:20 24 4
gpt4 key购买 nike

下面是允许用户添加 <a href> tag 的简单脚本到他们的名单。

如果没有 <a href> tag , 然后 $author_id是空的。

第一个查询工作正常,如果 $author_id 为空,第二个查询不工作。

如果它是空的,我怎样才能避免出错? (我确定我想多了

$content = $_POST['book_title'];
preg_match_all('/@\w+/is', $content, $matches);
foreach ($matches[0] as $v) {
$name = substr($v, 1, strlen($v));
$sql = "SELECT id, author FROM users WHERE author like '%{$name}%' LIMIT 1";
$result = query($sql);
if($result[0]['author']) {
$author = $result[0]['author'];
$author_id = $result[0]['id'];
$content = str_replace($v, "<a href='/$author'>$v</a>",$content);

--->
//if i put $sql query here, $bookid doesn't exist yet
}
}

// works fine
$insert=$sth->con->prepare("INSERT INTO booklist (userid, bookid, book) VALUES (?,'', ?)");
$insert->bindParam(1, $id, PDO::PARAM_STR, 12);
$insert->bindParam(2, $content, PDO::PARAM_STR, 12);
$insert->execute();

// when $author_id is empty, it doesn't work
$sql=$sth->con->prepare("INSERT INTO notifications (notifid, user, author, type, bookid) VALUES ('',?, ?,'3',LAST_INSERT_ID())");
$sql->bindParam(1, $id, PDO::PARAM_STR, 12);
$sql->bindParam(2, $author_id, PDO::PARAM_STR, 12);
$sql->execute();

有什么想法吗?

最佳答案

$author_id 在你的情况下不为空,它是未定义的,在 foreach 语句之前定义 $author_id 为:

$author_id = '';
foreach ($matches...

关于php - 如果 PDO INSERT 查询中的值为空,则如何避免错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308203/

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