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
https://forums.phpfreaks.com/topic/252477-selectif-existsupdateinsert-problem/
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.

 

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

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.