For school I'm working on creating a website that sells animals. We're required to have an adminpage that enables owners to add, edit and delete animals on the site. We've made it possible to add animals, but I can't seem to get editing the existing ones working. I'm also not really experienced in PHP. I've added a couple of checks to see if the statement is being executed, and once i fill in the form and click submit it tells me 'donedone', which should mean it worked - but the table remains the same. Could someone shed some light on what might be the problem I'm facing? This is the first time I've asked something via SO.
edit1: Instead of the "where animal_id = ':animal_id'" i tried it with only animal no. 33, and instead of the values i entered in my form, it replaces all the values with ':class' and ':price' etc. So something must be wrong in binding the form values to the 'insert into' stmt.
This is the database connection
<?php
$server = 'localhost';
username = 'username';
$password = '*********';
$database = 'databasename';
$conn = new PDO("mysql:host=$server;dbname=$database;", $username, $password);
?>
and this is the edit_animal file:
<?php
session_start();
if( isset($_SESSION['user']) ){
header("Location: /");
}
ini_set('display_errors', '0');
error_reporting(E_ALL | E_STRICT);
require 'database.php';
$message = '';
if(!empty($_POST['animal_name']) && !empty($_POST['official_name']) && !empty($_POST['average_lifespan']) && !empty($_POST['length']) && !empty($_POST['class']) && !empty($_POST['housing']) && !empty($_POST['animal_price']) && !empty($_POST['diet']) && !empty($_POST['description'])):
$sql = "UPDATE animal_test SET animal_name =':animal_name', animal_price = ':animal_price', official_name =':official_name', average_lifespan = ':average_lifespan' , length =':length' , class =':class', housing = ':housing', description=':description', diet =':diet' WHERE animal_id =':animal_id'";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':animal_id', $_POST['animal_id']);
$stmt->bindParam(':animal_name', $_POST['animal_name']);
$stmt->bindParam(':official_name', $_POST['official_name']);
$stmt->bindParam(':average_lifespan', $_POST['average_lifespan']);
$stmt->bindParam(':length', $_POST['length']);
$stmt->bindParam(':class', $_POST['class']);
$stmt->bindParam(':housing', $_POST['housing']);
$stmt->bindParam(':animal_price', $_POST['animal_price']);
$stmt->bindParam(':description', $_POST['description']);
$stmt->bindParam(':diet', $_POST['diet']);
$result = $stmt->execute();
if (!$stmt) {
echo "Jammer";
}
if( $stmt){
echo "done";
}
if ($result) {
echo "done";
}
else{
$message = 'Sorry there must have been an issue with editing your animale';
}
endif;
?>
<!DOCTYPE html>
<html>
<head>
<title>Admin - Exotic Animal Shop</title>
<link rel="stylesheet" type="text/css" href="admin_page.css">
</head>
<body>
<h2>Add a product</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"method="POST">
<div class=add_product_div>
<div id=animal_name_div>
<p>Animal name</p>
<input id=animal_name type="text" placeholder="Animal name" name="animal_name">
</div>
<div id=animal_id_div>
<p>Animal ID</p>
<input id=animal_id type="number" placeholder="Animal ID" name="animal_id" min="0">
</div>
<div id=price_div>
<p>Price</p>
<input id=animal_price type="number" placeholder="Price" name="animal_price" min="0">
</div>
<div id=official_name_div>
<p>Official name</p>
<input id=official_name type="text" placeholder="Official name" name="official_name">
</div>
<div id=current_age_div>
<p>Average lifespan</p>
<input id=average_lifespan type="text" placeholder="Average lifespan" name="average_lifespan">
</div>
</div>
<div class=add_product_div>
<div id=length_div>
<p>Max length</p>
<input id=length type="text" placeholder="Max length" name="length">
</div>
<div id=class_div>
<p>Class</p>
<input id=class type="text" placeholder="Class" name="class">
</div>
<div id=housing_div>
<p>Housing</p>
<input id=housing type="text" placeholder="Housing" name="housing">
</div>
<div id=animal_diet>
<p>Diet</p>
<input id=diet type="text" placeholder="Diet" name="diet">
</div>
</div>
<div class=add_product_div>
<p>Description</p>
<textarea placeholder="Description" id="description" name="description" rows="20" cols="40"></textarea>
</div>
<div id=submit_div>
<input id=submit type="submit" value="Submit">
</div>
</form>
<form action="upload.php" method="post" enctype="multipart/form-data">
<p>Select image to upload</p>
<input type="file" name="userfile" id="userfile">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>