I'm new to php and dymanic web applications
using PHP I'm tyring to read from table Id(primary key) to other form and save othe table as forign key.
I appreciat for you help

Dani AI

Generated

Quick practical summary (building on points from , , and @diafol):

The immediate PHP issues are simple: use the correct function name header() (no typo) and do not send any output (including stray whitespace or BOM) before calling header(). After header('Location: ...') call exit; so the script stops. Beyond that, avoid the old mysql_* extension (deprecated/removed); use PDO or MySQLi with prepared statements, and handle the two inserts as a single logical operation.

Recommended workflow

  • Insert the person row with a prepared statement.
  • Immediately obtain the new primary key (PDO::lastInsertId() or mysqli_insert_id()).
  • Insert the academic row using that id as the foreign key.
  • Wrap both inserts in a transaction so either both succeed or both roll back.
  • Enforce the relationship in the database with an InnoDB foreign key constraint.
  • If you must pass the id between pages, prefer storing it in $_SESSION rather than exposing it in a GET parameter.

Example (PDO outline):

// connect, begin transaction
$pdo->beginTransaction();
$pdo->prepare('INSERT INTO person (staff_id, first_name) VALUES (?, ?)')->execute([$staffId, $firstName]);
$personId = $pdo->lastInsertId();
$pdo->prepare('INSERT INTO academic (person_id, degree) VALUES (?, ?)')->execute([$personId, $degree]);
$pdo->commit();
header('Location: register/academic.php');
exit;

Final notes: validate and sanitize inputs, check and handle exceptions, and prefer server-side session state for continuity between multi-step forms. This keeps data consistent, prevents SQL injection, and avoids the redirect/output pitfalls discussed earlier.

Recommended Answers

All 7 Replies

What do you have so far?

This is what I have Sofar :
I created From and save to mysql table let say <person>.. so I wanted to continue to next step of registeration but save diffrent table<academicq> so I want to get to Id from the <person> table and save mysql as foriegn key... this is whay I have so far...

//my php code for <person>
<?php 

include("connection/sqlconnect.php");

// get values from form
$staffId=$_POST['staffid'];
$fName=$_POST['fName'];
$lName=$_POST['lName'];
$country=$_POST['countryoforigin'];
$nationality=$_POST['nationality'];
$race=$_POST['race'];
$religion=$_POST['religion'];
$general_status=$_POST['general_status'];
$phone=$_POST['phone'];
$department=$_POST['department'];
$cposition=$_POST['position'];
$email=$_POST['email'];
$category=$_POST['category'];
$year=$_POST['year'];

// Insert data into mysql

echo "hithere ";


$sql= "INSERT INTO expertdirectory (staffId, firstName, lastName, countryoforigin, nationality, race, religion, general_status,
                                    department, cposition, year_employed, handphone, email) VALUES 
                                    ('$staffId','$fName', '$lName', '$country', '$nationality', '$race', '$religion',
                                    '$general_status', '$department', '$cposition', '$year', '$phone', '$email')";




$result=mysql_query($sql);
if($result)
{
echo "<BR>";

hearder("location: register/academic.php?staffId=$_POST[staffId]");
echo "the data sent somewhere";
}


mysql_close();
?>

And I get this error:
Fatal error: Call to undefined function hearder() in C:\xampp\htdocs\projectX\registration1.php on line 42
pls halp me thanks

It's header(), remove the 'r'.

Can I give you some additional advice? You said you are trying to save <person> table to <academic> table as foreign key right?

So why don't you try to normalize the database since the connector is staffId why dont you use left join functions? It will make it short and faster in computation of the database.

Member Avatar for Member #120589
header("location: register/academic.php?staffId=$_POST[staffId]");
    echo "the data sent somewhere";
}

COuld be an issue. It may be best to save the staff_id in a session as opposed to making it visible and liable to messing with. ANyway, would the user know his/her staff_id to place it in a form? Or is this placed there from a previous form? Bit confused.

there can be no output to the browser be for the header().
So remove echo statments at line 24 and 38
also move the comment at line 1 inside the <?php tag

Thank You guys That help a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.