-2

i am having problem in uploading data to online mysql database. i am using scheduled tasks to run php script all the data is sent to the server through json_encode and the data is received perfectly but when inserting into mysql database it only insert first alphabet of that data

this code gets the data

$da = $_GET['data'];
$d = json_decode($da, true);

by printing it shows complete data but only inserts the first alphabet of the data

$a = 0;
foreach($d as $data)
    {
        echo $v = "INSERT INTO clms_data(id,teacher_id,course_id,session,section,stDate,endDate,type,description,filename,allow) VALUES(".$data[$a][0].",'".$data[$a][1]."','".$data[$a][2]."','".$data[$a][3]."','".$data[$a][4]."','".$data[$a][5]."','".$data[$a][6]."','".$data[$a][7]."','".$data[$a][8]."','".$data[$a][9]."',".$data[$a]['allow'].")";

        $qu = mysql_query($v) or die(mysql_error());
        if($qu)
        {
            echo "Execute $a<br />";
        }
        else
        {
            echo "Error";
            }
    $a++;
        }
  • 1
    This is not everything, the code you have pasted in your question is nothing wrong with.. Add some more details/code to your question. – node_modules Apr 18 '16 at 09:19
  • Why are you showing us the code which "receives the data perfectly" and not the code which fails to insert it into the database correctly? – Quentin Apr 18 '16 at 09:19
  • What is the value you are dealing with anyway? Create a proper test case: http://stackoverflow.com/help/mcve – Quentin Apr 18 '16 at 09:20
  • 2
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 18 '16 at 09:23
  • Share your json data; – itzmukeshy7 Apr 18 '16 at 09:24
  • `Array ( [ID] => 8 [0] => 8 [teacher_id] => 0366 [1] => 0366 [course_id] => CSC-101 [2] => CSC-101 [session] => Fall 2016 [3] => Fall 2016 [section] => A [4] => A [stDate] => 2016-04-07 [5] => 2016-04-07 [endDate] => 2016-04-13 [6] => 2016-04-13 [type] => Quiz [7] => Quiz [description] => Test [8] => Test [filename] => [9] => [allow] => 0 [10] => 0 )` – Sana Ullah Apr 18 '16 at 09:28
  • @SanaUllah `for` loop is not required just pass values from array to query; and upgrade from `mysql` to `mysqli` or `PDO` to prevent SQL injections; – itzmukeshy7 Apr 18 '16 at 09:41
  • i use for loop in case if there is multiple insertion with different data – Sana Ullah Apr 18 '16 at 09:43
  • @SanaUllah then the format of data will be different then how you will manage that format of data? – itzmukeshy7 Apr 18 '16 at 13:17

1 Answers1

0

Can't you do it by simple insert query with json

insert.php

<?php
include_once('connect.php');

    error_reporting( error_reporting() & ~E_NOTICE ); 

      $id= $_GET['id'];
      $teacher_id= $_GET['teacher_id'];
      $course_id= $_GET['course_id'];
      $session= $_GET['session'];
    {


                    $insert="INSERT INTO clms_data(id,teacher_id,course_id
                                                        ,session)values
                                                            ('$id','$teacher_id','$course_id',session)";
                   $result = mysqli_query($con, $insert);
                    if(!$result)
                    {

                        print("invalid query");
                    }
                    else
                    {


                        $output['success']=1;
                        $output['message']="Insert records successfully";
                        print(json_encode($output));

                    }
                }
    ?>

Try this example with json Ask if there is any doubt