Chadd C
asked on
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.
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:
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?
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();
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
}
?>
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Murfur! Most helpful!
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html