Jump to content

[SOLVED] PHP MySQL Insert Where does not exist


TOMMURRAY

Recommended Posts

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());

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.