I'm trying to randomly insert a name if the variable is empty but I keep getting the error message
Array to string conversion
my attempt so far
<?php
require('classes/Data.php');
$db = new Database;
$post = json_decode( file_get_contents("php://input"),true );
$totalScore = $name = $totalScore = $correct = $wrong = "";
$totalScore = $post['totalScore'];
$correct = $post['correct'];
$wrong = $post['wrong'];
$name = $post['person'];
if(empty($name)){
$name = array(1 => "Shrewd Farseer", 2 => "Verity Farseer", 3 => "Dutiful Farseer", 4 => "Chade Fallstar", 5 => "Chivalry Farseer", 6 => "Regal Farseer",
7 => "Shrewd Farseer", 8 => "FitzChivalry Farseer", 9 => "FitzVigilant", 10 => "Shine Fallstar", 11 => "Prosper Farseer", 12 => "Integrity Farseer",
13 => "August", 14 => "Merry Farseer", 15 => "Nettle", 16 => "Bee"
);
shuffle($name);
implode($name);
}
$scores = new Data();
$scores->addData($totalScore, $correct, $wrong, $name);
$success = ['response' => 'true'];
header('Content-Type: application/json');
echo json_encode($success);
class
<?php
require('classes/Database.php');
class Data{
public function __construct()
{
$db = new Database;
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
$this->db = $db;
}
function addData($totalScore, $correct, $wrong, $name){
$this->db->beginTransaction();
$insert = $this->db->prepare("BEGIN;
INSERT INTO gamescore (totalScore, correct, wrong) VALUES (:totalScore, :correct, :wrong);
INSERT INTO usernames (person, scoreId) VALUES (:name, LAST_INSERT_ID());
COMMIT;
");
$insert->execute(array(
':totalScore' => $totalScore,
':correct' => $correct,
':wrong' => $wrong,
':name' => $name
));
$this->db->commit();
}
}