Mutley Posted January 11, 2007 Share Posted January 11, 2007 I'm trying to do an IF statement to check if an entry already exists, if it doesn't continue with submitting data, if it does, come up with an error message, how would I do this?I have my INSERT INTO sql statement and the $_POST variables listed. Do I put the IF Statement before the INSERT INTO? How do I check the database though? Quote Link to comment Share on other sites More sharing options...
Mutley Posted January 11, 2007 Author Share Posted January 11, 2007 Here is the code:[code] $this = $_POST['this']; $that = $_POST['that']; $sql = "INSERT INTO `data` "; $sql .= "(this, that) "; $sql .= "VALUES "; $sql .= "('".$this."', '".$that."')"; mysql_query($sql); $profile_id = mysql_insert_id(); echo "<b>Data inserted! </b><br /><br />"; echo "<a href=index.php>Click here to continue</a>"; exit();}else { // Form below[/code]So what I want to do is something likeif($this ...already exists) {echo "Sorry that already exists."} else {......Insert date Quote Link to comment Share on other sites More sharing options...
trq Posted January 11, 2007 Share Posted January 11, 2007 [quote]Do I put the IF Statement before the INSERT INTO[/quote]Yes.An example.[code]<?php if (isset($_POST['data'])) { // connect to db. $sql = "SELECT data FROM tbl WHERE data = '{$_POST['data']}';" if ($result = mysql_query($sql)) { if (!mysql_num_rows($result)) { $sql = "INSERT INTO tbl (data) VALUES ('{$_POST['data']}');" if (mysql_query($sql)) { echo "insert complete"; } } else { echo "data already exists"; } } }?>[/code] Quote Link to comment Share on other sites More sharing options...
chronister Posted January 11, 2007 Share Posted January 11, 2007 Connect to the db and run a query to check the information you want.[code]<?php$query="SELECT * FROM table WHERE field='$comparision_variable";$result=mysql_query($query);if(mysql_num_rows($result > 0)){// condition is true and this already exists, skip it}else{// mysql_num_rows($result) is not greater, than 0 so it does not exist... insert data now$query="INSERT INTO table blah blah blah";$result=mysql_query($query);}?>[/code]AHHH Thorpe, you are faster (and better)than me.. I was typing when you posted yours.. curse you ;) Quote Link to comment Share on other sites More sharing options...
marcus Posted January 11, 2007 Share Posted January 11, 2007 [quote author=chronister link=topic=121951.msg502296#msg502296 date=1168528712]if(mysql_num_rows($result > 0)){[/quote]That wouldn't work. mysql_num_rows($result) > 0 Quote Link to comment Share on other sites More sharing options...
Mutley Posted January 11, 2007 Author Share Posted January 11, 2007 What if I want to see if a field is the same in rows where another field is?For example, where "$that" already exists in the row where field "$this" is. Quote Link to comment Share on other sites More sharing options...
chronister Posted January 12, 2007 Share Posted January 12, 2007 [quote author=mgallforever link=topic=121951.msg502305#msg502305 date=1168529434][quote author=chronister link=topic=121951.msg502296#msg502296 date=1168528712]if(mysql_num_rows($result > 0)){[/quote]That wouldn't work. mysql_num_rows($result) > 0[/quote]dumb fat fingers..... ;Dfingers were faster than my brain.Nate Quote Link to comment 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.