Jump to content

Question About Mysql Update Query


eldan88

Recommended Posts

Hey,

 

I have wrote a simple mysql update statment below. However when I enter a duplicate username and password, there obviously isn't any mysql_affected_rows. So how do create another condition that checks to see if there has been a duplicate?

 

Thanks

 

if(empty($errors))
{
$id= $_POST['id']
$username= $_POST['username'];
$password = $_POST['password'];

$query = "UPDATE tablename SET
username = '{$username}',
password = '{$password}'
WHERE id = '{$id}'";


$result_set = mysql_query($query,$connection);
if (mysql_affected_rows() == 1 ) {
// mysql_affected_rows is succesful
}// end of if condition for the mysql_affected rows
else {
echo "Username and Password Creation Failed" . "<br />";
echo mysql_error();
  } // End of else condition if mysql_affected_rows did not succeed
 } // End of if(empty($errors)) {

Link to comment
https://forums.phpfreaks.com/topic/271966-question-about-mysql-update-query/
Share on other sites

$query = "UPDATE tablename t1 SET
t1.username = '{$username}',
t1.password = '{$password}'
WHERE t1.id = '{$id}' AND !EXISTS(
SELECT *
FROM tablename t2
WHERE t2.account = '{$account}' AND t2.id != '{$id}'
)"

 

This checks if there does not exist another row with that username that you want to set. But it does exclude the row you want to update, because that may be the same username.

  On 12/13/2012 at 4:51 PM, Langstra said:

$query = "UPDATE tablename t1 SET
t1.username = '{$username}',
t1.password = '{$password}'
WHERE t1.id = '{$id}' AND !EXISTS(
SELECT *
FROM tablename t2
WHERE t2.account = '{$account}' AND t2.id != '{$id}'
)"

 

This checks if there does not exist another row with that username that you want to set. But it does exclude the row you want to update, because that may be the same username.

 

Oh gotcha. That makes a whole lot of sense. Thank you!

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.