Jump to content

not connecting to the database.


dominic600

Recommended Posts

So im creating an 'edit profile' page and when i go to test it, it will not connect to the database.

 

I know its not th database cause it still allows me to login, post comments and stuff like that.

 

Heres the code its probably something obivious that im just over looking but im not sure..

 

<?php require("top.php"); ?>

<div id='content'>
<?php

if($username){
require('scripts/connect.php');


if ($_POST['savebtn']){

$firstname = fixtext($_POST['firstname']);
$lastname = fixtext($_POST['lastname']);
$email = fixtext($_POST['email']);
$website = fixtext($_POST['website']);
$youtube = fixtext($_POST['youtube']);
$bio = fixtext($_POST['bio']);
$password = fixtext($_POST['password']);

$name = $_FILES['avatar'] ['name'];
$type = $_FILES['avatar'] ['type'];
$size = $_FILES['avatar'] ['size'];
$tmpname = $_FILES['avatar']['tmpname'];
$ext = substr($name, strrpos($name, '.'));

if ($firstname && $lastname && $email && $password){
if (strstr($email, "@") && strstr($email, ".") && strlen($email) >= 6){

$password = md5($password);
$query = mysql_query("SELECT password FROM users WHERE id='$userid'");
$numrows = mysql_num_rows($query);
if($numrows == 1){



	mysql_query ("UPDATE users SET firstname='$first_name' WHERE id='$userid'");
	mysql_query ("UPDATE users SET lastname='$last_name' WHERE id='$userid'");
	mysql_query ("UPDATE users SET email='$email' WHERE id='$userid'");
	mysql_query ("UPDATE users SET website='$website' WHERE id='$userid'");
	mysql_query ("UPDATE users SET youtube='$youtube' WHERE id='$userid'");
	mysql_query ("UPDATE users SET bio='$bio' WHERE id='$userid'");

	if ($name){
		$avatarname = $username.$ext;
		move_upload_file($tmpname, "avatars/$avatarname");
		mysql_query ("UPDATE INTO users SET avatar='$avatarname' WHERE id='$userid'");

				}

			echo "Changes have been saved."; 
		}
		else
			echo "Your password was incorrect.";
}
else	
	echo "You did not enter a valid Email.";
}
	else
		echo "You did not provide the required info.";

}

$query = mysql_query("SELECT * FROM users WHERE id='$getid'");
$numrows = mysql_num_rows($query);
if($numrows == 1){
$row = mysql_fetch_assoc($query);
$id = $row['id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email = $row['email'];
$avatar = $row['avatar'];
$bio =($row['bio']);
$website = $row['website'];
$youtube = $row['youtube'];
$lastlogin = $row['last_login'];
$active = $row['active'];
$locked = $row['locked'];
$date = $row['date'];

}
else	
	echo "An error occured while connecting to the datatbase.";



$infoform = "<form action='editprofile.php' method='post'>
<table cellspacing='10px'>

<tr>
	<td>First Name:</td>
	<td><input type='text' name='firstname' class='textbox' size='40' value='$firstname'><font color='red'>*</font></td>
</tr>
	<tr>
	<td>Last Name:</td>
	<td><input type='text' name='lastname' class='textbox' size='40' value='$lastname'><font color='red'>*</font></td>
</tr>
	<tr>
	<td>E-mail:</td>
	<td><input type='text' name='email' class='textbox' size='40' value='$email'><font color='red'>*</font></td>
</tr>
	<tr>
	<td>Avatar:</td>
	<td><input type='file' name='avatar'></td>
</tr>
	<tr>
	<td>Website:</td>
	<td><input type='text' name='website' class='textbox' size='40' value='$website'></td>
</tr>
	<tr>
	<td>Youtube User Name:</td>
	<td><input type='text' name='youtube' class='textbox' size='40' value='$youtube'></td>
</tr>
	<tr>
	<td>Bio:</td>
	<td><textarea name='bio' cols='35' rows='5' class='textbox'>$bio</textarea></td>
</tr>
<tr>
	<td>Current Password:</td>
	<td><input type='password' name='password' class='textbox' size='40'><font color='red'>*</font></td>
</tr>
	<tr>
	<td></td>
	<td><input type='submit' name='savebtn' value='Confirm Changes' class='button'></td>
</tr>
</table>
</form>";

echo "$infoform";

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////

echo "<br /><hr /><br />";

$passform ="<form action='editprofile.php' method='post'>
<table cellspacing='10px'>

<tr>
	<td>Current Password:</td>
	<td><input type='password' name='oldpass' class='textbox' size='40'><font color='red'>*</font></td>
</tr>
<tr>
	<td>New Password:</td>
	<td><input type='password' name='newpass' class='textbox' size='40'><font color='red'>*</font></td>
</tr>
<tr>
	<td>Confirm New Password:</td>
	<td><input type='password' name='confirmpass' class='textbox' size='40'><font color='red'>*</font></td>
</tr>
<tr>
	<td></td>
	<td><input type='submit' name='updatepass' class='submitbtn' size='40'><font color='red'>*</font></td>
</tr>
</table></form>";

if($_POST['passbtn']){
$oldpass = fixtext($_POST['oldpass']);
$newpass = fixtext($_POST['newpass']);
$confirmpass = fixtext($_POST['confirmpass']);

if($oldpass && $newpass && $confirmpass){
	if ($newpass == $confirmpass){
	$oldpass = md5($oldpass);
	$newpass = md5($newpass);
	$query = mysql_query("SELECT password FROM users WHERE id='$userid'");
	$numrows = mysql_num_rows($query);
	if($numrows == 1){
			mysql_query ("UPDATE users SET password='$newpass' WHERE id='$userid'");
		echo"Your password has been changed.";

	}
	else
		echo "Your current password is incorrect.";
	}
	else
		echo "Your new passwords did not match.";

}
else
	echo"You did not fill in all required fileds.";


}
else	
echo "$passwform";


}
else
echo "you must be logged in to do that.";
?>


</div>
<div id='footer'></div>
</body>
</html>

Link to comment
Share on other sites

Hi,

 

Try moving connect.php to the very top of the file (where the top.php line is located)?

 

I assume the only error you're seeing is,

 

An error occured while connecting to the datatbase.

 

Try adding something like mysql_error(); to your code so you can see a more detailed error message.

Link to comment
Share on other sites

Without the actual symptom or error you are getting that leads you to believe that - "... an 'edit profile' page and when i go to test it, it will not connect to the database.", it is not directly possible to help you with what your code is doing. We don't have access to all your code, your server, or your database, so we must rely on just the information that you supply in your posts.

Link to comment
Share on other sites

There's no need for all of this:

<?php

mysql_query ("UPDATE users SET firstname='$first_name' WHERE id='$userid'");
	mysql_query ("UPDATE users SET lastname='$last_name' WHERE id='$userid'");
	mysql_query ("UPDATE users SET email='$email' WHERE id='$userid'");
	mysql_query ("UPDATE users SET website='$website' WHERE id='$userid'");
	mysql_query ("UPDATE users SET youtube='$youtube' WHERE id='$userid'");
	mysql_query ("UPDATE users SET bio='$bio' WHERE id='$userid'");
?>

 

Just do this:

mysql_query("UPDATE users SET firstname = '$firstname', lastname = '$lastname', email = '$email', website = '$website', youtube = '$youtube', bio = '$bio' WHERE id = '$userid'") or trigger_error(mysql_error());

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.