iT邦幫忙

0

使用INSERT INTO同時插入兩個資料表,不同資料

  • 分享至 

  • xImage

有很多input輸入資料但是想要插入不同的資料表
我的做法是這樣,只有TABLE users_data的時候是正常的
但是再加上第二個資料表TABLE hotel就失敗,還想要同時新增資料到第三個資料表...

$insertSQL ="INSERT INTO users_data  VALUES (NULL,$_SESSION[add_ID], '$_POST[BN]', '$_POST[GN]', '$_POST[users_address]', '$_POST[line_ID]', '$_POST[users_url]', '備註')";

$myData = $myconnect->query($insertSQL);

if ($myData) {
      echo "成功";  
  //header("Location: con_show.php");
}else{
    echo "錯誤: " . $insertSQL. "<br>" . $myconnect->error;
}

$insert2SQL ="INSERT INTO hotel VALUES (NULL,$_SESSION[add_ID],$_POST[hotel],$_POST[room],$_POST[print_time],'待確認','待確認',$_POST[table_num],$_POST[host],$_POST[host_tel],'停車場','0',current_timestamp())";

$myData2 = $myconnect->query($insert2SQL);

if ($myData2) {
      echo "成功hotel";  
}else{
    echo "錯誤: " . $insert2SQL. "<br>" . $myconnect->error;
}

出現的錯誤是

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':00~15:00,'待確認','待確認',0,後續提供,後續提供,'停車場','0',cu' at line 1

看起來是與資料庫的資料類型錯誤嗎?
一個一個檢查之後還是找不出來原因

是否可以幫我看看呢??><

謝謝各位前輩

player iT邦大師 1 級 ‧ 2020-06-19 17:35:51 檢舉
預防SQL injection
請使用【參數化查詢】
https://zh.wikipedia.org/wiki/%E5%8F%83%E6%95%B8%E5%8C%96%E6%9F%A5%E8%A9%A2
你好player

MySQL的參數格式是以"@"字元加上參數名稱而成。

set @c1 := xxx;
set @c2 := xxx;
set @c3 := xxx;
set @c4 := xxx;
UPDATE myTable SET c1 = @c1, c2 = @c2, c3 = @c3 WHERE c4 = @c4

請問這個意思是跟設定變數一樣先set起來嗎?

set @a1 :=$_POST[hotel];

請問是這樣嗎? 避免在UPDATE資料庫的資料嗎?
fillano iT邦超人 1 級 ‧ 2020-06-20 13:31:38 檢舉
https://www.php.net/manual/en/book.pdo.php
簡單一點的方法是改用pdo,他有支援參數化查詢
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
rogeryao
iT邦超人 8 級 ‧ 2020-06-19 14:22:40
最佳解答
後續提供,後續提供  => '後續提供','後續提供'

echo $insert2SQL;

看更多先前的回應...收起先前的回應...

這是兩個input裡面的value

form有設定 method="post"

所以在插入的語法是
$insert2SQL ="INSERT INTO hotel VALUES (NULL,$_SESSION[add_ID],$_POST[hotel],$_POST[room],$_POST[print_time],'待確認','待確認',$_POST[table_num],$_POST[host],$_POST[host_tel],'停車場','0',current_timestamp())";

<td><input type="text" size="20" name="host" value="後續提供"></td>
<td><input type="text" size="20" name="host_tel" value="後續提供"></td>

剛剛 echo insert2SQL; 沒有東西

後來這樣做

$insert2SQL ="INSERT INTO hotel VALUES (NULL,$_SESSION[add_ID],$_POST[hotel],$_POST[room],$_POST[print_time],'待確認','待確認',$_POST[table_num],$_POST[host],$_POST[host_tel],'停車場','0',current_timestamp())";
$myData2 = $myconnect->query($insert2SQL);
echo $myData2."嗨";;

echo $myData2."嗨"; 只有出現 嗨

rogeryao iT邦超人 8 級 ‧ 2020-06-19 14:42:31 檢舉

echo $insert2SQL;
放在
$myData2 = $myconnect->query($insert2SQL);
前面

fillano iT邦超人 1 級 ‧ 2020-06-19 14:45:22 檢舉
$insert2SQL ="INSERT INTO hotel VALUES (NULL,$_SESSION[add_ID],$_POST[hotel],$_POST[room],$_POST[print_time],'待確認','待確認',$_POST[table_num],'$_POST[host]','$_POST[host_tel]','停車場','0',current_timestamp())";

少了單引號就加上去...

另外雖是老生常談:不要直接把input放進sql...

我成功了><
天啊 超級感謝><

已經好幾天了...
看到INSERT ALL看起來可以用,也試了兩天.

rogeryao iT邦超人 8 級 ‧ 2020-06-19 14:57:34 檢舉

寫到資料庫前最好先 echo $insert2SQL; 出來看一下或測一下有沒有問題

froce iT邦大師 1 級 ‧ 2020-06-19 15:34:27 檢舉

這種code最好不要出現...
SQL injection啊。

謝謝froce的提醒,因為之前爬文有看到文章,建議把特殊符號如單引號(‘) 、分號(;)、注釋符號(--)過濾掉
所以在在加入會員的地方有加上

preg_match( '/^([0-9]+)$/', $myac)
preg_match( '/^([A-Za-z0-9]+)$/', $myps)

測試過特殊符號就會出現錯誤了。

這是自己看書&爬文,還在學習中。

謝謝rogeryao我後來會用你的建議先echo出來看看有沒有問題就知道是錯在哪裡了!!非常感謝!!

我要發表回答

立即登入回答