繁体   English   中英

无法插入表中PHP / mySQL

[英]Insert into table cannot be done PHP/mySQL

我想将值插入数据库表中,但由于某种原因无法完成。我检查了数百万次代码,但不可能找出问题所在。如果有人可以看一下。

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 )

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM