看板 PHP 關於我們 聯絡資訊
<html> <head> <title>新增通訊錄</title> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> </head> <body> <div align="center"> <form action="addtx.php" name="txform" method="post"> <table border="1"> <tr> <td>姓名:</td> <td><input type="text" name="un"></td> </tr> <tr> <td>手機:</td> <td><input type="text" name="tel"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="addtxbtn" value="新增" ></td> </tr> </table> <?php $con=mysqli_connect("localhost","root","","tongxunlu") or die('連線失敗 '.mysqli_error()); mysqli_select_db($con,"tongxunlu"); //mysql_query("set names gb2312"); //如果頁面字符集設 定為GB2312則需要加上這句 if(!empty($_POST)){ $username=$_POST['un']; $phone=$_POST['tel']; $addtx="insert into txinfo(username,phone) values ('$username','$phone')"; mysqli_query($con,$addtx); } ?> </form> </div> </body> </html> 不好意思,上次沒排好版,這次排好了,請問各位大大,我新增通訊錄的資料按下新增, 資料庫新增的欄位卻是空白的值,比如說我新增姓名為胖虎,電話123,通訊錄卻顯示空 白或0,這是甚麼原因? <html> <head> <title>顯示通訊錄</title> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> </head> <body> <div align="center"> <?php $con=mysqli_connect('localhost','root','','tongxunlu') or die('連線失敗 '.mysql_error()); mysqli_select_db($con,"tongxunlu"); //mysql_query("set names gb2312"); $showtx="select username,phone from txinfo "; $result=mysqli_query($con,$showtx); ?> <table border="1"> <?php $num_rows=mysqli_num_rows($result); if($num_rows>0) { while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) { ?> <tr> <td>姓名</td> <td><?php echo $row["username"]?></td> </tr> <tr> <td>手機</td> <td><?php echo $row["phone"]?></td> </tr> <?php }}?> </table> <a href="phonebook.php">輸入電話</a> </div> </body> </html> 上面是addtx.php http://www.blueshop.com.tw/board/FUM20041006152627A9N/BRD20211110204009MKQ.html -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.143.36.87 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/PHP/M.1636809055.A.561.html ※ 編輯: joinbee (220.143.36.87 臺灣), 11/13/2021 21:20:50
kattte: 1. 確定 $username,$phone 有東西 11/13 21:34
kattte: 2. 確定你組的SQL長啥樣子 11/13 21:34
kattte: 3. 把你在2組的SQL 扔進DB 執行看看 11/13 21:34
kattte: 4. 確定你知道你自己在做甚麼 11/13 21:35
LMGG: 在$username 底下用echo 或 print呼叫出來看看有沒有取得值 11/14 10:14
LMGG: ,有值就查看SQL 組合,一樣呼叫出來查看,如果$username沒 11/14 10:14
LMGG: 值,就回頭查為什麼沒值 11/14 10:14
kattte: ...你這個 form 會post資料給 addtx.php 那你有給我們看 11/14 11:50
kattte: addtx.php裡做了甚麼嗎? 11/14 11:51
joinbee: addtx.php就把資料庫的資料印出來而已,看起來印出來好像 11/14 16:42
joinbee: 沒什麼問題?應該是沒什麼關係?txinfo資料表就兩個欄位, 11/14 16:44
※ 編輯: joinbee (220.143.36.87 臺灣), 11/14/2021 16:48:11 ※ 編輯: joinbee (220.143.36.87 臺灣), 11/14/2021 16:48:49
joinbee: 一個是username一個是phone都是varchar,我有echo$userna 11/14 16:50
joinbee: me跟$phone裡面的確是沒有東西,我在猜會不會是HTML的問題 11/14 16:53
joinbee: ? 11/14 16:53
※ 編輯: joinbee (220.143.36.87 臺灣), 11/14/2021 16:54:41 ※ 編輯: joinbee (220.143.36.87 臺灣), 11/14/2021 16:55:47
zx4109: 你要不要放到 github gist 上或是類似功能的平台 11/14 16:56
zx4109: 這樣大家能比較方便看 11/14 16:57
※ 編輯: joinbee (220.143.36.87 臺灣), 11/14/2021 16:57:50 ※ 編輯: joinbee (220.143.36.87 臺灣), 11/14/2021 17:12:07
joinbee: 問題解決了,把addtx.php放到同一個php底下一切就正常了 11/14 19:36
LMGG: 前端送資料去addtx.php,要把接收資料的程式碼放到那裏才對 11/14 19:37