I’m using the MySQL statement INSERT INTO to insert data into my table. I have several unique fields such as username and email. I want to insert data, but if a unique field is already taken (say somebody tries to use an already existing username) I want to perform an action.
Say redirect the page, or set a session. At the moment my code just acts like it inserts, but then obviously does nothing as the field is unique. I’ve tried the belowcode but I’m having no luck.
// Add the data to the table 'users'
mysql_query("INSERT INTO users
(username, password, email, dob, gender) VALUES('$username', '$cleansedPassword', '$email', '$dob', '$gender') ")
or header( 'Location: index.php' ) ;
header( 'Location: SignUp.php' ) ;
well I have not worked on returning error from mysql but if mysql query is not true it will return the error…. you can use that to check whether it is actually added or error returned and in case of error – you can redirect accordingly – it will take 2 more statements rather then just one line statement. but that is right way as i far as I know