請教各位,有關於無法寫入資料庫的問題

edited 十月 2013 in PHP新手區
小弟最近在做專題,需要用到php連結資料庫
以下是程式碼
<?
$link = mysql_connect("localhost","m******","********");
mysql_select_db("m******",$link);

session_start();
$r_id=$_SESSION;
$pass=$_SESSION;
$nano=$_POST["nano"];
$profound=$_POST["profound"];
$sex=$_POST["sex"];
$nature=$_POST["nature"];
$white=$_POST["white"];
$reduce=$_POST["reduce"];
$tight=$_POST["tight"];
$thorough=$_POST["thorough"];
$acne=$_POST["acne"];
$spot=$_POST["spot"];
$water=$_POST["water"];
$irritation=$_POST["irritation"];
$dark=$_POST["dark"];
$cuticle=$_POST["cuticle"];
$antioxidant=$_POST["antioxidant"];
$allay=$_POST["allay"];
$nourishment=$_POST["nourishment"];
$notdry=$_POST["notdry"];
$mild=$_POST["mild"];
$rinse=$_POST["rinse"];
$remain=$_POST["remain"];
$relaxed=$_POST["relaxed"];
$greasy=$_POST["greasy"];
$furnish=$_POST["furnish"];
$attitude=$_POST["attitude"];
$newproduct=$_POST["newproduct"];
$newarticles=$_POST["newarticles"];
$bounds=$_POST["bounds"];
$query="insert into number_weight (id,password,nano,profound,sex,nature,white,reduce,tight,thorough,acne,spot,water,irritation,dark,cuticle,antioxidant,allay,nourishment,notdry,mild,rinse,remain,relaxed,greasy,furnish,attitude,newproduct,newarticles,bounds) values('$r_id','$pass','$nano','$profound','$sex','$nature','$white','$reduce','$tight','$thorough','$acne','$spot','$water','$irritation','$dark','$cuticle','$antioxidant','$allay','$nourishment','$notdry','$mild','$rinse','$remain','$relaxed','$greasy','$furnish','$attitude','$newproduct','$newarticles','$bounds')";

echo $query;
if (mysql_query($query))
echo "ok";
else
echo "no";
?>

執行之後出現了以下的字樣
insert into number_weight (id,password,nano,profound,sex,nature,white,reduce,tight,thorough,acne,spot,water,irritation,dark,cuticle,antioxidant,allay,nourishment,notdry,mild,rinse,remain,relaxed,greasy,furnish,attitude,newproduct,newarticles,bounds) values('aaaa','aaaa','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1')no

我所使用的phpMyAdmin版本為2.6.0-pl3,MySQL 版本 4.1.10-nt
請問是哪邊發生了問題?煩請各位先知解答,感激不盡!

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=1905

評論

  • edited 八月 2006
    可以將 $query 的內容直接輸入 phpMyAdmin 看錯誤訊息,或是在 echo 'no' 時也 echo mysql_error();
  • edited 八月 2006
    insert into number_weight (id,password,nano,....

    應該是這樣吧

    insert into number_weight values(id,password,nano,.....

    @...加加values看這個看有沒有用
  • edited 八月 2006
    values是加在欄位與資料之間
    建議照kiang說的
    把mysql_error echo出來比較好找問題
  • edited 八月 2006
    @哈..我看錯了

    那把('$r_id','$pass','$nano','$profound','$sex',.....
    改成('".$r_id."','".$pass."','".$nano."','".$profound."','".$sex."',.....
    我通常都是用下面這種寫法,看看這樣行不行
  • edited 八月 2006
    感謝以上各位先知的解答。

    另外,我照著 kiang 兄的方式去做:

    1、phpMyAdmin的錯誤訊息為
    MySQL 傳回: 說明文件
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'i' at line 1
    

    2、加入echo mysql_error();之後執行的畫面為
    insert into number_weight (id,password,nano,profound,sex,nature,white,reduce,tight,thorough,acne,spot,water,irritation,dark,cuticle,antioxidant,allay,nourishment,notdry,mild,rinse,remain,relaxed,greasy,furnish,attitude,newproduct,newarticles,bounds) values('qwe','qwe','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1')noUnknown column 'sex' in 'field list'
    

    請問這是哪邊有問題呢?
  • edited 八月 2006


    由於當初設立資料庫的時候,我朋友一個粗心,把有個欄位設定錯誤了,所以才會造成這樣。

    不過還是很感謝各位先知們的解答!
Sign In or Register to comment.