Assume we have three tables.
- characters with the fields id, project_id, name, basic_info_id, appearance_id.
- char_appearance with fields id, eyes, hair, body, clothing, other.
- char_basic_info with fields id, alias, age, description, personality, backstory.
Note that the basic_info_id and appearance_id fields in the characters table are remnants of an old design: now they are always the same value as the character id. Example entry: id="12" project_id="3" name="Harry Potter" basic_info_id="12" appearance_id="12", img_id="117"
. Notice how id=basic_info_id=appearance_id=12.
Anyway, I’ve written code to import (i.e. copy) a character.
$pid = $_POST['projid'];
$charid = $_POST['charid'];
$charQuery = "SELECT name FROM characters WHERE id='$charid';";
$infoQuery = "SELECT alias, age, description, personality, backstory FROM char_basic_info WHERE id='$charid';";
$looksQuery = "SELECT eyes, hair, body, clothing, other FROM char_appearance WHERE id='$charid';";
$result1 = mysqli_query($conn, $charQuery);
$result2 = mysqli_query($conn, $infoQuery);
$result3 = mysqli_query($conn, $looksQuery);
$character = mysqli_fetch_all($result1, MYSQLI_ASSOC)[0];
$basicinfo = mysqli_fetch_all($result2, MYSQLI_ASSOC)[0];
$appearance = mysqli_fetch_all($result3, MYSQLI_ASSOC)[0];
$name = $character['name'];
$personality = $basicinfo['personality'];
$backstory = $basicinfo['backstory'];
$alias = $basicinfo['alias'];
$age = $basicinfo['age'];
$description = $basicinfo['description'];
$eyes = $appearance['eyes'];
$hair = $appearance['hair'];
$body = $appearance['body'];
$clothing = $appearance['clothing'];
$other = $appearance['other'];
//Create new character under current project
$importQuery = "INSERT INTO characters(project_id, name) VALUES('$pid', '$name');";
mysqli_query($conn, $importQuery);
//add basic info and appearance id field to characters table
$insertedCharId = mysqli_insert_id($conn);
$query1 = "UPDATE characters SET basic_info_id='$insertedCharId', appearance_id='$insertedCharId' WHERE id='$insertedCharId';";
mysqli_query($conn, $query1);
//Create basicinfo and appearance entries
$query2 = "INSERT INTO char_basic_info(id) VALUES('$insertedCharId');";
$query3 = "INSERT INTO char_appearance(id) VALUES('$insertedCharId');";
mysqli_query($conn, $query2);
mysqli_query($conn, $query3);
//now add all info
$importInfoQuery = "UPDATE char_basic_info SET alias='$alias', age='$age', description='$description', personality='$personality', backstory='$backstory' WHERE id='$insertedCharId';";
$importLooksQuery = "UPDATE char_appearance SET eyes='$eyes', hair='$hair', body='$body', clothing='$clothing', other='$other' WHERE id='$insertedCharId';";
mysqli_query($conn, $importInfoQuery);
mysqli_query($conn, $importLooksQuery);
Do you see anything wrong with that code?
For me, this works perfectly fine on my computer… but when I upload the files to my university’s student development server, it doesn’t work properly – it imports everything but the basic info stuff. So the name field from the characters table and everything from the char_appearance table properly transfer, but all of the fields in the char_basic_info table except the primary key id field get created as NULL.
Any idea why this might be happening? I’ve been stuck for only a day (usually wait at least three to post here), but this is already due and I’ve got other exams to study for ToT