douwen7603 2013-08-23 01:28
浏览 208

INSERT INTO时,列数与第1行的值计数不匹配

I am trying to pass the data into the data base using an array, but I keep getting this error. I checked my code carefully to see if I was missing a column but it doesn't look like it. What am I doing wrong?

<?php
class inventario {
  public function __construct() {}

  public function insertar($info) {
    if(isset($info)) {
      $db_host = 'localhost';
      $db_user = 'root';
      $db_pass = 'root';
      $db_name = 'inventory_cars';

      $db_link = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die('No Connection');
      $clean_info = mysqli_real_escape_string($db_link, array_values($info));

      $query = mysqli_query( $db_link, "INSERT INTO cars(date, stock, year, make, model, vin, cr) VALUES('" . (string)$clean_info. "')") or die(mysqli_error($db_link));
      if($query) {
        return 'Record inserted';
      }

      mysqli_close($db_link);
    }
    else {
      echo 'info variable not set';
    }
  }

  public function table() {
    $action = $_SERVER['PHP_SELF'];
    $table = '<form name="insertar" method="post" action="' . $action . '"><table><tr><td>Date</td><td><input type="date" name="date"/></td></tr><tr><td>Stock#</td><td><input type="text" name="stock"/></td></tr><tr><td>Year:</td><td><input type="text" name="year"/></td></tr><tr><td>Make:</td><td><input type="text" name="make"/></td></tr><tr><td>Model:</td><td><input type="text" name="model"/></td></tr><tr><td>VIN:</td><td><input type="text" name="vin"/></td></tr><tr><td>CR:</td><td><input type="text" name="cr"/></td></tr><tr><td><input type="submit" value="Submit" name="submit"/></td></tr></table></form>';
    return $table;
  }

  public function stock_list() {
    $db_host = 'localhost';
    $db_user = 'root';
    $db_pass = 'root';
    $db_name = 'inventory_cars';

    $db_link = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die('No Connection');

    $query = "SELECT * FROM cars";
    $result = mysqli_query($db_link, $query) or die('Query fail.. Please wait...');

    while($row = mysqli_fetch_array($result)) {
      echo '<tr><td>' . $row['date'] . '</td><td>' . $row['year'] . '</td><td>' . $row['make'] . '</td><td>' . $row['model'] . '</td><td>' . $row['vin'] . '</td><td><a href="' . $row['cr'] . '">CR</a></td></tr>';
    }

    mysqli_close($db_link);
  }
}
?>



<?php
require('inventoryControl.php');
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Inventory</title>
  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>
<body>
  <div id="wraper">
    <div class="add_item">

    <?php
    $inventario = new inventario;
    echo $inventario->table();
    if (isset($_POST['submit'])) {
      $info = array('date' => $_POST['date'], 'stock' => $_POST['stock'], 'year' => $_POST['year'], 'make' => $_POST['make'], 'model' => $_POST['model'], 'vin' => $_POST['vin'], 'cr' =>  $_POST['cr']);
      if(isset($info)) {
        $inventario->insertar($info);
      }
      else {
        echo 'variable not set';
      }
    }
    ?>

     </div><!--end add item -->
     <div class="inventory_list">
     <?php
       echo '<table>';
       $inventario->stock_list();
       echo '</table>';
     ?>
     </div><!--end inventory_list -->
  </div><!--end wraper -->

</body>
</html>
  • 写回答

2条回答 默认 最新

  • dongqiongzheng0615 2013-08-23 01:30
    关注

    It looks like you're passing the "submit" value, too.

    Output $clean_info to the screen (before you run the query) to debug it.

    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教