查询不插入没有错误的数据

时间:2015-08-25 20:44:49

标签: php mysql sql prepared-statement

我被告知我的查询错误,但我没有收到任何错误。没有任何东西被插入我的数据库,所以我不怀疑它是错的,虽然我没有看到语法中有什么问题。

$stmt2 = $con->prepare("INSERT INTO user_players (player1, player2, player3, player4, player5, player6, player7, player8, player9, player10, player11, player12, player13) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) WHERE (user_id=?)");

有人看到我遗失的任何东西吗?

更新以显示更多代码以更好地理解这一点..

<form action="" method="POST">
<?php
$con = mysqli_connect("localhost", "", "", "");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$stmt = mysqli_query($con,"SELECT * FROM user_players");

if(isset($_POST['Add Player'])){
//Variables to post in the bind_param
$insert_user_id = $_POST['user_id'];
$insert_player1 = $_POST['player1'];
$insert_player = $_POST['player2'];
$insert_player = $_POST['player3'];
$insert_player = $_POST['player4'];
$insert_player = $_POST['player5'];
$insert_player = $_POST['player6'];
$insert_player = $_POST['player7'];
$insert_player = $_POST['player8'];
$insert_player = $_POST['player9'];
$insert_player = $_POST['player10'];
$insert_player = $_POST['player11'];
$insert_player = $_POST['player12'];
$insert_player = $_POST['player13'];
$stmt2 = $con->prepare("INSERT INTO user_players (player1, player2, player3, player4, player5, player6, 
player7, player8, player9, player10, player11, player12, player13) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) WHERE (user_id=?)");
if ( false===$stmt2 ) {
    die('Player Insert prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt2->bind_param('isssssssssssss', $insert_user_id, $insert_player1, $insert_player2, $insert_player3, $insert_player4, $insert_player5, $insert_player6, 
$insert_player7, $insert_player8, $insert_player9, $insert_player10, $insert_player11, $insert_player12, $insert_player13);
if ( false===$stmt2 ) {
    die('Player Insert bind_param() failed: ' . htmlspecialchars($stmt2->error));
}
$stmt2->execute();
if ( false===$stmt2 ) {
    die('Player Insert execute() failed: ' . htmlspecialchars($stmt2->error));
}
}
while($row = mysqli_fetch_array($stmt)) {
    $player1 = $row['player1'];
    $player2 = $row['player2'];
    $player3 = $row['player3'];
    $player4 = $row['player4'];
    $player5 = $row['player5'];
    $player6 = $row['player6'];
    $player7 = $row['player7'];
    $player8 = $row['player8'];
    $player9 = $row['player9'];
    $player10 = $row['player10'];
    $player11 = $row['player11'];
    $player12 = $row['player12'];
    $player13 = $row['player13'];
?>
    <div class="draftResultsWrap">
        <div class="inline">
        <?php echo "<div>" . $row['firstname'] . " " . $row['lastname'] . "</div>"; ?>
        </div>
            <input class="draftBorder" value='<?php echo $player1; ?>'/>
            <input class="draftBorder" value='<?php echo $player2; ?>'/>
            <input class="draftBorder" value='<?php echo $player3; ?>'/>
            <input class="draftBorder" value='<?php echo $player4; ?>'/>
            <input class="draftBorder" value='<?php echo $player5; ?>'/>
            <input class="draftBorder" value='<?php echo $player6; ?>'/>
            <input class="draftBorder" value='<?php echo $player7; ?>'/>
            <input class="draftBorder" value='<?php echo $player8; ?>'/>
            <input class="draftBorder" value='<?php echo $player9; ?>'/>
            <input class="draftBorder" value='<?php echo $player10; ?>'/>
            <input class="draftBorder" value='<?php echo $player11; ?>'/>
            <input class="draftBorder" value='<?php echo $player12; ?>'/>
            <input class="draftBorder" value='<?php echo $player13; ?>'/>
        </div>

2 个答案:

答案 0 :(得分:1)

在插页上使用where == AnyKey,这可能是你的问题。

由于我不确定您是否正在尝试更新或插入,我会说要使用UPSERT,请参阅this SO (stackoverflow) Q/A

我认为......在不知道?,?,?,?,?,?,?,?,?的值的情况下......不能说这些值没有任何问题,但我真的认为这是停止插入的where子句。

用户ID应位于插入位置

应该是这样的

insert(user_id,...,...) values(@user_id,...,...,...)

然后您需要的用户ID(FK)将在表格中,以便稍后

select * from users join user_stuff on user_id

也许你正在尝试更新这些值,在这种情况下你应该有一个带有where子句

的更新语句

另外值得注意的是,你应该考虑规范你的DB结构,而不是player1,2,3,4,5

table: UserPlayers [ Id: UserId: Player ]

这将为您节省很多麻烦!我保证

答案 1 :(得分:0)

请尝试将准备好的声明更改为:

INSERT INTO user_players (user_id, player1, player2, player3, player4, player5, player6, player7, player8, player9, player10, player11, player12, player13) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)