Jump to content

hello friends please help me in this too


raghu_tak

Recommended Posts

friends,this is a code of head first php book
it is used to edit a profile of user who has logged in
but when i edit a profile the page displayed again with no feedback
please help me friends!!
 
 
<html>
<head><title>
Edit Profile
</title></head>
<body>
<h1 style="color:brown;"><center>Please Edit your profile to find new mismatch.</center></h1>
<form enctype="multipart/form-data" method="post" action="<?php echo$_SERVER['PHP_SELF']; ?>">
<p><i><center>This information would be seen by others and easily get connected to you.</center></i></p>
<hr/>
<img src="images1\mismatch.jpg" width="400" height="400" alt="mismatch image." align="right">
<fieldset style="width:250">
<legend>Edit your profile</legend>
<table><tr>
<td><label for="fname">Firstname:</label></td>
<td><input type="text" id="fname" name="fname"><br/></td>
</tr><tr>
<td><label for="lname">Lastname:</label></td>
<td><input type="text" id="lname" name="lname"><br/></td>
</tr><tr>
<td><label for="bday">Birthday:</label></td>
<td><input type="text" id="bday" name="bday"><br/></td>
</tr><tr>
<td><label for="sex">Gender:</label></td>
<td><input type="text" id="sex" name="sex"><br/></td>
</tr><tr>
<td><label for="nation">Country:</label></td>
<td><input type="text" id="nation" name="nation"><br/></td>
</tr><tr>
<td><label for="screenshot">Picture:</label></td>
<td><input type="file" name="screenshot" value="Choose image"><br/></td>
</tr></table>
</fieldset>
 
<input type="submit" name="submit" value="Save">
 
<?php
define('upath','mismatch/images1/');
define('msize',1024*50);
if(isset($_SESSION['user_id']))
{
$id1=$_SESSION['user_id'];
if(isset($_POST['submit']))
{
$dbc=mysqli_connect('localhost','root','','mismatch')
or die('Error connceting database');
if((!empty($_POST['fname']))&&(!empty($_POST['lname']))&&(!empty($_POST['bday']))&&
(!empty($_POST['sex']))&&(!empty($_POST['nation'])))
{
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$bday=$_POST['bday'];
$sex=$_POST['sex'];
$nation=$_POST['nation'];
if(!empty($_FILES['screenshot']['name']))
{
$screenshot=$_FILES['screenshot']['name'];
if(($_FILES['screenshot']['size']>0)&&($_FILES['screenshot']['size']<msize))
{
$target=upath.$screesnhot;
if(move_uploaded_file($_FILES['screenshot']['tmp_name'],$target))
{
$query="SELECT * FROM mismatch_user WHERE user_id=$id1";
$result=mysqli_query($dbc,$query)
or die('Error getting data');
if(mysqli_num_rows($result)==1)
{
$query1="UPDATE mismatch_user SET firstname='$fname', lastname='$lname', birthday='$bday',".
"gender='$sex', country='$nation', picture='$screenshot' WHERE user_id='$id1'";
 
$result1=mysqli_query($dbc,$query1)
or die('Error quering database');
echo'<br/><b>Profile Edited successfully</b>';
}
 
 
}
else
{
echo'<br/><b/>File cannot be uploaded';
}
}
else
{
echo'<br/><b/>Check size of Image.';
}
}
else
{
$query1="UPDATE mismatch_user SET firstname='$fname', lastname='$lname', birthday='$bday',".
"gender='$sex', country='$nation' WHERE user_id='$id1'";
$result2=mysqli_query($dbc,$query2)
or die('Error quering database');
echo'<br/><b>Profile Edited successfully</b>';
}
}
else
{
echo'<br/><b/>Please Enter all the information.';
}
}
}
?>
</body></html>
Link to comment
Share on other sites

- Firstly, display your code in code tags, identified by the "<>" icon on the top

- Second, make you have all your logic ABOVE the HTML.

- Trim your code to the stuff that is relevant

- Ask a question and tell us what is not working.

- It would be wise to indent your code :/ this is so hard to read.

 

Seriously, you arent even asking a question.

Edited by DaveyK
Link to comment
Share on other sites

I dont see your code in code blocks. So thats one thing you havent done. None of the things I said will actually solve anything. But it MAY help us understand your problem and give guidance (we wont solve your problem for you. Well sometimes we will.).

 

REGARDLESS, I am completely missing your problem. What is the issue?!

Link to comment
Share on other sites

HERE, this is what we need. Not a file, but code in code blocks:


<?php
define('upath','mismatch/images1/');
define('msize',1024*50);
if(isset($_SESSION['user_id']))
{
	$id1=$_SESSION['user_id'];
	if(isset($_POST['submit']))
		{
			$dbc=mysqli_connect('localhost','root','','mismatch')
				or die('Error connceting database');
			if((!empty($_POST['fname']))&&(!empty($_POST['lname']))&&(!empty($_POST['bday']))&&
				(!empty($_POST['sex']))&&(!empty($_POST['nation'])))
			{
				$fname=$_POST['fname'];	
				$lname=$_POST['lname'];
				$bday=$_POST['bday'];
				$sex=$_POST['sex'];
				$nation=$_POST['nation'];
				if(!empty($_FILES['screenshot']['name']))
				{
				$screenshot=$_FILES['screenshot']['name'];
				if(($_FILES['screenshot']['size']>0)&&($_FILES['screenshot']['size']<msize))
					{
					$target=upath.$screesnhot;
					if(move_uploaded_file($_FILES['screenshot']['tmp_name'],$target))
						{
						$query="SELECT * FROM mismatch_user WHERE user_id=$id1";
						$result=mysqli_query($dbc,$query)
							or die('Error getting data');
						if(mysqli_num_rows($result)==1)
							{
						$query1="UPDATE mismatch_user SET firstname='$fname', lastname='$lname', birthday='$bday',".
							"gender='$sex', country='$nation', picture='$screenshot' WHERE user_id='$id1'";

								$result1=mysqli_query($dbc,$query1)
								or die('Error quering database');
								echo'<br/><b>Profile Edited successfully</b>';
							}
							
					
						}
					else
						{
							echo'<br/><b/>File cannot be uploaded';
						}
					}
				else
					{
					echo'<br/><b/>Check size of Image.';
					}
				}
				else
					{
					$query2="UPDATE mismatch_user SET firstname='$fname', lastname='$lname', birthday='$bday',".
							"gender='$sex', country='$nation' WHERE user_id='$id1'";
					$result2=mysqli_query($dbc,$query2)
					or die('Error quering database');
					echo'<br/><b>Profile Edited successfully</b>';
					}
			}				
		else
		{
			echo'<br/><b/>Please Enter all the information.';
		}
	}
}
?>

<html>
<head><title>
Edit Profile
</title></head>
<body>
<h1 style="color:brown;"><center>Please Edit your profile to find new mismatch.</center></h1>
<form enctype="multipart/form-data" method="post" action="<?php echo$_SERVER['PHP_SELF']; ?>">
<p><i><center>This information would be seen by others and easily get connected to you.</center></i></p>
<hr/>
<img src="images1\mismatch.jpg" width="400" height="400" alt="mismatch image." align="right">
<fieldset style="width:250">
<legend>Edit your profile</legend>
<table><tr>
<td><label for="fname">Firstname:</label></td>
<td><input type="text" id="fname" name="fname"><br/></td>
</tr><tr>
<td><label for="lname">Lastname:</label></td>
<td><input type="text" id="lname" name="lname"><br/></td>
</tr><tr>
<td><label for="bday">Birthday:</label></td>
<td><input type="text" id="bday" name="bday"><br/></td>
</tr><tr>
<td><label for="sex">Gender:</label></td>
<td><input type="text" id="sex" name="sex"><br/></td>
</tr><tr>
<td><label for="nation">Country:</label></td>
<td><input type="text" id="nation" name="nation"><br/></td>
</tr><tr>
<td><label for="screenshot">Picture:</label></td>
<td><input type="file" name="screenshot" value="Choose image"><br/></td>
</tr></table>
</fieldset>

<input type="submit" name="submit" value="Save">

</body></html>

Okay so it doesnt update even tho you would expect it to. A good step at debugging would be to add echos or var_dumps to see which IFs are triggering and which arent. Its a really basic process, but you can try something like:

if(isset($_SESSION['user_id']))
{
  echo 'User session is set<br>';
	$id1=$_SESSION['user_id'];
	if(isset($_POST['submit']))
		{
 echo 'Form is submitted<br>';
			$dbc=mysqli_connect('localhost','root','','mismatch')
(...)

Do that on several key IF statements, to see where your code is going. The issue doesnt have to be lay in the PHP. Try it out and see how far your script actually goes.

Link to comment
Share on other sites

If nothing is echoed then that means your SESSION is not set. Try doing a var_dump on $_SESSION on the top of the page, like so:


<?php
define('upath','mismatch/images1/');
define('msize',1024*50);

var_dump($_SESSION);
  die(); // kill the rest of the page
if(isset($_SESSION['user_id']))
{

What does that echo?

Link to comment
Share on other sites

That is because you are not creating a session, check out the function session_start() and how you can use it.

 

Also, I PRESUME you are checking for a session because you have some sort of login functionality. Since you built a login system, I presume you also know how to handle sessions.

Link to comment
Share on other sites

yes i have built a login system that redirects to edit page means current page

 

but when we edit our information no data is updated in database

and i have created a session using session_start();

so i didnt use it again in edit page

because session_id is set now.

 

i am attaching login script too please have a look on it too.

loginx.php

Link to comment
Share on other sites

The session on a page will only exist if you call session_start() on that page, and as the var_dump shows the issue you are facing has nothing to do with your update. The issue lies in the fact that you dont have a session active on that page. Make sure you have a session and go from there.

Link to comment
Share on other sites

i wrote this this session_start() in my script

but nothing happened

same output

nothing.

but when i write if(!isset($_SESSION['user_id'])) at place of if(isset($_SESSION['user_id']))

 

i got feedback 'profile updated successfully'.

but when i saw my database nothing was updated.

Link to comment
Share on other sites

But following script causing another problem
you see i get following errors
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\xampp\htdocs\raghu\mismatch\edit.php on line 26

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php121B.tmp' to 'images1/' in C:\xampp\htdocs\raghu\mismatch\edit.php on line 26
 
if(move_uploaded_file($_FILES['screenshot']['tmp_name'],$target))
{
$query="SELECT * FROM mismatch_user WHERE user_id=$id1";
$result=mysqli_query($dbc,$query)
or die('Error getting data');
if(mysqli_num_rows($result)==1)
{
$query1="UPDATE mismatch_user SET firstname='$fname', lastname='$lname', birthday='$bday',".
"gender='$sex', country='$nation', picture='$screenshot' WHERE user_id='$id1'";
 
$result1=mysqli_query($dbc,$query1)
or die('Error quering database');
echo'<br/><b>Profile Edited successfully</b>';
Link to comment
Share on other sites

i wrote this this session_start() in my script

but nothing happened

same output

nothing.

but when i write if(!isset($_SESSION['user_id'])) at place of if(isset($_SESSION['user_id']))

 

i got feedback 'profile updated successfully'.

but when i saw my database nothing was updated.

 

ofc this works, but thats exactly what you DONT want.

 

Please put your code in code blocks.

 

What you need to think about is: how can I solve this issue on my own without making someone else do it for me. You have two error messages, so copy the first and google it.

 

http://bit.ly/11GeVRp

 

The first link is a stackoverflow question. The answer is on that page. Do some research and dont expect us to do it for you.

Edited by DaveyK
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.