PHP插入数据库

时间:2011-04-09 10:21:32

标签: php sql database checkbox

嘿伙计们,我试图在数据库中插入一些复选框,我非常确定我的代码是正确的,但我一直收到错误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 [ '附加'];     $ arr_num =计数($ idextra);     $ I = 0;     while($ i&lt; $ arr_num)    {

$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();`

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

2 个答案:

答案 0 :(得分:4)

您有以下列:

(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查询以确保它是正确的:)

答案 1 :(得分:1)

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

<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,",");