Jump to content

SELECT,IF EXISTS,UPDATE,INSERT problem


Dusaro

Recommended Posts

Well, I made this yesterday then realised, I need to check is it exists...

I got this but when I go to accept the application and the member exists in the table it enters it anyway...

 

<?php
$member=$_POST['memberid'];
$status=$_POST['Status'];

$con = mysql_connect("host","user","pass");
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}
mysql_select_db("a2186214_hbclan",$con);
$sql="UPDATE application SET Status = '$status' WHERE ID = '$member'";
$sql1="INSERT INTO table_members(name) SELECT application.Name FROM application WHERE application.ID = '$member'";
if ($status == 'ACCEPTED')
{
	if(mysql_num_rows(mysql_query("SELECT name FROM table_members WHERE name = '$member'")))
	{
		if(mysql_query($sql, $con) or die(mysql_error()))
		{
		echo 'Status Changed.<br /><a href="../applications.php">Return To Members List</a>';
		}
		else
		{
		die('Could not submit: ' . mysql_error());
		}	
	}
	else
	{
		if(mysql_query($sql, $con) or die(mysql_error()))
		{	
			if(mysql_query($sql1, $con) or die(mysql_error()))
			{	
				echo 'Status Changed.<br /><a href="../applications.php">Return To Members List</a>';
			}
		}
		else
		{
			die('Could not submit: ' . mysql_error());
		}
	}
}
else
{
	if(mysql_query($sql, $con) or die(mysql_error()))
	{
		echo 'Status Changed.<br /><a href="../applications.php">Return To Members List</a>';
	}
	else
	{
		die('Could not submit: ' . mysql_error());
	}
}
mysql_close($con);

?>

Link to comment
Share on other sites

$sql="UPDATE application SET Status = '$status' WHERE ID = '$member'";
$sql1="INSERT INTO table_members(name) SELECT application.Name FROM application WHERE application.ID = '$member'";

 

Those two queries imply that $member is an id, while

if(mysql_num_rows(mysql_query("SELECT name FROM table_members WHERE name = '$member'")))

 

implies it is a name.  It can't be both.  Either your using the wrong variable or the wrong column in your query.

 

As an alternative, you could use a unique key and then INSERT INTO ... ON DUPLICATE KEY UPDATE ... to handle it rather than separate queries.

 

Link to comment
Share on other sites

Like this?

<?php
$member=$_POST['memberid'];
$status=$_POST['Status'];

$con = mysql_connect("host","user","pass");
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}
mysql_select_db("a2186214_hbclan",$con);
$sql="UPDATE application SET Status = '$status' WHERE ID = '$member'";
$sql1="INSERT INTO table_members(name) SELECT application.Name FROM application WHERE application.ID = '$member' ON DUPLICATE KEY UPDATE table_members.name = application.Name";
if ($status == 'ACCEPTED')
{
	if(mysql_query($sql, $con) or die(mysql_error()))
	{	
		if(mysql_query($sql1, $con) or die(mysql_error()))
		{	
			echo 'Status Changed.<br /><a href="../applications.php">Return To Members List</a>';
		}
	}
	else
	{
		die('Could not submit: ' . mysql_error());
		}
}
else
{
	if(mysql_query($sql, $con) or die(mysql_error()))
	{
	echo 'Status Changed.<br /><a href="../applications.php">Return To Members List</a>';
	}
	else
	{
		die('Could not submit: ' . mysql_error());
	}
}
mysql_close($con);

?>

 

Because that is still inserting not updating...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.