Hello everyone this is my first post. I’ve just bought the book develop app with PHP and MYSQL and i don’t understand why my exercise is not working as expected.
I have an index.html with inside a controller.
Inside index I can see a list of “jokes”, add a joke inside a database or delete it.Here the code of the index.php file
//Se nell URL dell'header è presente la variabile addjoke, carica direttamente il form per aggiungere un joke
if (isset($_GET['addjoke']))
{
include 'form.html.php';
exit();
}
//Stabilisce la connessione al database e salva l'oggetto in una variabile pdo
try
{
$pdo = new PDO('mysql:host=localhost;dbname=joketable', 'root', 'root');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$error = 'Unable to connect to the database server.';
include 'error.html.php';
exit();
}
//Se la pagina riceve una variabile POST joketext, inserisce la nuova variabile nel database SQL
if (isset($_POST['joketext']))
{
try
{
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext',$_POST['joketext']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Errore in aggiunta di joke inviato: ' . $e->getMessage();
include 'error.html.php';
exit();
}
//Alla fine del processo di salvataggio, ricarica la pagine corrente per mostrare l'ultima aggiunta
header('Location: .');
exit();
}
//se l'utente clicca su elimina nella riga del joke
if (isset($_GET['deletejoke'])) {
try{
$sql = 'DELETE FROM joke WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e){
$error = "Errore nell'eliminazione de joke: " . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
//Seleziona i joketext e salvali in una variabile result
try
{
$sql = 'SELECT id, joketext FROM joke';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Errore in recupero joke: ' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $result->fetch())
{
//$jokes[] = $row['joketext'];
$jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
}
include 'jokes.html.php';
Second file is the jokes.html.php that display all my jokes is used to add joke inside my databases. here the code:
<body>
<form action="?" method="post"> <!-- il ? dovrebbe rimuovere dalla URL tutti i campi e fare il redirect alla index, ma non funziona -->
<div>
<label for="joketext">Digita qui il tuo joke:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40"></textarea>
</div>
<div><input type="submit" name="Aggiungi"></div>
</form>
</body>
and finally the file form.html.php where is possible add joke to the database
<body>
<form action="?" method="post"> <!-- il ? dovrebbe rimuovere dalla URL tutti i campi e fare il redirect alla index, ma non funziona -->
<div>
<label for="joketext">Digita qui il tuo joke:</label>
<textarea id="joketext" name="joketext" rows="3" cols="40"></textarea>
</div>
<div><input type="submit" name="Aggiungi"></div>
</form>
</body>
My problem is about the redirects after I add a joke or I delete it.
If I add a joke I’m redirected to this link http://localhost:8888/provePhp/jokes/? and the page is blank.
if I delete a joke instead I’m redirected to this link
http://localhost:8888/provePhp/jokes/?deletejoke and also here the page is blank.
It seems like the header function header(‘Location: .’) does not work or I made an error somewhere. If I add a joke I shoul be redirected to the index.php where all the jokes are displayed and also if I delete a joke the redirect shoul be the same. Anyone who can help me?
The exercise folder that I downloaded from the book it’s exactley the same but that one is working and not my example.