Note to the mod: I have posted this in both the PHP and MySQL area because the post depends on both technologies.
I have a table called newsletters that looks like this:
I add to the table using this:
Lets says a user has already registered for the mens newsletter, and they now want to register for the womens, I guess I would use this command:
When I do, I get the following error:
I am using PHP to insert this information (from form data), so to work around this, I am going to use an "if" argument to check if the e-mail address already exists in the table, and if it does, then to use an "update/alter" command to update the existing entry.
Does anyone know a command for PHP to check if something exists in a data base and then update it accordingly?
Something like:
I know that command is deeply flawed, I am only a few weeks old in the MySQL world.
I have a table called newsletters that looks like this:
+----------------+------+--------+
| email | mens | womens |
+----------------+------+--------+
| [email protected] | 1 | 0 |
| [email protected] | 0 | 1 |
+----------------+------+--------+
| email | mens | womens |
+----------------+------+--------+
| [email protected] | 1 | 0 |
| [email protected] | 0 | 1 |
+----------------+------+--------+
mysql> INSERT INTO newsletters (email, mens) VALUES ('[email protected]', 1);
mysql> INSERT INTO newsletters (email, womens) VALUES ('[email protected]', 1);
ERROR 1062 (23000): Duplicate entry '[email protected]' for key 'PRIMARY'
Does anyone know a command for PHP to check if something exists in a data base and then update it accordingly?
Something like:
if email $email exists, then {alter table newsletters change column womens VALUES (1);}
elseif email $email null {INSERT INTO newsletters (email, womens) VALUES ('$email', 1);}
elseif email $email null {INSERT INTO newsletters (email, womens) VALUES ('$email', 1);}

Comment