# serialize width php mysql (ok)

![](https://2726517656-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M1E4Gk2ppVKb4olmnun%2F-MaBwrTdmEA7t4ObRl4l%2F-MaC3vxosLTe9llijDzK%2FScreenshot_3.jpg?alt=media\&token=2326edaa-3d8d-4f73-9781-cb73bb89b540)

![](https://2726517656-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M1E4Gk2ppVKb4olmnun%2F-MaBwrTdmEA7t4ObRl4l%2F-MaC3zK12Gb6G5laQTmx%2FScreenshot_5.jpg?alt=media\&token=7c058ba0-3a59-444c-8a73-0c449a4bdcd7)

C:\xampp\htdocs\windows\index.php

```
<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
  <script type="text/javascript">
  $(document).on('click', '#save', function(e) {
  	e.preventDefault();
    var data = $("#form-search").serialize();
    $.ajax({
      data: data,
      type: "post",
      url: "save.php",
      dataType: 'json',
      success: function(data) {
        console.log(data)
      }
    });
  });
  </script>
</head>
<body>
  <form action="" id="form-search">
    First name: <input type="text" name="FirstName" value="Mickey"><br>
    Last name: <input type="text" name="LastName" value="Mouse"><br>
  </form>
  <button id="save">Serialize form values</button>
</body>
</html>
```

C:\xampp\htdocs\windows\save.php

```
<?php
include 'database.php';
$FirstName = $_POST['FirstName'];
$LastName  = $_POST['LastName'];
if (isset($_POST['FirstName'])) {
  $sql = "INSERT INTO `user_data`( `FirstName`, `LastName`) VALUES ('$FirstName','$LastName')";
  if (mysqli_query($conn, $sql)) {
    echo json_encode(["status"=>"Success!"]);
  } else {
    echo json_encode(["status"=>"Error!"]);
  }
  mysqli_close($conn);
}
?>
```

C:\xampp\htdocs\windows\database.php

```
<?php
	$servername = "localhost";
	$username   = "root";
	$password   = "";
	$db         = "school";
	$conn       = mysqli_connect($servername, $username, $password, $db);
?>
```

```
-- phpMyAdmin SQL Dump
-- version 5.1.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: May 21, 2021 at 06:01 AM
-- Server version: 10.4.18-MariaDB
-- PHP Version: 7.4.16

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `school`
--

-- --------------------------------------------------------

--
-- Table structure for table `user_data`
--

CREATE TABLE `user_data` (
  `id` int(11) NOT NULL,
  `FirstName` varchar(100) NOT NULL,
  `LastName` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `user_data`
--

INSERT INTO `user_data` (`id`, `FirstName`, `LastName`) VALUES
(0, 'Mickey', 'Mouse'),
(0, 'Mickey 1', 'Mouse 1'),
(0, 'Mickey 2', 'Mouse 2');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

```

{% file src="https://2726517656-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M1E4Gk2ppVKb4olmnun%2F-MaBwrTdmEA7t4ObRl4l%2F-MaC4hTHO57Avwap2VtS%2Fschool.sql?alt=media&token=e5691dfc-5a55-43d6-babd-af2f71c739df" %}
