I am trying to replace mysql fields with this code... But unfortunatly it is not working... I do not know why... Here is the code please help me. Thank you.

<?php
session_start();
	if ($_POST['txtName'] != ''){
		$host="****"; 
		$username="****"; 
		$password="****"; 
		$db_name="****"; 
		$tbl_name="****";
		
		mysql_connect("$host", "$username", "$password")or die("cannot connect");
		mysql_select_db("$db_name")or die("cannot select DB");
	
		$userId = $_COOKIE['UserId'];
		$userName = $_POST['txtName']; 
		
		$sql="UPDATE $tbl_name SET user_name='$userName' WHERE user_id='$userId'";
		$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
		if ($result==1){
			header('Location: profile.php');
		}
	}
	mysql_close();
?>
<html>
<head>
<title>Change Name</title>
</head>
<body>
	<form method="post" name="frmChangeName" id="frmChangeName">
        <input type="text" name="txtName" id="txtName">
        <input type="submit" name="cmdSubmit" value="Change" id="cmdSubmit">
    </form>
</body>
</html>

Recommended Answers

All 11 Replies

Member Avatar for amigura
<?php
session_start();
	if ($_POST['txtName'] != ''){
		$host="localhost"; 
		$username="root"; 
		$password=""; 
		$db_name="testing"; 
		$tbl_name="host";
		
				
		[B]// change[/B]
	$con=mysql_connect($host, $username, $password) or die("cannot connect");
	mysql_select_db($db_name, $con) or die("cannot select DB");
		[B]// change[/B]

		
		print("Host Info: ".mysql_get_host_info()."\n");
  print("Server Info: ".mysql_get_server_info()."\n");
  print("Client Info: ".mysql_get_client_info()."\n");
  
		$userId = $_COOKIE['UserId'];
		$userName = $_POST['txtName']; 
		
		$sql="UPDATE $tbl_name SET user_name='".mysql_real_escape_string($userName)."' WHERE user_id='".mysql_real_escape_string($userId)."'";
		$result = mysql_query($sql) or die('Query failed. ' . mysql_error());
		
		[B]// change[/B]
		if (mysql_affected_rows()==1){
			header('Location: profile.php');
		}
		[B]// change[/B]
		
	[B]// change[/B]
	mysql_close($con);
	[B]// change[/B]
	}
?>

<html>
<head>
<title>Change Name</title>
</head>
<body>
	<form method="post" name="frmChangeName" id="frmChangeName">
        <input type="text" name="txtName" id="txtName">
        <input type="submit" name="cmdSubmit" value="Change" id="cmdSubmit">
    </form>
</body>
</html>

It didnt work... But thanks for the help.

Member Avatar for amigura

wat was the error?

Host Info: Localhost via UNIX socket Server Info: 5.0.27-Debian_0.dotdeb.1 Client Info: 5.0.27
Thats the error message

Member Avatar for amigura

Host Info: Localhost via UNIX socket Server Info: 5.0.27-Debian_0.dotdeb.1 Client Info: 5.0.27
Thats the error message

it wasn't a error, but delete this bit. i was just using it for testing.

print("Host Info: ".mysql_get_host_info()."\n");
  print("Server Info: ".mysql_get_server_info()."\n");
  print("Client Info: ".mysql_get_client_info()."\n");

Ok now the page isn't redirecting, and the field isn't updating still. I'm confused as to why its not working. Thanks for the help, and sorry for the trouble.

Member Avatar for amigura

are u using cookies or session?

this is probably why it is not updating

$userId = $_COOKIE['UserId'];
 to 
// SESSION
$userId = $_SESSION['UserId'];

also if you have duplicate $userId which u shouldn't this bit will not work. if updated row is just one then redirect, if less than 1 or more don't

if (mysql_affected_rows()==1){
header('Location: profile.php'); 	}

Just out of curiousity why wont it work if im using a cookie to store the information?
also I changed it to a session... And it still wont work... I have updating for the forum posting... And that bit works. Its this single portion that wont work.

Thanks for the links, I am using a session for something else, but the cookie was to store the users name

Member Avatar for amigura

clear your cache first.

// check to see if cookie is set
echo $_COOKIE['UserId'];

// check to see if any row have been updated. should be more than 0 if update
echo mysql_affected_rows();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.