【问题标题】:Not able to store data in mysql using ajax无法使用ajax将数据存储在mysql中
【发布时间】:2019-05-18 07:08:49
【问题描述】:

我正在尝试使用 PHP 和 Ajax 将表单详细信息保存在数据库中。它包括文本字段和图像。我的数据没有保存在 mysql 中。如果我试图在没有 ajax 的情况下保存数据,它会完美运行。 所以请在我的代码中帮助我。

这就是我在数据库中保存我的详细信息的方式:


// Create database connection
$db = mysqli_connect("localhost:8889","root","123","temp") or die("could not connect to server");

// If upload button is clicked ...

  $finame=$_POST['firstname'];
  $laname=$_POST['lastname'];
  $image_text = $_POST['image_text'];
  $template = $_POST['template'];
  $address = $_POST['address'];
  $email = $_POST['email'];
  $experience = $_POST['experience'];

   // Get image name
   $image = $_FILES['image']['name'];
   $directory = date("Y").'/'.date("m").'/'.date("d").'/';
  //If the directory doesn't already exists.
  if(!is_dir($directory)){
      //Create our directory.
      mkdir($directory, 755, true);
  }
   // image file directory
   $target = $directory.basename($image);

   $sql = "UPDATE person SET fname='$finame', sname='$laname', image='$image', about='$image_text', template='$template', address='$address', email='$email', experience='$experience'  where id=1";
   // execute query
   mysqli_query($db, $sql);
 ?>

这是我在 mysql 中的表:

CREATE TABLE `person` (
  `fname` varchar(30) DEFAULT NULL,
  `sname` varchar(30) DEFAULT NULL,
  `image` mediumblob NOT NULL,
  `about` varchar(10000) NOT NULL,
  `id` int(10) NOT NULL,
  `template` int(10) NOT NULL,
  `address` varchar(300) NOT NULL,
  `email` varchar(100) NOT NULL,
  `experience` varchar(1000) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

这是我的表单和脚本:

<form enctype="multipart/form-data" id="frmBox" method="post">
<input type="text" name="firstname" id="fname" placeholder="First Name">
<input type="text" name="lastname" id="lname" placeholder="Last Name">
<input type="text" name="address" placeholder="Your address..." id="address">
<input type="file" name="image" id="image">
<textarea id="text" cols="30" rows="4" name="image_text" placeholder="Say something"></textarea></div>
<input type="radio" value="1" name="template" style="margin-left: 15px;">&nbsp; Template 1 &nbsp; &nbsp; 
<input type="radio" value="2" name="template">&nbsp; Template 2
<input type="text" name="email" placeholder="Your Email" id="email">
<textarea id="experience" cols="30" rows="4" name="experience" placeholder="Say something..."></textarea>
<h3 id="success"></h3>
<input type="submit" name="upload" class="sub-btn" value="submit">
</form>

<script type="text/javascript">
      $('document').ready(function(){
        $('#frmBox').submit(function(){
          $.ajax({
            type: 'POST',
            url: 'insert.php',
            data: $('#frmBox').serialize(),
            success: function(response){
              alert(response);
            }
          });
        });

          // var form = document.getElementById('frmBox').reset();

        });
</script>

【问题讨论】:

  • 您可能会发现使用FormData 对象是前进的方向 - stackoverflow.com/questions/44283986/… 可能会有所帮助
  • 如果没有适当的处理,您将无法通过 ajax 发送文件,@RamRaider 有一个很好的解决方案,如果您想先尝试保存数据,请注释掉文件输入PHP 中的表单和变量。

标签: php html mysql ajax


【解决方案1】:

您必须在脚本中使用preventDefault 以防止表单被提交。 然后,您将能够将数据传递给您的 php 脚本,但图像除外。为此,您可以使用FormData。 这是脚本:

<script type="text/javascript">
    $('document').ready(function(){
        $('#frmBox').submit(function(e){
            e.preventDefault();
            var formData = new FormData(this);
            $.ajax({
                type: 'POST',
                url: 'insert.php',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response){
                    alert(response);
                }
            });
        });
    });
</script>

【讨论】:

    猜你喜欢
    • 2014-08-02
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多