2013-02-19 43 views
1

這裏是我的數組:列計數與第1行的值計數不匹配 - 但是我有27列和27個值? PHP MySQL錯誤

$person = array(
    "fullname" => $fn, 
    "skin_shade" => $_POST['skin_shade'], 
    "acne" => $_POST['acne'], 
    "dry_skin" => $_POST['dry_skin'], 
    "oily_skin" => $_POST['oily_skin'], 
    "wrinkles_aging" => $_POST['wrinkles_aging'], 
    "sensative_skin" => $_POST['sensative_skin'], 
    "darkspots" => $_POST['darkspots'], 
    "hair_type" => $_POST['hair_type'], 
    "parabens" => $_POST['parabens'], 
    "sulfates" => $_POST['sulfates'], 
    "mineral_oil" => $_POST['mineral_oil'], 
    "silicones" => $_POST['silicones'], 
    "relaxed" => $_POST['relaxed'], 
    "colortreated" => $_POST['colortreated'], 
    "thinning" => $_POST['thinning'], 
    "growth" => $_POST['growth'], 
    "braidout" => $_POST['braidout'], 
    "roller" => $_POST['roller'], 
    "wng" => $_POST['wng'], 
    "heat" => $_POST['heat'], 
    "wig" => $_POST['wig'], 
    "braid" => $_POST['braid'], 
    "dreadlocks" => $_POST['dreadlocks'], 
    "henna" => $_POST['henna'], 
    "hair_color" => $_POST['hair_color'], 
    "hair_style" => $_POST['hair_style'], 
); 

這裏是我嘗試插入,並得到了錯誤:

$columns = implode(", ",array_keys($person)); 
$escaped_values = array_map('mysql_real_escape_string', array_values($person)); 
$values = implode(", ", $escaped_values); 
$sql = "INSERT INTO people ($columns) VALUES ('$values')"; 
mysql_query($sql) or die (mysql_error()); 

我還用在列和值的print_r確保它們的大小相同:

print_r($columns); echo"</br></br>"; 
print_r($values); 

這裏是輸出我得到爲:

fullname, skin_shade, acne, dry_skin, oily_skin, wrinkles_aging, sensative_skin, darkspots, hair_type, parabens, sulfates, mineral_oil, silicones, relaxed, colortreated, thinning, growth, braidout, roller, wng, heat, wig, braid, dreadlocks, henna, hair_color, hair_style

Chris Runo, 2, No, Yes, No, Yes, Yes, No, Straight, No, No, No, No, No, No, No, Yes, No, No, No, No, No, No, No, No, dark_brown, classic

我也檢查了我的MySQL表,並有27列。

回答

5
$sql = "INSERT INTO people ($columns) VALUES ('$values')"; 

這是打算把一個字符串文字到VALUES子句,包含一個逗號分隔值列表一個單引號字符:

INSERT INTO people (...columns...) VALUES ('Chris Runo, 2, No, Yes, No, Yes, Yes, No, Straight, No, No, No, No, No, No, No, Yes, No, No, No, No, No, No, No, No, dark_brown, classic') 

爲了解決這個問題,你可以寫你的自己的報價/避開功能和了在array_map()使用它:

function myquote($val) 
{ 
    return "'" . mysql_real_escape_string($val) . "'"; 
} 

$escaped_values = array_map('myquote', array_values($person)); 

$values = implode(", ", $escaped_values); 
$sql = "INSERT INTO people ($columns) VALUES ($values)"; 

否則,你可以拋棄過時的mysql_ *功能,並使用PDO,WHI ch使編寫SQL注入安全的查詢更容易:

$columns = implode(", ",array_keys($person)); 
$params = implode(",", array_fill(0, count($person), "?")); 

$sql = "INSERT INTO people ($columns) VALUES ($params)"; 
$stmt = $pdo->prepare($sql) or die(print_r($pdo->errorInfo(), true)); 
$stmt->execute(array_values($people)) or die(print_r($stmt->errorInfo(), true)); 
0

您需要將您的值與''分開。所以把你的內爆改爲:

$values = implode("', '", $escaped_values); 
相關問題