[請益] PHP表單Update資料庫

看板PHP作者 (strifecloud)時間11年前 (2012/10/02 07:43), 編輯推噓2(205)
留言7則, 3人參與, 最新討論串1/1
小弟現在正在學習用表單來Update資料庫/資料表裡面的資料 我是先用書本上的範例去改,但一直無法成功Update資料表裡的資料 (基本上是可以成功顯示,但是在submit之後雖然會成功跳回指定的頁面但是資料表中的資料 並沒有成功Update) 以下是我寫的PHP(Update的部分),懇請大大們賜教,已經為這個問題試了好一陣子了... 謝謝~! <?php include("connMysql.php"); if (!mysql_select_db("mlbdata")) die("fail!"); if(isset($_POST["action"])&&($_POST["action"]=="update")){ $sql_query = "UPDATE `mlbplayers` SET cName='".$_POST["cName"]."', WHERE cID=".$_POST["cID"]; mysql_query($sql_query); header("Location:data.php"); } $sql_db = "SELECT * FROM `mlbplayers` WHERE `cID`=".$_GET["id"]; $result = mysql_query($sql_db); $row_result=mysql_fetch_assoc($result); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>playerdata</title> </head> <body> <form action="" method="post" name="formFix" id="formFix"> <table border="1" align="center" cellpadding="4"> <tr> <th>Table</th><th>data</th> </tr> <tr> <td>Name</td><td><input type="text" name="cName" id="cName" value="<?php echo $row_result["cName"];?>"></td> </tr> <tr> <td colspan="2" align="center"> <input name="cID" type="hidden" value="<?php echo $row_result["cID"];?>"> <input name="action" type="hidden" value="update"> <input type="submit" name="button" id="button" value="sumit"> <input type="reset" name="button2" id="button2" value="reset"> </td> </tr> </table> </form> </body> </html> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.25.212

10/02 08:51, , 1F
WHERE前面多個「,」
10/02 08:51, 1F

10/02 08:56, , 2F
建議:mysql_query($sql_query);可改寫為
10/02 08:56, 2F

10/02 08:56, , 3F
if(mysql_query($sql_query)){
10/02 08:56, 3F

10/02 08:57, , 4F
header("location:data.php");}
10/02 08:57, 4F

10/02 15:58, , 5F
sql有問題 通常先把那段sql echo出來 再直接丟到admin那
10/02 15:58, 5F

10/02 15:59, , 6F
執行 確定語法沒問題再找php哪裡有錯@@
10/02 15:59, 6F

10/04 07:42, , 7F
如同上面大大所說找到問題了!!感謝!
10/04 07:42, 7F
文章代碼(AID): #1GQYg-mX (PHP)