Jump to content

[SOLVED] Display and store MYSQL data


winmastergames

Recommended Posts

Firstly Sorry if this is already answered..

I need some help with storing and displaying Mysql data I have a MYSQL user auth script multiuser one I need to make a script that would get the user thats logged in like "Bob" or something then be able to get that data from a MYSQL database so the user can See the Data they inputted in Registration so they can edit it and stuff We bit confusing please ask questions Its really annoying me im trying to rip my head out trying to make this so i hope you guys can help me :) Thanks

Link to comment
Share on other sites

Well I have a simple script here i made using the tutorials you gave me

 

View Data Script:

<html>
<head>
<title>Displaying MySQL Data</title>
</head>
<body>
<?php
$db_host = "localhost";
$db_user = "root";
$db_pwd = "mypass";
$db_name = "test";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<table>
<?php
$sql = "SELECT * FROM data";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['slogan']."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>

 

 

Store Data Script:

<?php
$db_host = "localhost";
$db_user = "root";
$db_pwd = "mypass";
$db_name = "test";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Title: <input type="text" name="title"><br>
Slogan: <input type="text" name="slogan"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$title = $_POST['title'];
$slogan = $_POST['slogan'];
mysql_query("INSERT INTO `data` (title, slogan) VALUES ('$title', '$slogan')");
echo "Success! Your favourite colour has been added!";
}
?>
</body>
</html>

Is that Ok? no problems with that?

And how will i do the GET thing with the sesson would you please be able to give me an example and maybe what it does like explained if possible

Link to comment
Share on other sites

This is quite confusing  ??? isnt it? ill explain it in more detail

The user will login into their profile they will then be able to change Data that is in the MYSQL database under there name only..

 

Then in another PHP script i want it to be able to connect to the MYSQL database and let the user have like index.php?user=bob if their user name is bob and it will view the MYSQL data for their name

 

Is that a bit better Well hopefully

Link to comment
Share on other sites

Oh ive just done some Searching and finally found a tut cause the one you gave me was confussing so the tut told me to use this for storing Sessions is that right

<?php
session_start(); //Start Session
_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>

EDIT: forgot some code in it

 

Link to comment
Share on other sites

On your login page, when you authenticate them, do a SELECT ID (or whatever field you want to store, ID, username, email..blah blah).  Then fetch that data and use:

 

$_SESSION['id'] = $row[0];

 

Assuming the first field is the ID field, change to the field you want to store.

Link to comment
Share on other sites

It has an error when i try to make sessions work anyway heres the login script see if you can make it work

<?php 
// Connects to your Database 
mysql_connect("localhost", "root", "mypass") or die(mysql_error()); 
mysql_select_db("test") or die(mysql_error()); 

//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))

//if there is, it logs you in and directes you to the members page
{ 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check )) 
{
if ($pass != $info['password']) 
{
}
else
{
header("Location: members.php");

}
}
}

//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted

// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database

if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());

//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check )) 
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);

//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}
else 
{ 

// if login is ok then we add a cookie 
$_POST['username'] = stripslashes($_POST['username']); 
$hour = time() + 3600; 
setcookie(ID_my_site, $_POST['username'], $hour); 
setcookie(Key_my_site, $_POST['pass'], $hour);	

//then redirect them to the members area 
header("Location: members.php"); 
} 
} 
} 
else 
{	

// if they are not logged in 
?> 
<?php
$userlog = $_POST['username']
session_start(); //Start Session
_SESSION['log31531'] = $userlog; // store session data
?>
$_SESSION['views'] = 1; // store session data
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
<table border="0"> 
<tr><td colspan=2><h1>Login</h1></td></tr> 
<tr><td>Username:</td><td> 
<input type="text" name="username" maxlength="40">
</td></tr> 
<tr><td>Password:</td><td> 
<input type="password" name="pass" maxlength="50">
</td></tr> 
<tr><td colspan="2" align="right"> 
<input type="submit" name="submit" value="Login"> 
</td></tr> 
</table> 
</form> 
<?php 
} 

?>

 

I tried getting the data using POST but normally i use GET so it wont work using they way i tried it I really dont know how to do sessions how do you make it store the Username as a session

it just displays this error

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\userauthttt\login.php on line 77

 

Link to comment
Share on other sites

It's not too hard at all.  You're getting a straight answer, but you don't understand it.  You need to read some more tutorials and learn more PHP it seems.

 

Is this too hard? is there a better way i should do this because no one here seems to be getting me a straight answer

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.