Dusaro Posted December 4, 2011 Share Posted December 4, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252477-selectif-existsupdateinsert-problem/ Share on other sites More sharing options...
kicken Posted December 5, 2011 Share Posted December 5, 2011 $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. Quote Link to comment https://forums.phpfreaks.com/topic/252477-selectif-existsupdateinsert-problem/#findComment-1294471 Share on other sites More sharing options...
Dusaro Posted December 5, 2011 Author Share Posted December 5, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/252477-selectif-existsupdateinsert-problem/#findComment-1294486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.