2012-09-28 79 views
-1

我在PostgreSQL系統上。我之前使用過它,並沒有遇到這個問題,但那是基本的選擇查詢。INSERT SQL語句不起作用

現在我試圖通過HTML表單和php插入數據。

我沒有得到一個錯誤回來時,我填寫的數據(甚至當我輸入表單上的所有字段),所以我認爲它應該被插入....

這裏是我的PHP/HTML:

<div id='insert_form'> 
<form method='POST' action='insert.php' onsubmit='return checkSubmit()'> 
    <label id='lbl_header'><b>Bold</b> fields are required.</label> 
    <br /><br /> 
    <label class='required' for='code1' id='lbl_code1'>* Country code</label> 
    <input type='text' name='code1' id='code1' maxlength='3' size='3'/> 
    <br /><br /> 
    <label class='required' for='code2' id='lbl_code2'>* Country code (abbr.)</label> 
    <input type='text' name='code2' id='code2' maxlength='2' size='2'/> 
    <br /><br /> 
    <label class='required' for='country_name' id='lbl_name'>* Country name</label> 
    <input type='text' name='country_name' id='country_name' maxlength='52'/> 
    <br /><br /> 
    <label class='required' for='continent' id='lbl_continent'>* Continent</label> 
    <select name='continent' id='continent'> 
    <option value='Africa'>Africa</option> 
    <option value='Antarctica'>Antarctica</option> 
    <option value='Asia'>Asia</option> 
    <option value='Europe'>Europe</option> 
    <option value='North America'>North America</option> 
    <option value='Oceania'>Oceania</option> 
    <option value='South America'>South America</option> 
    </select> 


    <br /><br /> 
    <label class='required' for='region' id='lbl_region'>* Region</label> 
    <input type='text' name='region' id='region' maxlength='26' /> 
    <br /><br /> 
    <label class='required' for='area' id='lbl_area'>* Surface area</label> 
    <input type='text' name='area' id='area' /> 
    <br /><br /> 
    <label for='indepYear' id='lbl_year'>Year of independence</label> 
    <input type='text' name='indepYear' id='indepYear' /> 
    <br /><br /> 
    <label class='required' for='population' id='lbl_pop'>* Population</label> 
    <input type='text' name='population' id='population' /> 
    <br /><br /> 
    <label for='lifeExp' id='lbl_lifeExp'>Life expectancy</label> 
    <input type='text' name='lifeExp' id='lifeExp' /> 
    <br /><br /> 
    <label for='gnp' id='lbl_gnp'>GNP</label> 
    <input type='text' name='gnp' id='gnp' /> 
    <br /><br /> 
    <label for='gnp_old' id='lbl_gnp_old'>GNP (old)</label> 
    <input type='text' name='gnp_old' id='gnp_old' /> 
    <br /><br /> 
    <label class='required' for='local_name' id='lbl_local_name'>* Local name</label> 
    <input type='text' name='local_name' id='local_name' maxlength='45' /> 
    <br /><br /> 
    <label class='required' for='govt' id='lbl_govt'>* Form of government</label> 
    <input type='text' name='govt' id='govt' maxlength='45' /> 
    <br /><br /> 
    <label for='HoS' id='lbl_HoS'>Head of state</label> 
    <input type='text' name='HoS' id='HoS' maxlength='60' /> 
    <br /><br /> 
    <label for='capital' id='lbl_capital'>Capital code</label> 
    <input type='text' name='capital' id='capital' /> 
    <br /><br /> 
    <label><a href='index.php'>Return to Selection Page</a></label> 
    <input type='submit' name='submit' value='Insert row' /> 
</form> 


?> 

連接文件已被證明在我過去的項目中工作,所以我知道這不是問題。我已經編譯它,它也說這不是問題。任何想法爲什麼它不會被插入到數據庫中?或者,也許我可以添加一個函數結束if語句,可以檢查它是否實際上已被插入?

回答

3

以您所使用的方式使用INSERT,您必須確保VALUES()子句的內容與表中列的數量和順序完全匹配。如果有任何不包含的列,即使它們具有默認值或自動編號,該命令也會失敗。既然你沒有顯示任何有關你的表格的信息,我不知道這是否是你問題的根源。

如果是這個問題,你可以使用INSERT的完整形式:

INSERT INTO county (col1, col2, col3) VALUES ($val1, $val2, $val3) 

該形式在任何情況下強烈推薦,因爲它不進行關於表的假設(比列的名稱等),並且防止後來的結構變化失敗。

+0

@larry_lustig謝謝你的迴應。雖然我不認爲這是問題,但我添加了列。該表有15列,只有一些字段是必需的(非空)。也許它與我的HTML無論出於什麼原因發送到PHP的問題?我正在更新我的問題以包含整個代碼 – ZAX