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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.