bicky Posted July 14, 2010 Share Posted July 14, 2010 Hello All Hoping for some help this time I have created a statement that works fine now, taking the value from a form and inserting into a MS SQL table. I now want to expand the code, to only insert when the value doesnt already exist... My Current Code is as follows: $tsql = "INSERT INTO email_supp (email,date,source) VALUES (?,?,?)"; $today = date("d/m/y"); $source= 'Email Form'; $email = array($_POST['emailopt'], $today,$source); /* Prepare and execute the query. */ $stmt = sqlsrv_query( $conn, $tsql, $email); if( $stmt ) { echo "Row successfully inserted.\n"; } else { echo "Row insertion failed.\n"; die( print_r( sqlsrv_errors(), true)); } /* Free statement and connection resources. */ /*sqlsrv_free_stmt( $stmt);*/ sqlsrv_close( $conn); ?> My main problem here is I dont really know how the value is taken from the form, I know its within this part (array($_POST['emailopt']) but If i wanted to just specify the value to a variable my PHP seems to then fall over. ANY help would be appreciated.. Thanks in advance Bicky Link to comment https://forums.phpfreaks.com/topic/207719-inserting-a-value-into-sql-where-the-value-doesnt-already-exist/ Share on other sites More sharing options...
AMcHarg Posted July 16, 2010 Share Posted July 16, 2010 Why not just add a constraint to the particular field on the database that you want to be unique? This way your code will not actually be able add any duplicate entries, even if it tries to. Link to comment https://forums.phpfreaks.com/topic/207719-inserting-a-value-into-sql-where-the-value-doesnt-already-exist/#findComment-1086800 Share on other sites More sharing options...
Pikachu2000 Posted July 16, 2010 Share Posted July 16, 2010 You'll ned to clarify the question. Are you wanting to prevent duplicate entries, or what? Be as specific as possible as to what you need the script to do. Link to comment https://forums.phpfreaks.com/topic/207719-inserting-a-value-into-sql-where-the-value-doesnt-already-exist/#findComment-1086837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.