繁体   English   中英

用php提交html并使用Ajax和jQuery保存到MySQL Db?

[英]Submitting an html with php and saving to MySQL Db using Ajax and jQuery?

这是我第一次创建php表单,因此我一直在阅读和复制大量教程,但是似乎没有真正涵盖从头到尾的所有内容,因此我将不同的人的决定零碎地放在一个文件中,但这不是真的有效。

我知道到目前为止我已经学到了什么,我只是对如何对这个php进行故障排除并找出问题出在哪儿不多。 这是HTML:

<section class="content">
  <form method="POST" action="" enctype="multipart/form-data" name="form">
    <p>Please remember, these results are saved into the database and will be shown to other users. So please do not include any identifying or personal information.</p>
    <label>
      <input type="text" id="object" placeholder="Name an object" required="required" />
      <i class="fa fa-wrench"></i>
    </label>
    <label>
      <input type="text" id="location" placeholder="Name a location" required="required" />
      <i class="fa fa-map-marker"></i>
    </label>
    <label>
      <input type="text" id="person" placeholder="Name a person" required="required" />
      <i class="fa fa-user"></i>
    </label>
    <button type="submit">Submit</button>
  </form>
</section>
<section class="result">
  <div class="return"><?php echo $result; ?></div>
  <h2>See how other people have responded.</h2>
  <div class="previous"></div>
</section>

js:

<script type="text/javascript">
  $(function(){
    $("button").click(function(e){
      e.preventDefault();
      $.post("phptest.php", {$("form").serialize()}, function(res){
        $(".return").html(res);
        $(".content").hide();
        $(".result").show();
      });
    });
  });
</script>

这是PHP:

<?php
  $mysqli = new mysqli('mysite.com', 'myuser', 'mypass', 'mydb');
  if($mysqli->errno) {
    printf("Connection To Database Failed:<br/> %s", $mysqli->error());
    die();
  };
  $query = "INSERT INTO test_table (person, object, location) VALUES ('{$person}', '{$object}', '{$location}')";
  $stmt = $mysqli->stmt_init();
  $stmt->prepare($query);
  $stmt->bind_param('sss', $person, $object, $location);
  $person = $_POST['person'];
  $object = $_POST['object'];
  $location = $_POST['location'];
  $results = $mysqli->query($query);
  $stmt->execute();
  $stmt->close();
  $mysqli->close();
?>
<?php
  $mysqli = new mysqli('mysite.com', 'myuser', 'mypass', 'mydb');
  if($mysqli->errno) {
    printf("Connection To Database Failed:<br/> %s", $mysqli->error());
    die();
  };
  $query = "SELECT person, object, location FROM test_table WHERE person = ?";
  $stmt = $mysqli->stmt_init();
  $stmt->prepare($query);
  $stmt->bind_param('sss', $person, $object, $location);
  $stmt->execute();
  $result = $stmt->get_result();
  while($row = $result->fetch_assoc()) {
    printf('<strong>%s</strong> is a person. <strong>%s</strong> is an object. <strong>%s</strong> is a location.', $row['person'], $row['object'], $row['location']);
  $stmt->close();
  $mysqli->close();
?>

现在,当我理解了教程并理解了教程中所说的内容并试图将其应用于我的用例时,我将其重写了,因此上述错误是我自己的错,而不是我遵循错误建议的情况。

目标:

  1. 提交字段的html表单
  2. 将它们添加到数据库
  3. 将提交的结果返回到<div class="return">
  4. 返回先前提交的前10个结果,其中<div class="previous"> (因此, i<11部分)

首先,您必须在每个输入标签中使用name属性。 例如。:

<input type="text" id="location" name="location" placeholder="Name a location" required="required" />

js代码可能是这样的:

<script type="text/javascript">
  $(function(){
    $("button").click(function(e){
      e.preventDefault();
      $.post("phptest.php", $("form").serialize())
      .done(function(res) {
        $(".return").html(res);
        $(".content").hide();
        $(".result").show();
      });
    });
  });
</script>

这是针对PHP脚本的。 带注释符号的行是您的第一个脚本:

<?php
  $mysqli = new mysqli('mysite.com', 'myuser', 'mypass', 'mydb');
  if($mysqli->errno) {
    printf("Connection To Database Failed:<br/> %s", $mysqli->error());
    die();
  };
  //$query = "INSERT INTO test_table (person, object, location) VALUES ('{$person}', '{$object}', '{$location}')";
  $query = "INSERT INTO test_table (person, object, location) VALUES (?, ?, ?)";
  $stmt = $mysqli->stmt_init();
  $stmt->prepare($query);
  $stmt->bind_param('sss', $person, $object, $location);
  $person = $_POST['person'];
  $object = $_POST['object'];
  $location = $_POST['location'];
  $results = $mysqli->query($query);
  $stmt->execute();
  $stmt->close();
/* No need a new mysqli object, so i block some lines from your script
  $mysqli->close();
?>
<?php
  $mysqli = new mysqli('mysite.com', 'myuser', 'mypass', 'mydb');
  if($mysqli->errno) {
    printf("Connection To Database Failed:<br/> %s", $mysqli->error());
    die();
  };
*/
  $query = "SELECT person, object, location FROM test_table WHERE person = ?";
  $stmt = $mysqli->stmt_init();
  $stmt->prepare($query);
  $stmt->bind_param('s', $person); // The query just need one parameter
  $stmt->execute();
  $result = $stmt->get_result();
  while($row = $result->fetch_assoc()) {
    printf('<strong>%s</strong> is a person. <strong>%s</strong> is an object. <strong>%s</strong> is a location.', $row['person'], $row['object'], $row['location']);
  } // Don't forget to close a statement
  $stmt->close();
  $mysqli->close();
?>

就这样。 我之前已经尝试过,并且一切正常。 希望这可以帮到你。

好,让我们看一下PHP。

对于您的bindparam,请执行以下操作:

$query = "INSERT INTO test_table (person, object, location) VALUES ('?', '?', '?')";

另外,您的while循环未关闭:

while($row = $result->fetch_assoc()) {
    printf('<strong>%s</strong> is a person. <strong>%s</strong> is an object. <strong>%s</strong> is a location.', $row['person'], $row['object'], $row['location']);
  $stmt->close();
}

将}添加到末尾。

另外,您只需要$ stmt-> close();即可。

我是否还建议您最终转到PDO? 相信我,学习它会容易100倍:)

暂无
暂无

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

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