Add a new reference

Go back to references.

[insert_php]

global $schemaKey, $schemaFKeys;

$title = $year = $freelink = $otherlink = $ttlErr = $authErr = $topicErr = $linkErr1 = $linkErr2 = “”;

$authors = $topics = array(“”,””);

$nofreelink = false;

$ready = false;

include(“/home/appliedct/appliedcategorytheory.org/wp-content/themes/twentyfifteen_mod/utils.php”);

include(“/home/appliedct/appliedcategorytheory.org/wp-content/themes/twentyfifteen_mod/utils.js”);

[/insert_php]

[insert_php]

// This section processes the inputs of the form

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$ready = true;

if (empty($_POST["ztitle"])) {
$ttlErr = "* Title is required.";
$ready = false;
} else {
$title = capitalize(testInput($_POST["ztitle"]));
$ttlErr = "";
}

if ($_POST["authors"][0]=="") {
$authErr = "* At least one author is required.";
$ready = false;
} else {
$authErr = "";
for ($i=0; $i Add a new paper to the database:
(* = required field)

[insert_php]
for ($i=1; $i\n";
echo "

\n";
echo "

\n";
echo "

\n";
}
[/insert_php]

[insert_php]
for ($i=1; $i\n";
echo "

\n";
echo "

\n";
echo "

\n";
}
[/insert_php]

Paper:
Title*:
[insert_php] echo $ttlErr;[/insert_php]
Year:
Author(s):
Author 1*:
[insert_php] echo $authErr;[/insert_php]
Author ";
echo $i + 1;
echo ":
Topic(s):
Topic 1*:
[insert_php] echo $topicErr;[/insert_php]
Topic ";
echo $i + 1;
echo ":
Access:
Free Access*:
No free access available.
[insert_php] echo $linkErr1;[/insert_php]
Other Access:
[insert_php] echo $linkErr2;[/insert_php]



[insert_php]

// This section adds the inputs to the database

if ($_SERVER["REQUEST_METHOD"] == "POST" and $ready) {

global $schemaKey, $schemaFKeys;

$servername = "mysql.appliedcategorytheory.org";
$dbname = "appliedcategorytheory_or";
$username = "appliedcategoryt";
$password = "Z65iDPVZ";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$paperData = array('title' => $title);

if ($freelink) {
$paperData['free_link'] = $freelink;
}
if ($year != "") {
$paperData['year'] = $year;
}
if ($otherlink != "") {
$paperData['other_access'] = $otherlink;
}

$paperID = addRow($conn,'Paper',$paperData);

$tables = array('Person','Area');
$rels = array('Author','Topic','Interest');

$newData = array('Person' => array_filter($authors), 'Area' => array_filter($topics));
$oldData = array();

foreach ($tables as $table) {
$otherKey = $schemaKey[$table];

$entries = array();
foreach ($newData[$table] as $entry) {
$entries[] = "$otherKey='$entry'";
}

$tableQuery = "SELECT $otherKey FROM $table WHERE (" . join(" OR ",$entries) . ")";
$tableRes = $conn->query($tableQuery);

$oldData[$table] = array();
while ($row = $tableRes->fetch_assoc()) {
$oldData[$table][] = $row[$otherKey];
}
$diff = array_diff($newData[$table],$oldData[$table]);

if (count($diff)>0) {
$insertQuery = "INSERT INTO $table ($otherKey) VALUES ('" . join("'), ('", $diff) . "')";
if ($conn->query($insertQuery) === TRUE) {
echo "$table records inserted successfully.
";
} else {
echo "Error: " . $insertQuery . "
" . $conn->error;
}
}
}

$relQuery = buildInsertCheck('Paper',$paperID, array('Person' => $authors,'Area' => $topics), array('Author','Topic','Interest'));

$relRes = $conn->query($relQuery);
$newRels = array();
foreach ($rels as $rel) {
$newRels[$rel] = array();
}

while ($row = $relRes->fetch_assoc()) {
foreach ($rels as $rel) {
$relTables = array_keys($schemaFKeys[$rel]);
if (empty($row[$rel . "ID"])) {
$newRels[$rel][] = "('" . $row[$relTables[0] . "ID"] . "', '" . $row[$relTables[1] . "ID"] . "')";
}
}
}

foreach ($rels as $rel) {
$fKeys = array_values($schemaFKeys[$rel]);

if (count($newRels[$rel])>0) {
$insertQuery = "INSERT INTO $rel ($fKeys[0],$fKeys[1]) VALUES " . join(", ",array_unique($newRels[$rel]));
if ($conn->query($insertQuery) === TRUE) {
echo "$rel records inserted successfully.
";
} else {
echo "Error: " . $insertQuery . "
" . $conn->error . "
";
}
}
}

$displayData = array('year' => $year, 'free_link' => $freelink, 'other_access' => $otherlink);
$displayData['authors'] = array_filter($authors);
$displayData['topics'] = array_filter($topics);

echo "\n";

}

[/insert_php]