cleary1981 Posted August 22, 2008 Share Posted August 22, 2008 Hi, I have some php code which works fine until I try adding a bit of code that is supposed to check that if a record already exists it will not carryout the INSERT. When I add the commented out code no records are updated even those that do not exist <?php require "config.php"; foreach($_POST['result'] as $n=>$result){ $model = $_POST['model'][$n]; $xReturnValue = $_POST['xReturnValue'][$n]; $yReturnValue = $_POST['yReturnValue'][$n]; $proj = $_POST['proj'][$n]; // $query = ("SELECT COUNT(*) FROM object WHERE object_name = $result" AND proj_id = $proj); // $result = mysql_query($query) or die(mysql_error()); // if ($result =0) { $results = mysql_query("INSERT INTO object (module_name, object_name, xpos, ypos, proj_id) VALUES ('".$model."', '".$result."', '".$xReturnValue."', '".$yReturnValue."', '".$proj."')"); // } } ?> Link to comment https://forums.phpfreaks.com/topic/120844-solved-adding-count-effecting-insert/ Share on other sites More sharing options...
ratcateme Posted August 22, 2008 Share Posted August 22, 2008 i don't know but try instead of COUNT(*) just select one field Scott. Link to comment https://forums.phpfreaks.com/topic/120844-solved-adding-count-effecting-insert/#findComment-622907 Share on other sites More sharing options...
paul2463 Posted August 22, 2008 Share Posted August 22, 2008 $query = "SELECT * FROM object WHERE object_name = '$result' AND proj_id = '$proj'"; //get all rows with this information $result = mysql_query($query) or die(mysql_error()); $count = mysql_num_rows($result); //count the rows returned, if it doesnt exist in the DB then it will be 0 if ($count == 0) { //for conditional checking it should be == (= assigns a value to a variable) Link to comment https://forums.phpfreaks.com/topic/120844-solved-adding-count-effecting-insert/#findComment-622908 Share on other sites More sharing options...
cleary1981 Posted August 22, 2008 Author Share Posted August 22, 2008 That didn't work either :'( Link to comment https://forums.phpfreaks.com/topic/120844-solved-adding-count-effecting-insert/#findComment-622912 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.