Hi guys I am attempting to get data entered in an HTML form through PHP then converting this data into a JSON object which will be saved on a server and retrieved to the form incase someone wants to edit entries. Please help and advice on an alternative if you think of any.

Usually giving an alternative requires showing us what you've come up with. I always process and validate all form data before storing in a database. And I usually only store data as JSON into a database for multiple values, like data from checkboxes. I'll be happy to give more input if you display your code first.

Storing a string in db from JSON decode has really no meaning at all. The only case I can think of that might have some reason is storing data that you don't know what you do with them but you don't want to loose them because might be precious to the application in near future (e.g. all the info data that sends facebook for the user when login).

You are talking about a form , probably it has certain fields , so there you are , you already have your table.

If the form is generated dynamically again you know what data type each field can have , so you create a forms. table having basic info about the form , id title etc , a formFields. table to contain form.id and what fields each form have , their name , their title , their type and so on , a formEntries. that is a an entry (from post) to a form having form.id and maybe a userId or what ever you like and last formEntryField. having a formEntries.id , formFields.id and a value.

Thanks guys. I'm using dummy data for now. Here is the code.

<!doctype html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="easy.css">
    <meta charset="UTF-8">
    <title>Basics of PHP OOP</title>
</head>
<body>

    <form method="post" action="Client.php">
  <label> Name: </label> <input  type="text" name="name"  /> <br/>
        <label> Number: </label>   <input type="tel" name="phonenumber" /><br/>
        <label> Email: </label> <input  type="email" name="email"  /> <br/>
        <label> DOB: </label>   <input type="text" name="birthdate"  /><br/>
        <label> CV: </label>   <input type="file" name="cv"  /><br/>

    <input type="submit" value="Test Me" />
</form>

</body>
</html>



<?php

class BusaryClient {
//declaring variables to hold form input.
    public $clientName = "";
    public  $clientNumber = "";
    public  $clientDOB = "";
    public  $clientCV = "";
    public  $clientEmail = "";
}
//assigning form input to variables
$clientName = $_POST ["name"];
$clientNumber = $_POST ["phonenumber"];
$clientEmail = $_POST ["email"];
$clientDOB = $_POST ["birthdate"];
$clientCV = $_POST ["cv"];
//putting form input into a array
$busaryClient = array('clientName' => $clientName,  'clientNumber' => $clientNumber,'clientEmail' => $clientEmail, 'clientDOB' =>  $clientDOB, 'clientCV' => $clientCV);

//require_once 'json.js';
echo '<br/>';
echo '<br/>';
//transforming to JSON object
$info = json_encode($busaryClient);
require 'dbconnect.php';
//$file = fopen('new_map_data.json','a+');
//fwrite($file, $info);
//fclose($file);







<?php
/**
 * Created by PhpStorm.
 * User: ASIC
 * Date: 2015/04/16
 * Time: 3:35 PM
 */

$servername = "localhost";
$username = "root";
$password = "Jemimah2011";
$dbname = "SSC";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO busaryapps (bursaryJSON)
VALUES ('$info')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Were do I begin...

  • The 'BusaryClient' class that you have there isn't doing anything. It's not being instantiated, and even if it was, it wouldn't do anything because all it does is declare blank variables, but then nothing else. It doesn't need to be there.

  • There shouldn't be spaces when assigning your POST values to variables.
    Turn: $_POST ["name"] to $_POST["name"]

  • This method provides no security.

  • I strongly recommend storing variables in separate fields within a database, but if you do insist on storing as one big string, use serialized data instead of JSON.

  • This may have been a mistake made when copying over code, but you open php twice in a row but close it only once.

That's what I got for now, only had a quick moment to glance over the code.

So it is not necessary to use JSON at all right? Yes I created the class hoping to build up on it later. Thanks

Member Avatar for diafol

I agree with previous contributors. Use a DB in the way it is ment to be used. Don't store JSON data. However, if you really need to do this, you can do so with a NoSQL DB like Mongo - but, I doubt very much whether you want to do this.

Thank you so much really appreciate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.