gladiator83x Posted June 22, 2006 Share Posted June 22, 2006 [!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]Hey All, I have a Mysql table (called End_Review) and it has 3 columns: an incrementing number, topic_title, and date. On another page I have have a listbox with many topic names. When a topic is chosen, I go to another page where this topic name is passed as $title. Each time I choose a new topic name, I want it to be stored in a database. If it is already in the database, I just want an output message that says that it has already been stored. [!--colorc--][/span][!--/colorc--]$res=mysql_query("select * from End_Review");if(mysql_num_rows($res)==0) echo "there is no data in table..";else {if ($title !=$row[topic_title]){ mysql_query("INSERT INTO End_Review (id,topic_title, date) VALUES('','$title','$test_date' ) ") or die(mysql_error());} else{$end_query= "SELECT End_Review.date FROM End_Review WHERE End_Review.topic_title='$title'";$end_result = mysql_query($end_query) or die('Query failed: ' . mysql_error());$line = mysql_fetch_array($end_result, MYSQL_ASSOC); foreach ($line as $col_value) {echo "\t\t<td>$col_value3</td>\n"; }exit("This topic has already been reviewed. Please observe the timestamp");}}}[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]All this code pretty much does is compare the specified title with the titles from the table and if it finds a match the associated data will be retrieved. However, my problem is whenever the specified data is not match, it keeps on populating my table with the same data. I know that it should be in the for loop, but I am confused as to where it could go? Any suggestions?[!--colorc--][/span][!--/colorc--] Quote Link to comment https://forums.phpfreaks.com/topic/12663-problems-with-storing-data/ Share on other sites More sharing options...
phpstuck Posted June 23, 2006 Share Posted June 23, 2006 If I am understanding right? Just use a MySQL statement to query the database to see if it exists before adding it. Something like the following example:[code]$sql_word_check = mysql_query("SELECT word FROM define WHERE word='$word'"); $word_check = mysql_num_rows($sql_word_check);if(($word_check > 0)){ echo "<center><b><font face='tahoma' color='red'>Please fix the following errors: </b><br />"; if($word_check > 0){ echo "The word you have submitted has already been defined in our database. Please submit a different word or phrase!<br />"; unset($word); }[/code]Of course use your own details I copies that from a dictionary I built a while backHope that helps Quote Link to comment https://forums.phpfreaks.com/topic/12663-problems-with-storing-data/#findComment-48673 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.