Jump to content

[SOLVED] adding count effecting INSERT


cleary1981

Recommended Posts

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

$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)

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.