我的脚本正确吗?
我想要的是检查id是否存在,然后更新,如果不存在,则插入。

$sqlCheckRow = mysql_query( "SELECT * FROM wctilerack WHERE gameID = '$up_gameID' " ) or die ( mysql_error() );
$rowCounted = mysql_num_rows( $sqlCheckRow );
if ( $rowCounted == '0' ) {
    // INSERT wctilerack
    $sqlTileINSERT = mysql_query( "INSERT INTO wctilerack VALUE('', '$up_gameID', '$up_email_player1', '$playerRack' ) ") or die ( mysql_error() );
} elseif ( $rowCounted == '1' ) {
    // INSERT wctilerack
    $sqlTileINSERT = mysql_query( "UPDATE wctilerack SET tiles = '$playerRack' WHERE gamedID = '$up_gameID'  ") or die ( mysql_error() );
}


我测试了它的工作原理,只是想确认过程是否正确。

谢谢

最佳答案

你也可以这样

if (empty($rowCounted))
{
    $sqlTileINSERT = mysql_query( "INSERT INTO wctilerack VALUE('', '$up_gameID', '$up_email_player1', '$playerRack' ) ") or die ( mysql_error() );
}
else
{
    $sqlTileINSERT = mysql_query( "UPDATE wctilerack SET tiles = '$playerRack' WHERE gamedID = '$up_gameID'  ") or die ( mysql_error() );
}

08-26 20:08