PHP/MySQL:将用户输入保存到数据库


PHP/MySQL: Saving user input to Database

我有一个页面,它会显示一个地图,用户可以在其中添加标记,并显示一些输入框,供他们添加信息(名称、描述等)。然后,这些信息将被发送到数据库,在那里可以调用并在必要时显示。

单击SAVE(保存)时,框应关闭并显示一条消息("完成")

将其发送到数据库的JS函数:

    //when user clicks on the "submit" button
        form.submit({point: point}, function (event) {
        //prevent the default form behavior (which would refresh the page)
        event.preventDefault();
        //put all form elements in a "data" object
        var data = {
            name: $("input[name=name]", this).val(),
            description: $("textarea[name=description]", this).val(),
            category: $("select[name=category]",this).val(),
            lat: event.data.point.overlay.getPosition().lat(),
            lon: event.data.point.overlay.getPosition().lng()
        };
        trace(data)
        //send the results to the PHP script that adds the point to the database
        $.post("adddata.php", data, tidy_maps.saveStopResponse, "json");
        //Erase the form and replace with new message
        infowindow.setContent('done')
        return false;

"adddata.php":

<?php
ini_set("display_errors",1);
//database access info
$db = new mysqli("localhost", "stephen", "pass1", "gmaps1");
$statement = $db->stmt_init();
//database insert statement
$statement->prepare("INSERT INTO tidy_maps_test (lat, lon, name, description,   category) VALUES (?, ?, ?, ?, ?)");
//grab values from the url and add to database
$statement->bind_param("ddsss", &$_POST['lat'], &$_POST['lon'],     &$_POST['name'], &$_POST['description'], &$_POST['category']);
$status = $statement->execute();
//create a status message
if ($status)
{
    $message = array("message" => $_POST['name'] . " has been added!");
}
else
{
    $message = array("error" => $db->error);
}
$statement->close();
echo json_encode($message);
?>

单击SAVE按钮时,不会发生任何事情,表单将保持打开状态并输入文本,不会保存到DB。我在浏览器上运行了"adddata.php",看看它是否连接到数据库,并且是.

我一直在使用本教程作为指南:http://gis.yohman.com/up206b/tutorials/9-2/


我试过把按钮改成输入,但还是没有成功。我的HTML表单是:

<form class="form-horizontal save-form" style="display: none">
        <h1>Add me!</h1>
        <fieldset>
            <div class="control-group">
                ....
                Form Contents Here
                ...
                </div>
            </div>
            <div class="form-actions">                    
                <input name="Save" type="submit" class="btn btn-primary" value="Save">
            </div>
        </fieldset>
    </form>

您的代码(来自教程)使用函数"trace"

trace(data);

但是该函数没有被定义。在教程演示中,此函数定义为:

function trace(message)
{
  if (typeof console != 'undefined')
  {
      console.log(message);
  }
}

尝试在脚本中添加此代码。