简体   繁体   English

从jQuery发送json到php并插入数据库

[英]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 我正在使用此代码将其发送到php文件

    $.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. 然后我想将4个字段插入到mySQL数据库中。 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. 我在另一页的会话中使用var_dump,数据正在正确处理。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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