插入表无法完成PHP / mySQL

时间:2016-05-26 12:46:49

标签: php mysql pdo insert

我想将值插入数据库表但由于某种原因无法完成。我检查了数百万次代码但是找不到问题是不可能的。如果有人可以看看..

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");     
}

数据库连接:

$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 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>

表格发布数据

Array ( [comments] => Antiliaka [points] => 50 )

1 个答案:

答案 0 :(得分:0)

尝试一下,看看是否收到错误信息。

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']);
}

<强>更新

尝试更改表单单选按钮。

<input type="radio" value="add" name="opt1" checked="checked">

另请尝试将提交按钮更改为输入元素。

<input name="test" type="submit" onclick="return checkInp()" class="btn btn-primary" value="go">