无法插入SQL表或显示全部

时间:2012-03-05 19:27:21

标签: php mysql sql

我似乎无法访问我的桌子。再一次,我确信它的东西很简单,我忽略了。我没有收到任何错误。表中没有添加任何内容,也没有显示任何内容。我已经多次重命名所有内容,确保连接良好,确保表存在。我看不出有什么不妥。

将数据添加到表

    // Valid Data
    require_once('resources/php/db.php');
    $id = uniqid();

    //die ( $id . $name . $sex. $age. $hair. $eye. $skin. $body. $pf );

    // Insert Data to the Table
    $statement1 = $db->prepare('INSERT INTO player VALUES(:player_id, :name, :sex, :age, :hair, :eye, :skin, :body, :pf)');
    $result1 = $statement1 -> execute(array(
        ':player_id' =>$id,
        ':name' =>$name,
        ':sex' =>$sex,
        ':age' =>$age,
        ':hair' =>$hair,
        ':eye' =>$eye,
        ':skin' =>$skin,
        ':body' =>$body,
        ':pf' =>$pf
    ));

    // Make Sure Everything Worked
    if( $result1 == false )
    {
        die('Update Failed, Please Check Your Database.');
    }

    header("Location: ../../new_success.php?id=$id;");
    exit();

成功页面

 // Start the Load
$query1 = "SELECT * 
           FROM player
           WHERE player_id = :player_id";

$statement1 = $db->prepare($query1);
$statement1 -> execute(array(
    ':player_id' =>$id
));

// Make Sure the Data Exists
if( $statement1->rowCount() == 0 )
{
    die('Please Enter a Valid ID Tag - (id)');
}
else
{
    $notEmpty = true;   
}


while($row = $statement1->fetch())
{
    $name = $row['name'];
    $sex = $row['sex'];
    $age = $row['age'];
    $hair = $row['hair'];
    $eye = $row['eye'];
    $skin = $row['skin'];
    $body = $row['body'];
    $pf = $row['pf'];
}

1 个答案:

答案 0 :(得分:1)

您似乎正在使用。如果是这种情况,您在:方法中遗漏了execute。它应该是:player_id而不是player_id

$statement1 -> execute(array(
    ':player_id' =>$id
));

同样的方式,第二个查询也不合适。

$result1 = $statement1 -> execute(array(
        ':player_id' =>$id,
        ':name' =>$name,
         ...
    ));

php.net example