简体   繁体   中英

send json from jquery to php and insert into database

I need to insert data from a webpage into a database. I'm using this code to send it to the php file

    $.getJSON('./calendar.php?action=save&id=0&start='+ds.getTime()/1000+'&end='+df.getTime()/1000,
      { 
        'body':$('#calendar_new_entry_form_body').val(), 
        'title':$('#calendar_new_entry_form_title').val() 
      }

and then I want to insert the 4 fields into a mySQL database. Right now, I can retrieve the data using

  $start_date=(int)$_REQUEST['start'] - 60*60;

  $data=array( 
    'title'=>(isset($_REQUEST['title'])?$_REQUEST['title']:''), 
    'body' =>(isset($_REQUEST['body'])?$_REQUEST['body']:''), 
    'start'=>date('c',$start_date), 
    'end'  =>date('c',(int)$_REQUEST['end'] - 60*60) 
    ); 

Then I save it into a session variable just to see if I am actually recieving it

  $id=(int)$_REQUEST['id']; 
  if($id && isset($_SESSION['calendar'][$id])){ 
    $_SESSION['calendar'][$id]=$data; 
  }  
  else{ 
    $id= ++$_SESSION['calendar']['ids']; 
    $_SESSION['calendar'][$id]=$data; 
  }

I'm using var_dump on session in another page and the data is going through correctly.

session_start(); 
var_dump($_SESSION);
echo "<br>";
echo $_SESSION['calendar'][1]['title'];
echo "<br>";
echo $_SESSION['calendar'][1]['start'];

What I can't seem to be able to do, is insert this data into a database. Every time I try to run a query, nothing happens.

The query I'm using is:

$query1 = "INSERT INTO `doc`.`appointment` (`start`, `end`, `title`, `body`) VALUES ('$data['start']', '$data['end']', '$data['title']', '$data['body']');";
      $result1 = $con->query($query1);

your query

$query1 = "INSERT INTO `doc`.`appointment` (`start`, `end`, `title`, `body`) VALUES ('$data['start']', '$data['end']', '$data['title']', '$data['body']');";
  $result1 = $con->query($query1);

i have corrected it little bit try this

$query1 = "INSERT INTO `doc`.`appointment` (`start`, `end`, `title`, `body`) VALUES (".$data['start'].", ".$data['end'].", ".$data['title'].", ".$data['body'].")";
  $result1 = $con->query($query1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM