PHP - Mysql prepare不会执行

时间:2017-11-11 18:56:44

标签: javascript php mysql

我正在尝试使用名为editable grid(http://www.editablegrid.net/en/)的工具创建mysql条目。我只是在代码上添加了2个文本域..但是看起来像mysql准备代码不喜欢我正在做的事情。

这就是php代码:

    require_once('config.php');         

// Database connection                                   
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
$mysqli->real_connect($config['db_host'],$config['db_user'],$config['db_password'],$config['db_name']); 

// Get all parameter provided by the javascript
$name = $mysqli->real_escape_string(strip_tags($_POST['name']));
$firstname = $mysqli->real_escape_string(strip_tags($_POST['firstname']));
$uid = $mysqli->real_escape_string(strip_tags($_POST['uid']));
$show = $mysqli->real_escape_string(strip_tags($_POST['show']));
$tablename = $mysqli->real_escape_string(strip_tags($_POST['tablename']));

$return=false;
if ( $stmt = $mysqli->prepare("INSERT INTO ".$tablename."  (name, firstname, uid, show) VALUES (  ?, ?, ?, ? )")) {

    $stmt->bind_param("ssss", $name, $firstname, $uid, $show);
    $return = $stmt->execute();
    $stmt->close();
}             
$mysqli->close();        

echo $return ? "ok" : "error";

这里是html代码:

 <div id="addform" style="height:210px;">

            <div class="row">
                <input type="text" id="name" name="name" placeholder="name" />
            </div>

             <div class="row">
                <input type="text" id="firstname" name="firstname" placeholder="firstname" />
            </div>
             <div class="row">
                 <input type="text" id="uid" name="uid" placeholder="uid" />
            </div>

            <div class="row">
                <input type="text" id="show" name="show" placeholder="show" /> 
                </div>

            <div class="row tright">
              <a id="addbutton" class="button green" ><i class="fa fa-save"></i> Apply</a>
              <a id="cancelbutton" class="button delete">Cancel</a>
            </div>
        </div>

他们都用javascript连接在一起,这很有效。我刚刚添加了2个文本字段。我在PHP代码中有错误。在整个准备声明中,我是否允许使用4个值?

1 个答案:

答案 0 :(得分:0)

代码似乎正确但未定义变量tablename,因此将更多文本字段添加到html

function loadDetails() {

    $.ajax({
        type: 'GET',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        cache: false,
        url: _WebRoot() + '/Charts/GetDetails',
        success: function (response, textStatus, jqXHR) {
            prepareData(response.Records, 'Text Title');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('Error - ' + errorThrown);
        }
    });
}

或者在php中设置表名

<div class="row">
<input type="text" id="tablename" name="tablename" placeholder="input table name" />
</div>