TOMMURRAY Posted May 13, 2007 Share Posted May 13, 2007 Hi all i'm not sure if I should be positing in this forum, if I am posting in the wrong forum i'm sorry. I have searched and searched for information on how to make a sql insert where the row does not exsist. I have two insert statements and I wish that the second statement whould only be inserted if the row does not exsist. Has anyone come across a solution to this problem or can point me in the right direction? Thank You For Your help. Regards Tom $queryInsert = "INSERT INTO `golfcourses` (CourseName,hole1,hole2,hole3,hole4,hole5,hole6,hole7,hole8,hole9,hole10,hole11,hole12,hole13,hole14,hole15,hole16,hole17,hole18,totalpar) VALUES ('$course','$hole_1_par','$hole_2_par','$hole_3_par','$hole_4_par','$hole_5_par','$hole_6_par','$hole_7_par','$hole_8_par','$hole_9_par','$hole_10_par','$hole_11_par','$hole_12_par','$hole_13_par','$hole_14_par','$hole_15_par','$hole_16_par','$hole_17_par','$hole_18_par','$partotal')"; $resultGetPages = mysql_db_query($this->dbase,$queryInsert) or die ("Query failed: error was ".mysql_error()); Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 13, 2007 Share Posted May 13, 2007 you could use a simple if statement.. something like <?php $sql = "SELECT * FROM golfcourses WHERE CourseName = '$course'"; if($result = mysql_query($sql)){ if(mysql_num_rows($result)){ echo "row already exists"; }else{ $queryInsert = "INSERT INTO `golfcourses` (CourseName,hole1,hole2,hole3,hole4,hole5,hole6,hole7,hole8,hole9,hole10,hole11,hole12,hole13,hole14,hole15,hole16,hole17,hole18,totalpar) VALUES ('$course','$hole_1_par','$hole_2_par','$hole_3_par','$hole_4_par','$hole_5_par','$hole_6_par','$hole_7_par','$hole_8_par','$hole_9_par','$hole_10_par','$hole_11_par','$hole_12_par','$hole_13_par','$hole_14_par','$hole_15_par','$hole_16_par','$hole_17_par','$hole_18_par','$partotal')"; } }else{ echo mysql_error(); } ?> I think that should work, although you'd need to add more checks to the $sql ="" statement if you have more than one entry for each course Quote Link to comment Share on other sites More sharing options...
TOMMURRAY Posted May 13, 2007 Author Share Posted May 13, 2007 Thank You for your quick response that was exactly what I was looking for, I have been working on this project now for about 10 hours today and my brains well frazzled. Thank You so much. Regards Tom Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 13, 2007 Share Posted May 13, 2007 glad it works! 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.