I have a php file called edit_atividade.php, which runs when I click a Edit command on a table containing the Atividades table. Here is the code that it is not working:
<?php
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($cdatividade, $atividade, $error) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Atividade</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '') {
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="cdatividade" value="<?php echo $cdatividade; ?>"/>
<div>
<p><strong>CdAtividade:</strong> <?php echo $cdatividade; ?></p>
<strong>Atividade: *</strong> <input type="text" name="atividade" value="<?php echo $atividade; ?>"/><br/>
<p>* Required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include "session.php";
include "connectSQL.php";
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit'])) {
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['cdatividade'])) {
// get form data, making sure it is valid
$cdatividade = $_POST['cdatividade'];
$atividade = $_POST['atividade'];
// check that firstname/lastname fields are both filled in
if ($atividade == '') {
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($cdatividade, $atividade, $error);
} else {
// save the data to the database
$db->query("UPDATE Atividades SET Atividade='$atividade' WHERE CdAtividade='$cdatividade'");
// once saved, redirect back to the view page
header("Location: atividades.php");
}
} else {
// if the 'id' isn't valid, display an error
echo 'Error!';
}
} else {
// if the form hasn't been submitted, get the data from the db and display the form
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['cdatividade']) && is_numeric($_GET['cdatividade']) && $_GET['cdatividade'] > 0) {
// query db
$cdatividade = $_GET['cdatividade'];
$result = $db->query("SELECT * FROM Atividades WHERE cdatividade=$cdatividade")
//$row = mysql_fetch_array($result);
$row = $result->fetch();
// check that the 'id' matches up with a row in the databse
if($row) {
// get data from db
$atividade = $row['atividade'];
// show form
renderForm($cdatividade, $atividade, '');
} else {
// if no match, display result
echo "No results!";
}
} else {
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
echo 'Error!';
}
}
?>
I took codes from this page as base to my code: http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/
Is there something missing in the code to not be working? I appreciate any help.