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()); Link to comment https://forums.phpfreaks.com/topic/51233-solved-php-mysql-insert-where-does-not-exist/ 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 Link to comment https://forums.phpfreaks.com/topic/51233-solved-php-mysql-insert-where-does-not-exist/#findComment-252366 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 Link to comment https://forums.phpfreaks.com/topic/51233-solved-php-mysql-insert-where-does-not-exist/#findComment-252373 Share on other sites More sharing options...
Dragen Posted May 13, 2007 Share Posted May 13, 2007 glad it works! Link to comment https://forums.phpfreaks.com/topic/51233-solved-php-mysql-insert-where-does-not-exist/#findComment-252374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.