简体   繁体   中英

php pdo error on write, value being set to 0

// Write details to the database
//die("id = ".$country_id); id = au
try
{
    $sql = "INSERT INTO ldm_country (cId, cName) 
                    VALUES(:cty_id, :cty_name)";
    $query = $conn->prepare( $sql );
    $query->execute( array( ':cty_id'=>$country_id,':cty_name'=>$country_name ) );
} catch (PDOException $err) {  
    echo "Database Issue: " . $err->getMessage();
    $conn = null;
    return;
}

When executed am getting

Database Issue: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'

Yet $country_id has a value of "au"????

It looks like the problem may be that cId is an integer field, and au is getting coerced into an int, which is being evaluated as 0.

If that's the case, to fix it you'll need to either change the DB field cId type to something like varchar, or use an int as the country ID instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM