【问题标题】:PHP inserting into databasePHP插入数据库
【发布时间】:2011-04-09 10:21:32
【问题描述】:

大家好,我正在尝试将一些复选框插入数据库,我很确定我的代码是正确的,但是我一直收到错误 ERROR INSERTING: Column count doesn't match value count at row 1

基本上我将每个复选框添加到数据库中的不同列

这是我的代码

    $idextra=$_POST['extras'];
    $arr_num=count($idextra);
    $i=0;
    while ($i < $arr_num)
   {

    $qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine) VALUES ('$idextra[$i]')";
    $res=mysql_query($qu) or die('ERROR INSERTING: '.mysql_error());
      $i++;
    }

大家好,这里是我的复选框和联系表单的 HTML。

`

          <tr>
            <td height="30" align="right" class="align_right">Your Name*:&nbsp;</td>
            <td>
              <input type="text" name="name" id="name" value="<?php echo $name?>"  onchange="checkFieldBack(this)"/>
            </td>
          </tr>
          <tr>
            <td height="30" align="right" class="align_right">Phone*:&nbsp;</td>
            <td><input type="text" name="phone" id="phone" value="<?php echo $phone?>"  onchange="checkFieldBack(this)" onkeyup="noAlpha(this)"/></td>
          </tr>

          <tr>
            <td height="30" align="right" class="align_right">E-mail*:&nbsp;</td>
            <td><input type="text" name="email" id="email"  value="<?php echo $email?>" onchange="checkFieldBack(this);"/></td>
          </tr>

          <tr>
            <td align="right"  valign="top" class="align_right">Address*:&nbsp;</td>
            <td><textarea name="comments" id="comments" cols="15" rows="5" onchange="checkFieldBack(this)"><?php echo $comments?></textarea></td>
          </tr>

    <tr>
<td width="236" height="25" align="left">Drop off at:</td>
<td width="548" height="23"><select name="dropoff">

             <option value="05:00" <?php echo $dropoff=="05:00"?"selected":""?>>05:00</option>
             <option value="06:00" <?php echo $dropoff=="06:00"?"selected":""?>>06:00</option>
             <option value="07:00" <?php echo $dropoff=="07:00"?"selected":""?>>07:00</option>
             <option value="08:00" <?php echo $dropoff=="08:00"?"selected":""?>>08:00</option>
             <option value="09:00" <?php echo $dropoff=="09:00"?"selected":""?>>09:00</option>                      
             <option value="10:00" <?php echo $dropoff=="10:00"?"selected":""?>>10:00</option>
             <option value="11:00" <?php echo $dropoff=="11:00"?"selected":""?>>11:00</option>
             <option value="12:00" <?php echo $dropoff=="12:00"?"selected":""?>>12:00</option>
             <option value="13:00" <?php echo $dropoff=="13:00"?"selected":""?>>13:00</option>
             <option value="14:00" <?php echo $dropoff=="14:00"?"selected":""?>>14:00</option>
             <option value="15:00" <?php echo $dropoff=="15:00"?"selected":""?>>15:00</option>
             <option value="16:00" <?php echo $dropoff=="16:00"?"selected":""?>>16:00</option>
             <option value="17:00" <?php echo $dropoff=="17:00"?"selected":""?>>17:00</option>
             <option value="18:00" <?php echo $dropoff=="18:00"?"selected":""?>>18:00</option>
             <option value="19:00" <?php echo $dropoff=="19:00"?"selected":""?>>19:00</option>


  </select> 
      </td>


    <tr>
            <td height="10" align="right" class="align_right">Deodoriser:&nbsp;</td>
            <td>
              <input type="checkbox" name="extras[]" id="deodoriser" value="Deodoriser>"/>
            </td>
          </tr>

<tr>
            <td height="30" align="right" class="align_right">Carpet Protector (5 litre):&nbsp;</td>
            <td>
              <input type="checkbox" name="extras[]" id="carpet" value="Carpet Protector (5 litre)"/>
            </td>
          </tr>
<tr>
            <td height="30" align="right" class="align_right">Carpet Repair Tools:&nbsp;</td>
            <td>
              <input type="checkbox" name="extras[]" id="carpetrepair" value="Carpet Repair Tools"/>
            </td>
          </tr>
<tr>
            <td height="30" align="right" class="align_right">Furniture Moving Equipment:&nbsp;</td>
            <td>
              <input type="checkbox" name="extras[]" id="furniture" value="Furniture Moving Equipment"/>
            </td>
          </tr>
<tr>
            <td height="30" align="right" class="align_right">Furniture Tabs:&nbsp;</td>
            <td>
              <input type="checkbox" name="extras[]" id="tabs" value="Furniture Tabs"/>
            </td>
          </tr>
<tr>
            <td height="30" align="right" class="align_right">Urine Decontamination Treatment:&nbsp;</td>
            <td>
              <input type="checkbox" name="extras[]" id="urine" value="Urine Decontamination Treatment"/>
            </td>
          </tr>  

`

这是我插入数据库的完整 php 代码

`$idextra=$_POST['extras']; $arr_num=count($idextra); $i=0; 而 ($i

$qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine) VALUES ('{$idextra[1]}','{$idextra[2]}','{$idextra[3]}','{$idextra[4]}','{$idextra[5]}','{$idextra[6]}')";
$res=mysql_query($qu) or die('ERROR INSERTING: '.mysql_error());
  $i++;
}


$q="INSERT INTO bs_reservations (dateCreated, name, email, phone, comments,status,eventID, qty,dropoff) VALUES (NOW(),'".$name."','".$email."','".$phone."','".$comments."','2','".$eventID."','".$qty."','".$dropoff."')";
$res=mysql_query($q) or die("error!");
$orderID=mysql_insert_id();`

我基本上想获取用户选择的所有输入并将它们插入到数据库中。

【问题讨论】:

  • 这是问题 bz 值没有用逗号分隔 ',' ??
  • 我也喜欢没有验证/清理裸 $_POST 变量的方式;)
  • 抱歉,您的代码正确。你正在做的正是 mysql 正在哭泣的事情。如果您告诉我们您想要做什么,我们可以为您提供帮助
  • 你能粘贴你的 html 代码来生成复选框吗?顺便说一句,您只计算预期按正确顺序发布的值,当 html 表单代码更改时,这可能会给您带来严重/持续的问题。

标签: php sql database checkbox


【解决方案1】:

你有这些列:

(deodoriser,carpet,carpetrepair,furniture,tabs,urine)

你正在插入这个:

'$idextra[$i]'

这是 6 列和 1 个值。正如错误所说,那不一样。

你的意思可能是这样的:

('{$idextra[1]}','{$idextra[2]}','{$idextra[3]}','{$idextra[4]}','{$idextra[5]}','{$idextra[6]}')

如果你想预先从你的数组中创建一个字符串,使用类似这样的东西 implode

$yourString = implode("','",$idextra);
$qu="INSERT INTO bs_reservations (deodoriser,carpet,carpetrepair,furniture,tabs,urine)
      VALUES ('{$yourString}')";

echo 查询以确保它是理智的:)

【讨论】:

  • 这样做可行,但是有 6 个复选框,如果客户只选择前 2 个和最后一个,则在插入数据库时​​,它会将最后一个复选框插入第 3 列
  • 复选框的一个技巧是:在复选框之前添加一个值为 0 的隐藏字段/输入。如果选中,您的值将被第二个字段覆盖,并获得 1(或 true)。如果第二个没有被检查,它会被表单忽略,但第一个(隐藏的)不是,所以它传递了它的 0.presto,6 个值,大家都很高兴:D
  • 如何添加“隐藏值”只需添加一个值 ='0'
  • &lt;input type="hidden" name="Name" value="0"/&gt;
  • 输入类型不是一定要checkbox吗?
【解决方案2】:

不要为您的复选框使用数组,而是为它们指定不同的名称,例如

<input type="checkbox" name="carpetrepair" id="carpetrepair" value="Carpet Repair Tools"/>

然后检查是否有任何被检查:

$options = explode(",","deodoriser,carpet,carpetrepair,furniture,tabs,urine");
$sql = "INSERT INTO bs_reservations SET ";
foreach($options as $opt){
  if (isset($_POST[$opt])) {
    $sql.= "`$opt`=1,";
  }
}
$sql = rtrim($sql,",");

【讨论】:

  • 很抱歉这么晚了,但我只是想知道这实际上是如何工作的。它实际上会为每个变量分配一个名称并将其插入数据库还是我需要添加更多代码
猜你喜欢
  • 2013-09-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 2020-06-07
  • 1970-01-01
相关资源
最近更新 更多