Inserting/updating data into MySql database using php -


i trying insert/update mysql database depending on whether post exists on database (i checking unique user_id). following works:

$select_query = "select * "; $select_query .= "from test "; $select_query .= "where user_id = '$user_id'";  $check_user_id = mysqli_query($connection, $select_query);  $query  = "insert test ("; $query .= "  user_id, name, message"; $query .= ") values ("; $query .= "  '{$user_id}', '{$name}', '{$message}'"; $query .= ")";  $result = mysqli_query($connection, $query);  if ($result) {     echo "success!"; } else {     die("database query failed. " . mysqli_error($connection)); } 

however, when use following code if/else statement, not work anymore, although console reports "success!" (meaning $result has value). appreciated. thanks.

$select_query = "select * "; $select_query .= "from test "; $select_query .= "where user_id = '$user_id'";  $check_user_id = mysqli_query($connection, $select_query);  if (!$check_user_id) {     $query  = "insert test (";     $query .= "  user_id, name, message";     $query .= ") values (";     $query .= "  '{$user_id}', '{$name}', '{$message}'";     $query .= ")"; } else {     $query  = "update test set ";     $query .= "name = '{$name}', ";     $query .= "message = '{$message}' ";     $query .= "where user_id = '{$user_id}'"; }  $result = mysqli_query($connection, $query);  if ($result) {     echo "success!"; } else {     die("database query failed. " . mysqli_error($connection)); } 

as understand code. trying check if user_id existing in database.. made simple code , think works me..

    $select_query = mysql_query("select * test user_id = '$user_id'") or die (mysql_error()); $result = mysql_num_rows($select_query);  if(!$result){     $query = mysql_query("insert test (user_id, name, message) values ('$user_id', '$name', '$message')");         if($query){             echo "success!";         }         else         {             die (mysql_error());         } } else{     $query2 = mysql_query("update test set name='$name', message='$message' user_id = '$user_id'") } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -