I´m facing quite interesting problem IMO. I´m working with MySQL & PHP and I have created two tables - 1. containing ID | Firstname | Lastname ; 2. containing ID | Phone Number. What I want to do is following: User can create a new entry in both tables. I want to achieve the state where in the second table (containing ID and Phone Number) there will be phone number added by the user and the ID of this phone number will match the ID of the person in the first table who the number belongs.
Let me give You an example:
#1 Table:
ID FirstName LastName
1 John Cena
2 Tom Jerry
#2 Table:
ID PhoneNumber
1 123456
1 654321
=> John Cena with the ID of "1" will have in the second table two numbers.
What I´m struggling to do is the PHP function which will return the ID from the first table (named "People") based on firstname and lastname and then insert this value into the second table (named "PhoneNums").
At the moment I have this:
function GetUserID($firstname, $lastname) {
$conn = OpenCon();
$stmt = $conn->prepare("SELECT userid FROM People WHERE firstname='$firstname' AND lastname='$lastname'");
$stmt->execute();
}
function AddNum($phonenum, $firstname, $lastname) {
try {
$conn = OpenCon();
$user_id = GetUserID($firstname, $lastname);
$sql = "INSERT INTO PhoneNums (userid, number)
VALUES ('$user_id', '$phonenum')";
$conn->exec($sql);
echo "New record added successfully";
} catch(PDOException $e) {
echo "<br>" . $e->getMessage();
}
}
Do You have any idea how to do it or how to correct my solution? :D