Jump to content

Problem with storing info & associated ID


blepblep

Recommended Posts

  • Replies 56
  • Created
  • Last Reply

I think it would be best (and quickest) if you posted everything from where your UPDATE query is being formed through to the end of the else {echo "nothing affected";} logic.

 

I suspect you have more than one database connection and you are executing a query using one connection and testing the mysql_affected_rows() on a different connection.

 

And why not simply refer to the query as $query? There's no need to specifically use variable names like $insert_query, especially if you are not going to rename them to what the query actually is to avoid confusion when you don't post all the relevant code at once.

 

 

Link to comment
Share on other sites

Done Muddy, this is returned:

 

bool(false) nothing affected

 

@PFMaBiSmAd -

 

<?php
///------------------------
///	Connecting to database
///------------------------

include 'connect_db.php';

///-----------------------------
///	Inserting data into database
///-----------------------------

function sanitize_data($data) // create the function sanitize_data
    {
        $data = mysql_real_escape_string(trim($data));
        return $data;
    }

$post = array(); 
foreach($_POST as $key =>$value){
$post[$key] = sanitize_data($value);   // call sanitize_data using each value from the $post array
}

$id = $post['review_id'];
//var_dump($id);

if(isset($post['save']))
{

$insert_query = "UPDATE tc_tool.forms

			SET quality_ranking_review = 		'{$post['quality_ranking_review']}',
			input_doc_rank = 					'{$post['input_doc_rank']}',
			correct_doc = 						'{$post['correct_doc']}',
			internally_reviewed = 				'{$post['internally_reviewed']}', 
			reqs_covered = 						'{$post['reqs_covered']}',
			correct_storage_library = 			'{$post['correct_storage_library']}',
			reports_described = 				'{$post['reports_described']}',
			approved_change_requests_included = '{$post['approved_change_requests_included']}',
			issues_addressed = 					'{$post['issues_addressed']}',
			doc_available = 					'{$post['doc_available']}',
			statement_problem_chapter = 		'{$post['statement_problem_chapter']}',
			major_comments = 					'{$post['major_comments']}',
			result = 							'{$post['result']}',
			num_of_major_comments =  			'{$post['num_of_major_comments']}',
			minor_comments = 					'{$post['minor_comments']}',
			next_review_forum = 				'{$post['minor_comments']}',
			date = 								'{$post['date']}',
			time =								'{$post['time']}',
			venue =								'{$post['venue']}',
			time2 =								'{$post['time2']}',
			location =							'{$post['location']}',
			severity =							'{$post['severity']}',
			resp =								'{$post['resp']}',
			status =							'{$post['status']}',
			comment =							'{$post['comment']}',
			remark =							'{$post['remark']}',
			miscellaneous =						'{$post['miscellaneous']}'

			 WHERE

			 (review_id = '{$post['review_id']}')";
			 //var_dump($post['review_id']);

	var_dump(mysql_info());		 
	if (mysql_affected_rows() === 1) 
	{
			echo 'Row was updated.';
	} 
	else 
	{
	echo "nothing affected";
	}

	//-----------------------------
	// Prints error if there is one
	//-----------------------------

	if (!mysql_query($insert_query, $connection))
	{
		echo "Query failed: $query<br />" . mysql_error();
	}

	function died($error) 
	{
		// Error code here
		echo "Error, not saving successfully. ";
		echo "The errors are below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please retry.<br /><br />";
		var_dump($_POST);
		die();
	}

	mysql_close($connection);
}
?>

Link to comment
Share on other sites

a sample from the code you posted :

			 WHERE

			 (review_id = '{$post['review_id']}')";
			 //var_dump($post['review_id']);

	var_dump(mysql_info());		 
	if (mysql_affected_rows() === 1) 
	{
			echo 'Row was updated.';
	} 
	else 
	{
	echo "nothing affected";
	}

	//-----------------------------
	// Prints error if there is one
	//-----------------------------

	if (!mysql_query($insert_query, $connection))
	{
		echo "Query failed: $query<br />" . mysql_error();
	}

As, Adam said : whats happening is that you are checking the var_dump and the mysql_affected_rows BEFORE your actualy running the query with if(!mysql_query($insert_query, $connection)).  So at the point where the check is performaed it's telling the truth, as there was no query performed, it's then going on and updating the database as it should.  You need to swap the possition of some things about.

Link to comment
Share on other sites

@Adam, how does this look now?

 

........
WHERE

			 (review_id = '{$post['review_id']}')";
			 //var_dump($post['review_id']);


	//-----------------------------
	// Prints error if there is one
	//-----------------------------

	if (!mysql_query($insert_query, $connection))
	{
		echo "Query failed: $query<br />" . mysql_error();
	}

	if (mysql_affected_rows() === 1) 
	{
			echo "Row was updated." . "<br />";
	}

	else 
	{
		echo "nothing affected" . "<br />";
	}

	var_dump(mysql_info());	

	function died($error) 
	{
		// Error code here
		echo "Error, not saving successfully. ";
		echo "The errors are below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please retry.<br /><br />";
		//var_dump($_POST);
		die();
	}

 

When I run it how it is above this is my output:

 

nothing affected

string(40) "Rows matched: 1 Changed: 0 Warnings: 6"

 

The 'Changed' in the above changes if I enter new data, working so I presume?

Link to comment
Share on other sites

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.