In order to avoid SQL injections and other vulnerabilities that come with the integration of variables straight in the SQL statement (not a huge problem in this case, but it's good practice), you can use PDO with prepared statements. The following code loops through each of the steps for each MySQL query and updates the robot name:
$SQLInsert = "INSERT INTO robots (origin, name, color, position, controller) values (:origin, :name, :color, :position, :controller)";
$SQLSelect = "SELECT origin, name, color, position, controller FROM robots WHERE controller = 'remote'";
foreach ($pdo->query($SQLSelect) as $row){
$params = [
':origin' => $row['origin'],
':name' => generateRobotName(),
':color' => $row['color'],
':position' => $row['position'],
':controller' => $row['controller']
];
$SQLInsert = $pdo->prepare($SQLInsert);
$SQLInsert->execute($params);
}
The above code does the SQL Select query, and for each row it finds, $row, It inserts the old data using the second defined query while still adding the robot name. DO keep in mind $pdo is defined as the PDO object, and you will need PDO support in your PHP install. Working example can be found here. Cheers!
generateRobotNameon MySQl sideprepare(INSERT INTO robots (origin, name, color, position, controller) SELECT origin, ?, color, position, controller FROM robots WHERE controller = 'remote');should wok then bind$rname.