[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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

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
chaddcarr
Asked:
chaddcarr
1 Solution
 
Ray PaseurCommented:
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
 
MurfurFull Stack DeveloperCommented:
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
 
chaddcarrAuthor Commented:
Thanks Murfur!  Most helpful!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now