0

I have created an insert form. I'm doing an insertion operation into MySQL using prepare statement but it's not working. I don't understand what's wrong. Please help me to solve this issue. Is this what I did correct?

insert.php

<?php

include('dbconn.php');
session_start();
$_SESSION['example']='Session Created';
    $srn = $_POST['srn'];
    $client = $_POST['client']; // required
    $category = $_POST['category'];
    $sd     = $_POST['sd']; // required
    $fd     = $_POST['fd'];

    $host = "localhost";
    $user = "root";
    $pwd  = "root";
    $db   = "eservice";
    $pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
    $sql = "Insert into main(client,category,sd,fd)             values(:client,:category,:sd,:fd)";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':client',$_POST['client'],PDO::PARAM_STR);
    $stmt->bindParam(':category',$_POST['category'],PDO::PARAM_STR);
    $stmt->bindParam(':sd',$_POST['sd'],PDO::PARAM_STR);
    $stmt->bindParam(':fd',$_POST['fd'],PDO::PARAM_STR);
    $stmt->execute();

?>

dbconn.php

<?php
$host = "localhost";
$user = "root";
$pwd  = "root";
$db   = "eservice";

$mysqli = new mysqli($host,$user,$pwd,$db);
/* ESTABLISH CONNECTION */
if (mysqli_connect_errno()) {
   echo "Failed to connect to mysql : " . mysqli_connect_error();
    exit();
}
?>
Kiran
  • 31
  • 7

1 Answers1

0

Its always good to put up the errors you are having.
You are using two different database connection types pdo, and mysqli. You only need one.

I stripped your code down to the minimum.

<?php

$host = "localhost";
$user = "root";
$pwd  = "root";
$db   = "eservice";
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pwd);

//$srn = $_POST['srn'];
$client = $_POST['client']; // required
$category = $_POST['category'];
$sd     = $_POST['sd']; // required
$fd     = $_POST['fd'];

// make sure client and sd are set here

$stmt = $pdo->prepare("
    INSERT INTO
        main 
            (client,category,sd,fd)             
        VALUES
            (:client,:category,:sd,:fd)
");
$success = $stmt->execute([
    'client' => $client,
    'category' => $category,
    'sd' => $sd,
    'fd' => $fd
]);
Gauthier
  • 1,251
  • 10
  • 25