我没有进行插入查询,所以mysqli_insert_id没有'工作
我需要选择表A中的id(Primary,INT),它将插入到表B中。
我的桌子结构:
Table book:
-id (Primary, Auto Increment, INT)
-title
-publisher_id (Foreign key to Table publisher.pub_id, INT)
Table publisher:
-pub_id (Primary, Auto Increment, INT)
-publisher
我试过这样:
$query ="SELECT pub_id FROM book WHERE publisher = '$publisher' ";
$result=$mysqli->query($query);
$pub_id=$result->fetch_assoc();
$query ="INSERT INTO book (title,publisher_id)
VALUES ('$title', '$pub_id['pub_id']')";
$mysqli->query($query);
然后我收到如下错误消息:
syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
数据类型或其他什么问题?
如何从Table book中获取INT-type id并将其插入另一个表?
答案 0 :(得分:0)
它没有用,因为你这样做:
$query ="INSERT INTO book (title,publisher_id)
VALUES ('$title', '$pub_id['pub_id']')";
请改为尝试:
$query ="INSERT INTO book (title,publisher_id)
VALUES ('$title', '".$pub_id['pub_id']."')";