Jump to content

[SOLVED] Create else Update


daveoffy

Recommended Posts

I have it so users can update their profile. If they have already created the row in the table I want it to update, if it didn't create a row in a table yet (never updated profile) i want it to CREATE.

 

Need to add code above this and into this...

$qry = "INSERT INTO profile(member_id, music, books, activities, interests, quotes, about_me) VALUES('$member_id', '$music', '$books', '$activities', '$interests', '$quote', '$about_me');";
$result = @mysql_query($qry);
if($result) {
	header("location: profile-success.php");
	exit();
}else {
	die("Query failed");
}

 

Link to comment
https://forums.phpfreaks.com/topic/135799-solved-create-else-update/
Share on other sites

<?php
$query = "SELECT member_id FROM profile WHERE member_id='$member_id'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
if($num_results){
  $query = "UPDATE profile SET music='$music', books=$books", activities='$activities', interests='$interests', quotes='$quote', about_me='$about_me' WHERE member_id='$member_id'";
  $result = mysql($query);
  if($result){
    header('Location: profile-success.php');
    exit();
  } else {
    die('Query Failed');
  }
} else {
  $query = "INSERT INTO profile (member_id, music, books, activities, interests, quotes, about_me) VALUES('$member_id', '$music', '$books', '$activities', '$interests', '$quote', '$about_me')";
  $result = mysql_query($query);
  if($result){
    header('Location: profile-success.php');
    exit();
  } else {
    die('Query Failed');
  }
}
?>

 

see if that works for you

Warning: Wrong parameter count for mysql() on line 33

Query Failed

 

Line 33:

  $result = mysql($query);

 

Code around it:

$query = "SELECT member_id FROM profile WHERE member_id='$member_id'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
if($num_results){
  $query = "UPDATE profile SET music='$music', books='$books', activities='$activities', interests='$interests', quotes='$quote', about_me='$about_me' WHERE member_id='$member_id'";
  $result = mysql($query);
  if($result){
    header('Location: profile-success.php');
    exit();
  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.