Solved

How can I call a "user_id" of a newly created record and use it as a variable on a Subsequent form?

Posted on 2014-07-29
3
207 Views
Last Modified: 2014-07-30
Upon adding a new employee record, the user is passed to a form where training records are assigned to the new employee. I cannot pass the user_id to the records form as that is auto-incremented when the employee record is created. So, I think it best to pass a unique variable (email) in the URL instead, then create a query to retrieve the user_id on the records page.  Both the user_id and item_id will be added to a linking table when the form is submitted.

Here is my script for the redirect from my create-user.php page.

// Redirect the user to the page that displays user information
$link = trim($_REQUEST['email2']);
header("Location: create_user_training.php?link=$link");
exit();

Open in new window


Which is passing the variable fine:
http://mysite.com/create_user_training.php?link=myemail@somedomain.com

And here is my code for the create_user_training.php page:
<?php

require_once 'scripts/app_config.php';
require_once 'scripts/database_connection.php';

//check for which boxes are selected then update database 
if(isset($_POST['checkbox'])) { 
foreach($_POST['checkbox'] as $i) { 

$sql1="INSERT INTO user_training_items (item_id, user_id)VALUES('{$_POST['item_id'][$i]}', '{$_POST['user_id'][$i]}')";
$result1=mysql_query($sql1);
} 
echo "Thank you!  Both the user and thier training records have been entered.  Have a great day!";
} else {
?>
<p>Please select which training items the user must accomplish</p> 
<form method="post" action="create_user_training.php"> 
<?php

$linked = "SELECT user_id FROM users where email = {$_POST['link']}";
$result2=mysql_query($linked);

$sql="SELECT * FROM training_items";
$result=mysql_query($sql);

// Count table rows 
$count=mysql_num_rows($result);

$i = 0; 
while($rows=mysql_fetch_array($result)){ 
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $i++;?>" /></td>
<td align="center"><? echo $rows['item_title'];?></td>
<input type="hidden" name="item_id[]" id="item_id" value="<? echo $rows['item_id'];?>"/><br />
<input type="hidden" name="user_id[]" id="user_id" value="<? echo $result2;?>"/>
</td>
</tr>

<?php
}
?>

<p><input type="submit" name="Submit" value="Add"></p>
</form>
<?php
}
?>

Open in new window


As you can see, first it sees if the form has been submitted to itself, and if so, adds the necessary form data. If not, it presents the form in which the user selects the training items.  The item_id's are making it into the database but not the associated user_id.  Those are zeroed out. My guess the error resides in my attempt to define the user_id.

Any suggestions?
0
Comment
Question by:chaddcarr
3 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40227981
This is not rocket science, but you need to get at least some foundation in PHP and MySQL to begin a task like this.  This article can help you get started.  If you do not have the time to spend a couple of months studying PHP and MySQL in structured lessons, you might want to hire someone to build this application.  The tradeoff is really simple: time vs money.  
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 11

Accepted Solution

by:
Murfur earned 150 total points
ID: 40229363
While you can do the SELECT method you have used, you can also do it without passing the email address e.g.
SELECT MAX(user_id) from users;
or
SELECT user_id FROM users ORDER BY user_id DESC LIMIT 1;

but while they both return the highest value of the user_id there is no guarantee that it is the one that relates to the insert you are referring to as there is the potential that another administrator could have added another user in-between  your submission and your query in effect giving you the user_id for THEIR newly created user.

The most efficient way is to use the built-in MySQL functionality...

If the user_id is generated by the db column with AUTO_INCREMENT then you can use mysql_insert_id() immediately after the INSERT command to retrieve the id generated by the last insert.
<?
# insert new user
mysql_query( $insert_sql_string );
# get new user_id value
$new_iser_id = mysql_insert_id();
?>

Open in new window

Then you can pass the $new_user_id variable back to the training values form as a hidden field.
NOTE: if the user_id db column DOES NOT have AUTO_INCREMENT then the function will always return zero

Also, you should start planning to updating your mysql extension code to mysqli as mysql has been deprecated - see here for the details and list of improvements: http://php.net/manual/en/mysqli.overview.php
0
 

Author Closing Comment

by:chaddcarr
ID: 40230072
Thanks Murfur!  Most helpful!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now