【问题标题】:INSERT SQL statement is not workingINSERT SQL 语句不起作用
【发布时间】:2012-09-28 01:00:53
【问题描述】:

我在 PostgreSQL 系统上。我以前用过,没有遇到过这个问题,但那是基本的选择查询。

现在我正在尝试通过 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 语句来检查它是否真的被插入了?

【问题讨论】:

    标签: php sql forms postgresql insert


    【解决方案1】:

    以您现在的方式使用 INSERT,您必须确保 VALUES () 子句的内容与表中列的数量和顺序完全匹配。如果有任何列不包括在内,即使它们具有默认值或自动编号,该命令也会失败。由于您没有显示有关您的表的任何信息,我不知道这是否是您的问题的根源。

    如果是问题,可以使用完整形式的INSERT:

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

    在任何情况下都强烈建议使用此表单,因为它不对表(除了列的名称)做任何假设,并防止命令因稍后对表的结构更改而失败。

    【讨论】:

    • @larry_lustig 感谢您的回复。我在其中添加了列,尽管我不认为这是问题所在。该表有 15 列,只有一些字段是必需的(非空)。也许这是我的 html 出于某种原因将其发送到 php 的问题?我现在正在更新我的问题以包含整个代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2016-06-10
    • 2012-02-17
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多