0

I tried to store a JavaScript array into MySQL table using PHP, see the following script below.

I first converted the string using JSON.stringify and passed it into a PHP file via AJAX request.

I then converted it to a PHP array, and after after I inserted those arrays using serialize();.

Finally, it properly stored using localhost but it does not work on my live server.

sample.Js

 $.ajax(
          {
              type: "POST",
              url: "save_plan_ajax.php",
              data: {plan: plan,totalInvesment: totalInvesment, location: JSON.stringify(locationsArr), cost: JSON.stringify(cost), personalised: JSON.stringify(personalised)},
               success: function(data){
                            //alert(data);
              }
         }
     )

In this above script I passed the JavaScript array into save_plan_ajax.php using JSON.stringify.

save_plan_ajax.php

<?php
session_start();
include 'config.php';
if(isset($_POST)){
    $planname           = $_POST['plan'];
    $cost       = json_decode($_POST['cost'], true);
    $personalised   = json_decode($_POST['personalised'], true);
    $locations          = json_decode($_POST['location'], true);
    $userid             = $_SESSION['BIID'];
    $constriant         = $_SESSION['CONSTRAINT'];
    $created_date   = date("Y-m-d H:i:s");
    $total_invs     =   $_POST['totalInvesment'];

    $query = "INSERT INTO `plans` (
            `refid` ,
            `userid` ,
            `plan_name` ,
            `cost` ,
            `locations`,
            `personalised` ,
            `total_invs`,
            `constriant` ,
            `created_date` ,
            `stat`
            )
            VALUES (
            NULL ,  '$userid',  '$planname',  '".serialize($cost) ."', '".serialize($locations)."','".serialize($personalised)."', '$total_invs',  '$constriant',  '$created_date',  'A'
            )";
    $res = $GLOBALS['Db']->Insert($query);

    if($res){
        echo $res;
    }
    else{
        echo "Error";
    }

}

?>

In the above script, the record stores correctly at local server, but in this same script insert N; in server.. how do I fix this error, is the above way correct?

In the MySQL database table I have set the cost, location and personalized fields datatype as LONGTEXT.

user812786
  • 3,632
  • 3
  • 36
  • 49
Balakumar B
  • 708
  • 3
  • 13
  • 37
  • 3
    Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Jun 09 '17 at 20:00
  • The code looks like it should work. What error are you getting? – Barmar Jun 09 '17 at 20:01
  • Instead of printing just `Error`, use `mysqli_error()` to get the actual error message. – Barmar Jun 09 '17 at 20:02
  • If you are using jQuery or anything that handles serialization to form data automatically, you do not need to call JSON.stringify. – Ruben Vincenten Jun 09 '17 at 20:03
  • i did not get any error ,but record will be inserted and its value is "N;" in table@Barmar – Balakumar B Jun 09 '17 at 20:08
  • `N;` means the value is `null`, not an array. – Barmar Jun 09 '17 at 20:10
  • See https://stackoverflow.com/questions/14297926/structure-of-a-serialized-php-string for how to interpret the result of `serialize()`. – Barmar Jun 09 '17 at 20:11
  • but record has stored in my local server(127.0.0.1), but N; in my site database@Barmar – Balakumar B Jun 09 '17 at 20:12
  • How to track that in server?@Barmar – Balakumar B Jun 09 '17 at 20:13
  • @Balakumar Recheck your database connection parameters. And the servers don't show the errors by default so make sure `display_errors` option is on. – Roshana Pitigala Jun 10 '17 at 03:48

0 Answers0