Jump to content

Help With MYSQL UPDATE Query...Cant see what the heck is going wrong.


mongoose00318

Recommended Posts

Ok, I'm going start off simple. If I have to provide more code I will.

 

I am doing an update on a table called countries. Yet my query just will not update the db. Is there anything wrong with this query?

 

mysql_query("UPDATE countries SET country_id = '{$_POST['update_value']}' WHERE country_id = '{$_POST['original_html']}'") or die(mysql_error());

Link to comment
Share on other sites

I also want to check if the query is being executed.  That's the first step to debugging why it's not updating.  If it's being executed and it's still not updating, then it's time to look at the query itself.

 

Yes the whole script will help :)  And also a brief description of the circumstances in which the update should be triggered.

Link to comment
Share on other sites

Ok I know what you mean now. I added a echo "hello"; under the query and that showed up fine. Here is the code.

 

<?php
$value = $_POST['update_value'];

include('dbconfig.php');
//edit department
if($_GET['mode'] != "delete") {
//get requested departments information from database
$exist_query = mysql_query("SELECT * FROM countries WHERE {$_GET['mode']}='{$_POST['update_value']}'") or die(mysql_error());

//edit department name
//if $exist_query = 0 or $exist_query = 1 and $_GET['mode'] = name
if(mysql_num_rows($exist_query) == 0) {
	if($_GET['mode'] == "country_name") { 
		mysql_query("UPDATE countries SET country_name = '{$_POST['update_value']}' WHERE country_name = '{$_POST['element_id']}'") or die(mysql_error());
		echo $_POST['update_value'];
		//echo $_POST['element_id'];
	}
	//edit department description
	//if $exist_query = 0 or $exist_query = 1 and $_GET['mode'] = description
	elseif($_GET['mode'] == "country_id") { 
		mysql_query("UPDATE countries SET country_id = '{$_POST['update_value']}' WHERE country_id = '{$_POST['original_html']}'") or die(mysql_error());

		echo "hello";
		die();
		/*
		//search for users that have state that was being edited and change it to the new value
		$get_users = mysql_query("SELECT * FROM users WHERE country = '{$_POST['original_html']}'") or die(mysql_query());

		//check to see that the users state is set to the old state value, if it is replace it with the new state value
		while($row = mysql_fetch_array($get_users)) {
			if($row['country'] ==	$_POST['original_html']) {
				mysql_query("UPDATE users SET country = '{$_POST['update_value']}' WHERE user_id = '{$row['user_id']}'") or die(mysql_error());
			}
		}

		//update the states country id field
		mysql_query("UPDATE states SET country_id = '{$_POST['update_value']}' WHERE country_id = '{$_POST['original_html']}'") or die(mysql_query());
		*/
		echo $_POST['update_value'];
	}
}
//the update value the user tried to enter already exists in the database
else {
	//alert the user of the problem
	echo "The entry already exists";
}
}
//delete department
elseif($_GET['mode'] == "delete") {
//delete the department
mysql_query("DELETE FROM countries WHERE country_id = '".$_GET['country_id']."'");
//redirect user
header("Location: ../admin_manage_countries.php");
}
?>

 

Look at this part.

elseif($_GET['mode'] == "country_id") { 
		mysql_query("UPDATE countries SET country_id = '{$_POST['update_value']}' WHERE country_id = '{$_POST['original_html']}'") or die(mysql_error());

		echo "hello";
		die();

 

The "hello" is showing up, and the $_POST vars are showing up. What else can I provide to help you?

Link to comment
Share on other sites

I even changed the POST vars to just static information and the query didnt do anything. But if I removed the single quotes from around the POST vars I get a mysql error.

 

It just doesn't make any sense because I have another page that is really similar that is working fine.

 

EDIT**

 

Your print statement worked also. :( Heres the result.

 

About to set country_id to USA2 for country_id hello

Link to comment
Share on other sites

OK, the next step I would do is to change this

 

mysql_query("UPDATE countries SET country_id = '{$_POST['update_value']}' WHERE country_id = '{$_POST['original_html']}'") or die(mysql_error());

 

to this

 

$sql = "UPDATE countries SET country_id = '{$_POST['update_value']}' WHERE country_id = '{$_POST['original_html']}'";
print "About to execute '$sql'<br>";
mysql_query($sql) or die(mysql_error());

 

So we can see exactly what query is getting executed.

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.