Below is the simple script to allow users to add an <a href> tag
to their list.
If there's no <a href> tag
, then $author_id
is empty.
The first query works fine, the 2nd doesn't if $author_id is empty.
How can i avoid getting an error if it's empty? (i'm sure I'm overthinking
$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();
Any ideas?