update query is not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sujira
Forum Newbie
Posts: 1
Joined: Tue Nov 04, 2014 6:50 am

update query is not working

Post by sujira »

The following code is not working..When the update query reached the control send to else part..I can't understand the problem..pls give the solution

Code: Select all

<?php 
require_once("functions.php");
require_once("db-const.php");
session_start();
?>
<html>
<head>
	<title>Admin Profile</title>
    <link rel="stylesheet" href="css/forget.css" />
	<link rel="stylesheet" href="font/styles1.css" />
	<script src="script.js" type="text/javascript"></script><!-- put it on user area pages -->
</head>
<body>
<div class="top">
		</div>
		<div class="header">
		<div class="menu">
    	<ul>
        	<li><a href="#">MY PROFILE</a></li>
			<li><a href="#">INTRO PAGE</a></li>
			<li><a href=\"galleryintro.php\">GALLERY PAGE</a></li>
		    <li><a href="logout.php">LOGOUT</a></li>         
        </ul>
		</div>
		</div>
        
<?php
if (logged_in() == false) {
	redirect_to("login.php");
} else {
	if (isset($_GET['id']) && $_GET['id'] != "") {
		$id = $_GET['id'];
	} else {
		$id = $_SESSION['user_id'];
	}
	
	## connect mysql server
		$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
		# check connection
		if ($mysqli->connect_errno) {
			echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
			exit();
		}
	## query database
		# fetch data from mysql database
		$sql = "SELECT * FROM users WHERE id = {$id} LIMIT 1";
	
		if ($result = $mysqli->query($sql)) {
			$user = $result->fetch_array();
			//$id=$result->fetch_array();
			
		} else {
			echo "<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>";
			exit();
		}
		
		if ($result->num_rows == 1) {
			# calculating online status
			if (time() - $user['status'] <= (30)) { // 300 seconds = 5 minutes timeout
				$status = "Online";
			} else {
				$status = "Offline";
			}
			
			# echo the user profile data
			//echo "<p>User ID: {$user['id']}</p>";
			echo "<p> Welcome {$user['username']}</p>";
			//echo "<p>Status: {$status}</p>";			
		} else { // 0 = invalid user id
			echo "<p><b>Error:</b> Invalid user ID.</p>";
		}
}

// showing the login & register or logout link
if (logged_in() == true) {
	//echo '<a href="logout.php">Log Out</a>';
	if(isset($_POST['edit']))
	{
		echo $user['username'];
		echo $user['id'];
		$username=$_POST['uname'];
		echo $username;
		
		//$update=mysql_query("update users set username='$_POST[uname]' where id='user['id']'");
		$update = 'UPDATE users SET( username)VALUES("'.$username.'")WHERE id="'.$id.'""';
		mysql_query($update) or die("Failed Updating Your Data,check SQL");
		/*if(!$update)
		{
			echo "Not Updated";
	
 		}
 		else
 		{
			echo "Updated"; 
 		}*/
	}
} else {
	echo '<a href="login.php">Login</a> | <a href="register.php">Register</a>';
}
?>

<div class="mypro">
	<form name="myprofile" method="post">
    
	<label>USER NAME</label>
	<input type="text" name="uname" value="<?php echo " {$user['username']}";?>" onFocus="this.value==this.defaultValue?this.value='':null"/>
    <a href="profile.php?id=<?php echo "{$user['id']}";?>"><input type="submit" name="edit" value="EDIT" id="btn" /></a>
    <hr size="1px" width="767" id="blc" />
	<label>PASSWORD</label>

    <a href="useredit.php?id=<?php echo "{$user['id']}";?>"><input type="button" name="change" value="CHANGE PASSWORD" id="btn1" /></a>
    <hr size="1px" width="767" id="blc" />
    
    <label>MAIL ID</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="text" name="mail" value="<?php echo " {$user['email']}";?>" onFocus="this.value==this.defaultValue?this.value='':null"/>
    <input type="submit" name="edit" value="EDIT" id="btn" />
    <hr size="1px" width="767" id="blc" />
	
    
    <label>MOBILE NO</label>
	<input type="text" name="mobile" value="<?php echo " {$user['mobile']}";?>" onFocus="this.value==this.defaultValue?this.value='':null"/>
    <input type="submit" name="edit" value="EDIT" id="btn" />
    <hr size="1px" width="767" id="blc" />
 
</form>

</div>
<?php
/*
$select=mysql_query("select * from users where id='$id'");
$fetch=mysql_fetch_array($select);
$name=$fetch['username'];
$id=$fetch['id'];
//$roll=$fetch['rollno'];
//$m1=$fetch['m1'];
//$m2=$fetch['m2'];
//$m3=$fetch['m3'];
//$total=$fetch['tot'];
//$avg=$fetch['avg'];
$mail=$fetch['email'];
$mobile=$fetch['mobile'];
echo $id;
echo $name;
?>
<?php
if(isset($_POST['edit']))
{
//$id=$_GET['id'];
$name1=$_POST['uname']; 


//$update=mysql_query("update student set name= '$name1',rollno='$roll1',m1='$m11',m2='$m22',m3='$m33',tot='$tot1',avg='$avg1' where name='$name1'");
//$update=mysql_query("update student set name= '$name1',rollno='$roll1',m1='$m11',m2='$m22',m3='$m33',tot='$tot1',avg='$avg1' where id='id'");
$update=mysql_query("update student set name='$name1',rollno='$roll1',m1='$m11',m2='$m22',m3='$m33',tot='$tot1',avg='$avg1' where id='$id'");
if(!$update)
{
	echo "Not Updated";
	
 }
 else
 {
	echo "Updated"; 
 }
 }
*/
?>
	
</body>
</html>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: update query is not working

Post by Celauran »

sujira wrote:The following code is not working
You'll need to be a lot more specific than that.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: update query is not working

Post by mikosiko »

//$update=mysql_query("update users set username='$_POST[uname]' where id='user['id']'");
$update = 'UPDATE users SET( username)VALUES("'.$username.'")WHERE id="'.$id.'""';
mysql_query($update) or die("Failed Updating Your Data,check SQL");

/*if(!$update)
{
echo "Not Updated";

}
else
{
echo "Updated";
}*/
2 observations:
- You are missing several spaces in the line where you define your UPDATE making it non-valid
- And then, you are trying to execute your query with the mysql_ API, and in all the previous code you are using the mysqli_ API ... 2 different animals that can't be mixed.
Post Reply