Jump to content

I would like to grab the ID of an edited record and insert into another table. Is this possible?


Go to solution Solved by requinix,

Recommended Posts

Greetings mates,

I have not been here for years. I have been thrust into an app written by someone else.

The requirement is to amend, not edit, an existing record. In other words, rather than edit an existing record, management would like an empty new text box to enter amended data and save to another table so that when queried, would display existing record and the newly amended record.

Below is an ID of an existing record:

	$sql = "SELECT * FROM DBO.existingTable where year_created = 2019 Order by Id";
    echo '<table id="results-table">
	<thead>
		<tr>
			<th>ID</th>
			<th>Edit</th>
			<th>View PDF</th>
			<th>Task Lead</th>
			<th>Task Title</th>
			<th>Division</th>
          		</tr>
	</thead>';

	while ( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_BOTH  ) ) {
		$div = ($row['division'] == "1" ? "Divname" : ($row['division'] == "2" ? "Highway" : "NULL"));
		$taskType = ($row['task_type'] == 0 ? "Annual" : ($row['task_type'] == 1 ? "Carryover" : ($row['task_type'] == 2 ? "New" : "NULL")));
		if($row['year_created'] == 2019){
			echo '<tr id="taskId">
			<td>'.$row['Id'].'</td>
			<td><a href="project_edit.html?id='.$row['Id'].'" target="_blank">Edit</a></td>
			<td><input type="button" onclick="getReport('.$row['Id'].')" value="View PDF"></td>
			<td>'.$row['task_lead'].'</td>
			<td>'.$row['task_title'].'</td>
			<td>'.$div.'</td>
    		</tr>
		}

	}
	echo '</table>'

The above code isa snippet of code in edit.php.

What I would like to do is grab the ID which is '.$row['Id'].' from this edit page and use it when inserting the amended code which is in insert,php file:

//The ID from edit.php code should go here
$task_lead = (empty($_POST['task_lead'])) ? "''" : "'" . str_ireplace( "'", "", $_POST[ 'task_lead' ] ) . "'";
$task_title = (empty($_POST['task_title'])) ? "''" : "'" . str_ireplace( "'", "''", $_POST[ 'task_title' ] ). "'";
$division = (empty($_POST['division'])) ? "''" : "'" . str_ireplace( "'", "", $_POST[ 'division' ] ) . "'";

//Then my insert statement should be something like this:
	$sql = "INSERT INTO DBO.Amended_Records ({id from table with existing record}, amended_taskActivities, amended_taskProducts,amendscope_dev_fhwa, amendconsultant_procurement, amendcontract_negotiations, amendconsultant_notice, amendtotal_duration, year_created)
	VALUES ({$rowId - that I need to get from edit.php}, $amendproposed_activities, $amendanticipated_products,$amendscope_dev_fhwa,$amendconsultant_procurement,$amendcontract_negotiations,$amendconsultant_notice,$amendtotal_duration, 2021)";

When I insert this record into the DB, the id from edit.php is getting inserted as null. 

Can you please advise how I can resolve this?

  • Solution

Are you talking about project_edit.html? Is that where the form with task_lead and task_title and such are?

That page gets the ID from the URL, right? Put the ID into the form as a hidden input so that you get a copy of that along with all the other data you want.

Thanks so much for your response.

The page said edit.html and it has the task_lead, task_title and a few more. I just shortened it for emphasis.

The code I posted comes from edit.php

I also see parameters like these:

$rowId = (int) $_GET[ 'rowId' ];
$taskId =  $row['Id'];

Not sure which one of those is passed through URL.

The edit.html doesn't have Id either coming from it or passed to it.

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.