Jump to content

Using session ID and username to update database?


orangecat

Recommended Posts

I'm helping out a friend who owns a boarding kennel. She would like an online site where she or a client can register, add their dogs to their profile and other info.

 

I know CSS and HTML but have very, very little experience in PHP, I figured it would be fun to give this a try and learn something new.

 

So far I've managed to create a register and log in area, and now I'm trying to make it possible for someone to add a dog breed from a drop down list to their "page" after they've logged in.

 

I cannot get the data to insert into that specific user's table.

 

I'm trying to use the session id and session username as the variable, and this is where the problem comes in. If I type the userid and the username out then the data will update fine...but that's not practical. I need it to know which user is logged in and update them accordingly.

 

Anyway, what I'm typing here makes sense in my head but I've been staring at this computer all day and it's possible I'm way out in left field,  so here's the code to see for yourself. (chances are it's something totally obvious....or I have stuff in there that doesn't belong, I haven't a clue.)

 

Thank you in advance to anyone willing to help me out!

 

 

The form

<?php session_start();
$_SESSION['userid']=$userid;?>


<html><body>
<h4></h4>
<form action="process.php" method="post"> 
<select name="breed" id="breed"> 
<option value="collie">Collie</option>
<option value="aussie">Aussie</option>
</select>
<select name="sex"> 
<option>Dog</option>
<option>Bitch</option>
</select>
<input type="submit" />
</form>
</body></html>

 

 

The php for that form

<?php session_start();
$_SESSION['userid']=$userid;
$_SESSION['Username']=$username;?>



<html><body>
<?php

$host="localhost"; // Host name
$username="silver_phptest"; // Mysql username
$password="bowser"; // Mysql password
$db_name="silver_phptestingbase"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$breed=$_POST['breed'];


// Insert data into mysql

mysql_query("UPDATE users SET dogs = '$breed'
WHERE userID = '$userid'");


// close connection
mysql_close();
?>
</body></html>

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.