This is an old revision of the document!
| alias | identity | hero_villain | first_appeared | power |
|---|---|---|---|---|
| Thor | Thor Odinson | Hero | 1962 | Magic |
| Red Skull | Johan Schmidt | Villain | 1941 | Biological |
| Bucky Barnes | Winter Soldier | Villain | 1941 | Tech |
| Clint Barton | Hawkeye | Hero | 1969 | Skill |
| Odin | Odin | Hero | 1962 | Magic |
| Nick Fury | Nick Fury | Hero | 1963 | Skill |
| Phil Coulson | Agent Coulson | Hero | 2008 | Skill |
| Peter Quill | Starlord | Hero | 1976 | Tech |
| Gamora | Gamora | Hero | 1975 | Skill |
| Pepper Potts | Rescue | Hero | 1963 | Tech |
INSERT INTO `heroes` (`alias`, `identity`, `hero_villain`, `first_appeared`, `power`) VALUES ('Bucky Barnes', 'Winter Soldier', 'Villain', '1941', 'Tech');
switch ($variable) {
case 'value':
# code...
break;
default:
# code...
break;
}
switch ($edad) {
case 1:
case 17:
case 43:
# code...
break;
case 22:
case 39:
# code...
break;
}
$_POST['submit']. switch ($_POST['submit']) {
case 'Agregar Héroe':
break;
case 'Agregar apariencia':
break;
default:
break;
}
case 'Agregar Héroe':. $query = "";
$args = array();
$rslt = $pdo->prepare($query);
$rslt->execute($args)
$query = "INSERT INTO `heroes` (`alias`, `identity`, `hero_villain`, `first_appeared`, `power`) VALUES (?,?,?,?,?)";
$args = array($_POST['alias'], $_POST['identity'], $_POST['alias'], $_POST['side'], $_POST['year'], $_POST['power']);
$rslt->execute($args) a lo siguiente. if ($rslt->execute($args)) {
} else {
}
if ($rslt->execute($args)) {
$message = "{$_POST['name']} was inserted successfully.";
} else {
$message = "There was a problem inserting {$_POST['name']}.";
}
INSERT INTO appearances (character_name, movie) VALUES ('Thor Odinson', 'Thor: Dark World'), ('Thor Odinson', 'Avengers: Age of Ultron')
foreach($_POST['movie'] as $movie) {
}
array_push($args, $_POST['hero'], $movie);
foreach. $args = array();
foreach($_POST['movie'] as $movie) {
array_push($args, $_POST['hero'], $movie);
}
// check if it exists already
$mquery = "SELECT * FROM appearances WHERE character_name=? AND movie=?";
$margs = array($_POST['hero'], $movie);
$rslt = $pdo->prepare($mquery);
$rslt->execute($margs);
if (!$row = $rslt->fetch()) {
array_push($args, $_POST['hero'], $movie);
}
if (!$row = $rslt->fetch())), la agregamos a la lista de argumentos.$_POST['new_movie'] su lugar. if ($movie == 'new') {
array_push($args, $_POST['hero'], $_POST['new_movie']);
} else {
// check if it exists already
$mquery = "SELECT * FROM appearances WHERE character_name=? AND movie=?";
$margs = array($_POST['hero'], $movie);
$rslt = $pdo->prepare($mquery);
if (!$rslt->execute($margs)) {
array_push($args, $_POST['hero'], $movie);
}
}
(?,?) al final de la consulta.$args matriz usando count y luego reducirlo a la mitad.count es como .length en Javascript, contando el número de elementos en una matriz.foreach bucle.$movies = count($args)/2;
(?,?) al final de la consulta, pero deben estar separadas por un , . $values = str_repeat('(?,?), ', $movies-1) . '(?,?)';
str_repeat para repetir una cadena establecida varias veces. Si repetimos '(?,?),' el número correcto de veces, tendríamos un colgante , al final que provocaría un error. En cambio, agregamos uno menos que la cantidad requerida ( count($movies) -1), luego agregamos (?,?) al final. $query = "INSERT INTO heroes ('alias', 'movie') VALUES $values";
break. error_log("MURRAY: $query\n" . print_r($args, 1);
$rslt = $pdo->prepare($query);
if ($rslt->execute($args)) {
$message = "$movies appearances were inserted successfully.";
} else {
$message = "There was a problem inserting $movies appearances.";
}
if ($movies > 0) {
$values = str_repeat('(?,?), ', $movies-1) . '(?,?)';
$query = "INSERT INTO appearances (alias, movie) VALUES $values";
error_log("MURRAY: $query " . print_r($args, 1));
$rslt = $pdo->prepare($query);
if ($rslt->execute($args)) {
$message = "$movies appearances were inserted successfully.";
} else {
$message = "There was a problem inserting $movies appearances.";
}
} else {
$checked = count($_POST['movie']);
$message = "All $checked appearances are already in the database.";
}