platanas20 platanas20 - 6 months ago 18
MySQL Question

Insert into table cannot be done PHP/mySQL

I want to insert values into a database table but for some reason it cannot be done.I checked the code millions of times but it is impossible to find what goes wrong.If anyone could have a look..

if($_POST['opt1'] == 'add') {
$stmt_insert = $conn->prepare("INSERT INTO customer (card, store, points, comments) VALUES (:card_number, :store, :points, :comments)");

$stmt_insert->bindParam(':card_number', $_SESSION['card']);
$stmt_insert->bindParam(':store', $_SESSION['store']);
$stmt_insert->bindParam(':points', $_POST['points']);
$stmt_insert->bindParam(':comments', $_POST['comments']);
$stmt_insert->execute();

header("Location:index.php");
}
else if($_POST['opt1'] == 'remove') {
$stmt_insert2 = $conn->prepare("INSERT INTO customer (card, store, points, comments) VALUES (:card_number, :store, :points, :comments)");

$stmt_insert2->bindParam(':card_number', $_SESSION['card']);
$stmt_insert2->bindParam(':store', $_SESSION['store']);
$stmt_insert2->bindParam(':points', $_POST['points']);
$stmt_insert2->bindParam(':comments', $_POST['comments']);
$stmt_insert2->execute();

header("Location:index.php");
}


Database connect :

$servername = 'localhost';
$username = "root";
$password = "1234";
$db = "customers";

try {
//Creating connection for mysql
$conn = new PDO("mysql:host=$servername;dbname=$db", $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}


Form Code :

<form name="pontoi" action="update.php" method="post">

<div class="input-group"><label class="col-sm-3 control-label">Σχόλια Συναλλαγής:</label>
<div class="col-md-10"><input type="text" name="comments" class="form-control">

</div>
</div>

</div>


<div class="ibox float-e-margins">
<div class="row">

<br>


<div class="col-sm-4">
<div class="i-checks">
<label class="radio-inline">ΠΡΟΣΘΗΚΗ
<input type="radio" value="add" name="opt1" checked="">
</label></div>
</div>




<div class="col-sm-4">
<div class="i-checks">
<label class="radio-inline">ΕΞΑΡΓΥΡΩΣΗ
<input type="radio" value="remove" name="opt1">
</label></div>

</div>

<div class="col-sm-4">
<div class="input-group">
<input type="text" class="form-control" maxlength="3" name="points"> <span class="input-group-btn">
<button type="submit" onclick="return checkInp()" class="btn btn-primary">Go
</button> </span></div>

</div>
</form>

Answer

Give this a try and see if you receive an error message at all.

if(!session_id()){
    session_start();
}

if(isset($_POST['opt1']) && ($_POST['opt1'] == 'add' || $_POST['opt1'] == 'remove')){
    try{
        if(!$stmt_insert = $conn->prepare("INSERT INTO customer (card, store, points, comments) VALUES (:card_number, :store, :points, :comments)")){
            print_r($conn->errorInfo());
            die('Invalid Query');
        }

        $stmt_insert->bindParam(':card_number', $_SESSION['card']);
        $stmt_insert->bindParam(':store', $_SESSION['store']);
        $stmt_insert->bindParam(':points', $_POST['points']);
        $stmt_insert->bindParam(':comments', $_POST['comments']);
        if (!$stmt_insert->execute()) {
            print_r($sth->errorInfo());
            exit;
        }
    } catch(\Exception $e) {
       echo $e->getMessage();
       exit;
    }
    header("Location:index.php");
    exit;
} else {
   die('Invalid Option Specified ' . $_POST['opt1']);
}
Comments