【问题标题】:Insert into table cannot be done PHP/mySQL无法插入表格 PHP/mySQL
【发布时间】:2016-05-26 12:46:49
【问题描述】:

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

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 )

【问题讨论】:

  • 我们需要更多信息。显示您连接到数据库的位置!
  • 请问发生了什么错误?请提供更多细节
  • 是用ini_set('error_reporting', -1);ini_set('display_errors', 'on');或在stmt_insert2-&gt;execute();之后调用print_r($conn-&gt;errorInfo());显示的任何东西可能是isset($_POST['opt1']);为假或不等于addremove跨度>
  • 可能是代码不能同时进入这两个条件!!也发布您的表单代码!
  • 是否存在您忽略插入的字段,但是否有 NOT NULL 字段,例如 customer_name 或 customer_id?还是您实际上是在尝试将新值插入到数据库中已经存在的客户行中?想知道为什么你有 $_POST['opt1'] == 'remove' 的插入。

标签: php mysql pdo insert


【解决方案1】:

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

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

更新

尝试更改表单单选按钮。

&lt;input type="radio" value="add" name="opt1" checked="checked"&gt;

还可以尝试将提交按钮更改为输入元素。

&lt;input name="test" type="submit" onclick="return checkInp()" class="btn btn-primary" value="go"&gt;

【讨论】:

  • 出现的信息是:Invalid Option Specified
  • 这意味着你的$_POST['opt1'] 是空的。由于它被设置为显示$_POST['opt1']的值。所以它永远不会执行 SQL 语句
  • 通过调用脚本顶部的print_r($_POST); 来验证$_POST 数据。
  • Array ( [comments] =&gt; Antiliaka [points] =&gt; 50 ) 这意味着单选按钮出了问题..我不明白真正出了什么问题。它们在表单中。所以我还需要检查什么以确保我通过 $_POST 全局变量传递值??
  • 可能是 javascript 有问题,checkInp() javascript 函数中有什么问题。它可能正在删除 opt1 值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
相关资源
最近更新 更多