Jump to content

[SOLVED] php and mysql


ngreenwood6

Recommended Posts

I have run into a pretty stumping question for me. I have a database with 2 tables. It looks like this:

 

table:users

field:id

field:username

field:password

table:work

field:id

field:job

field:user_id

 

Now the problem that I am running into is registering a user. When I register a user I want to enter there user id into the job that they select. However, the user id field is an auto_increment field. Can someone help me to get this working? Thanks in advance for any help.

 

Link to comment
https://forums.phpfreaks.com/topic/136263-solved-php-and-mysql/
Share on other sites

something like...

 

$query1 = "INSERT INTO users(id, username, password) VALUES (NULL, '$username', '$password')";
...
$lastid = mysql_insert_id($conn); //where $conn is the resource identifier you used in mysql_connect
$query2 = "INSERT INTO work (id, job, user_id) VALUES (NULL, '$job', '$lastid')";

Link to comment
https://forums.phpfreaks.com/topic/136263-solved-php-and-mysql/#findComment-710832
Share on other sites

I have tried this code:

 

<?php
session_start();

//include the variables
include("../vars.php");

//check that the session is set
if(!$_SESSION['logged_in'])
{
header("Location:not_logged.php");
}

//make the values a variable
foreach($_POST as $value);

//connect to the server
$conn = mysql_connect($host, $db_user, $db_pass);

//select database
mysql_select_db($db);

//encrypt the password
$encrypted_password = md5($password);

//make the query
$query = "INSERT INTO login (id, username, password, name, email, school_1, school_2, school_3, membership) VALUES (NULL, '$add_username','$encrypted_password','$name','$email','$school_1','$school_2','$school_3','$add_membership')";

if(empty($username))
{
echo "Please enter a username.";
}
elseif(empty($password))
{
echo "Please enter a password.";
}
elseif(empty($name))
{
echo "Please enter a name.";
}
elseif(empty($email))
{
echo "Please enter an email address.";
}
else if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
echo "Please enter a valid email address!";
}
elseif($password != $confirm_password)
{
echo "Your password's do not match.";
}
else
{
$results = mysql_query($query);

//update id
$last_id = mysql_insert_id($conn);

//make the school query
$school_query = "UPDATE schools SET tech_id='$lastid' WHERE school_1='$school_1' AND school_2='$school_2' AND school_3='$school_3'";

$school_results = mysql_query($school_query);

echo "You have added $name to the database. <a href=\"admin.php?edit=users\">Click here</a> to continue.";
}

?>

 

I am getting this error:

 

Notice: Undefined variable: lastid in C:\wamp\www\techs\added_user.php on line 60

 

Can someone help me as to what I am doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/136263-solved-php-and-mysql/#findComment-710858
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.