您好,我正在嘗試將 url 及其內容插入到 2 個表中,但我將使用表中的 id 并將此 id 插入到第二個表中的多個條目中,我希望我能成功地澄清我的問題,我可以查看數據,但我無法使用 pdo 在 mysql 中插入數據,我在 mysql 中得到的所有內容都是 NULL 值是我的代碼,用于更多說明首先我的類文件是這樣的 Links.php /** * Sets the object's properties using the values in the supplied array * * @param assoc The property values */ public function __construct1( $data=array() ) { if ( isset( $data['listURL'] ) ) $this->listURL = $data['listURL']; if ( isset( $data['hostname'] ) ) $this->hstname = $data['hostname']; } /** * Sets the object's properties using the values in the supplied array * * @param assoc The property values */ public function __construct2( $data=array() ) { if ( isset( $data['stringName'] ) ) $this->stringName = $data['stringName']; if ( isset( $data['stringUrl'] ) ) $this->stringUrl = $stringUrl = $data['stringUrl']; } /** * Sets the object's properties using the edit form post values in the supplied array * * @param assoc The form post values */ public function storeFormValues1 ( $params ) { // Store all the parameters $this->__construct1( $params ); } /** * Sets the object's properties using the edit form post values in the supplied array * * @param assoc The form post values */ public function storeFormValues2 ( $params ) { // Store all the parameters $this->__construct2( $params ); }public function insertlistFuncClass() { $conn = new PDO(DB_NDS, DB_USERNAME, DB_PASSWORD); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); $sql = "INSERT INTO lists ( listURL, hostname ) VALUES (:listURL, :hostname)"; $st = $conn->prepare($sql); $st->bindValue( ":listurl", $this->url, PDO::PARAM_STR ); $st->bindValue( ":hostname", $this->hstname, PDO::PARAM_STR ); $st->execute(); $listId = $conn->lastInsertId(); $conn = null;}}
1 回答

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
我找到了一個我剛剛使用的解決方案,global
我將它放在 file.php 的兩個函數中,并且它起作用了
global $listId;
但我必須從這個文件而不是從類中插入。
- 1 回答
- 0 關注
- 152 瀏覽
添加回答
舉報
0/150
提交
取消