Here i am writing a script which will send three different values to a database using a simple html form.But whenever i submit the form no values are inserted and i get the following error :
what might be the reason for this error.How can i fix this problem.
$server='localhost';
$user='root';
$password='';
$dbname='fruits';
if(isset($_POST['name']) &&isset($_POST['colour']) && isset($_POST['calories'])){
if(!empty($_POST['name']) && !empty($_POST['colour']) && !empty($_POST['calories'])){
$name=$_POST['name'];
$colour=$_POST['colour'];
$calories=$_POST['calories'];
try{
$conn=new PDO('mysql:host=$server,dbname=$dbname,$user,$password');
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt=$conn->prepare("INSERT INTO favfruit(name,colour,calories)
VALUES(:name,:colour,:calories)");
$stmt->execute(array(':name'=>$name,':colour'=>$colour,':calories'=>$calories));
echo 'it was successfully entered to database';
}catch(PDOException $e){
echo 'error : '.$e->getMessage();
}
}
}
HTML form :
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
fruit name :<input type='text' placeholder='fruit name' name='name'></br>
colur :<input type='text' placeholder='fruit colour' name='colour'></br>
calories :<input type='text' placeholder='calories' name='calories'></br>
<input type='submit' value='SUBMIT FORM'>
</form>