2013-08-23 134 views
-1

列數與第1行的值不匹配我試圖使用數組傳遞數據到數據庫,但我不斷收到此錯誤。我仔細檢查了我的代碼,看看我是否錯過了一列,但看起來並不像它。 我在做什麼錯?當列INSERT INTO

<?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> 
+0

你應該插入'7'值。 –

回答

1

它看起來像你傳遞的「提交」值,太。

輸出$clean_info到屏幕(在運行查詢之前)進行調試。

0

發出要發送到數據庫的查詢文本。

insert語句列出了7列,:

INSERT INTO cars 
(date, stock, year, make, model, vin, cr) 

因此,有需要在值列表,例如七個值

VALUES ('2013-08-22','stk','''74','Dodge','Charger','12345','1500') 

我懷疑串聯(string)$clean_info到SQL文本不會產生有效的語句。這只是基本的調試。將該SQL語句生成一個字符串,然後echo(或vardump)該字符串,以便查看傳遞給數據庫的內容。


隨着mysqli的界面,你可以使用paramaterized查詢,這確實是一個更好的辦法。 (您不提供作爲綁定參數提供的值的mysqli_real_escape_string)

例如,

$sqltext = "INSERT INTO cars(date,stock,year,make,model,vin,cr) VALUES (?,?,?,?,?,?,?)"; 
if ($stmt = $mysqli->prepare($sqltext)) { 
    $stmt->bind_param("sssssss",$date,$stock,$year,$make,$model,$vin,$cr); 
    $stmt->execute();