php - 不将变量加载到数据库中

时间:2013-02-17 19:01:03

标签: php

我有一个数据库表 - serial(自动增量主键),版本和turk_number。我使用以下代码插入一个新行。我通过$ _GET接收这些变量,我打印输出,所以我知道变量是可用的,所以我不确定什么是错的。序列和版本已加载,但不是turk_number。

$turk_number ='';
$serial='';
$version='';
if(isset($_GET['serial']))
{
    $serial=$_GET['serial'];
    $_SESSION['serial'] = $serial;
}
if(isset($_GET['version']))
{
    $version = $_GET['version'];
    $_SESSION['version'] = $version;
    print "version=" . $version;
}
if(isset($_GET['turk_number']))
{
    $turk_number= $_GET['turk_number'];
    $_SESSION['turk_number'] = $turk_number;
    print "turk number=".$turk_number;
}

//this assigns a participant a unique serial id at the beginning of the game
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "resolver";

try 
{
print 'turk2=' . $turk_number;
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $query2 = "INSERT INTO participants (version, turk_number) VALUES (:version, :turk_number)";
        $stmt = $dbh ->prepare($query2);
        $stmt ->execute(array(':version' => $version,
                            ':turk_number' => $turk_number));

}
catch(PDOException $e)
{
    echo $e->getMessage();
}   

1 个答案:

答案 0 :(得分:-1)

你忘记了引号。

变化:

$query2 = "INSERT INTO participants (version, turk_number) VALUES (:version, :turk_number)";

为:

$query2 = "INSERT INTO participants (version, turk_number) VALUES (':version', ':turk_number')";