Is my script correct? What i want is that check if the id is exist then UPDATE if not exist then INSERT.
$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() );
}
I tested it its working i just like to confirm if the process is correct.
thank you
You can also do like this
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() );
}
You can write it like this
INSERT INTO wctilerack VALUES ('', '$up_gameID', '$up_email_player1', '$playerRack')
ON DUPLICATE KEY UPDATE tiles='$playerRack';
Try this::
INSERT into table values (vcolumn1, vcolumn2) on duplicate key update set column1=vcolumn1, column2=volumn2