2016-07-24 9 views
0

件名には多くの質問がありますが、私は初心者であり、回答をまとめることはできません。AJAXの応答として配列を取得

私はフォームを使ってajaxを使ってMySQLデータベースを更新しようとしています。そして、更新されたデータベースをメインページ(ページを更新することなく)で使用する配列として戻します。

私は最初の部分を管理しましたが、2番目の部分は管理しませんでした。

最初の部分のための私のコード:

HTML:

$("#form").submit(function(){ 

      event.preventDefault(); 

      var form = new FormData(); 
      form.append("id", $('#id').val()); 
      form.append("src", $('#src').val()); 
      form.append("title_en", $('#title_en').val()); 
      form.append("title_he", $('#title_he').val()); 
      form.append("exhibition_en", $('#exhibition_en').val()); 
      form.append("exhibition_he", $('#exhibition_he').val()); 
      form.append("subjects_en", $('#subjects_en').val()); 
      form.append("subjects_he", $('#subjects_he').val()); 
      form.append("keywords_en", $('#keywords_en').val()); 
      form.append("keywords_he", $('#keywords_he').val()); 
      form.append("height", $('#height').val()); 
      form.append("width", $('#width').val()); 
      form.append("sold", $('#sold').val()); 
      var settings = { 
       "async": true, 
       "crossDomain": true, 
       "url": "update.php", 
       "method": "POST", 
       "dataType": 'json', 
       "processData": false, 
       "contentType": false, 
       "mimeType": "multipart/form-data", 
       "data": form 
      } 

      $.ajax(settings).success(function(data) { 

       }); 
      }); 

PHP:そのような

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "chana_goldberg"; 

    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 
    $conn->set_charset("utf8"); 

    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 


    $id = $_POST['id'];  
    $src = $_POST['src']; 
    $title_en = $_POST['title_en']; 
    $title_he = $_POST['title_he']; 
    $exhibition_en = $_POST['exhibition_en']; 
    $exhibition_he = $_POST['exhibition_he']; 
    $subjects_en = $_POST['subjects_en']; 
    $subjects_he = $_POST['subjects_he']; 
    $keywords_en = $_POST['keywords_en']; 
    $keywords_he = $_POST['keywords_he']; 
    $height = $_POST['height']; 
    $width = $_POST['width']; 
    $sold = $_POST['sold']; 

    $sql = "UPDATE paintings_catalog SET title_en='$title_en', title_he='$title_he', exhibition_en='$exhibition_en', exhibition_he='$exhibition_he', subjects_en='$subjects_en', subjects_he='$subjects_he', keywords_en='$keywords_en', keywords_he='$keywords_he', height='$height', width='$width', sold='$sold' WHERE id='$id'"; 
    $conn->query($sql);?> 
+0

おそらくJSON(JavaScriptオブジェクト表記を必要とします)は、PHPでサポートされています。参照:http://php.net/manual/en/ref.json.phpおよびhttp://www.w3schools.com/js/js_json.asp –

+0

あなたのコードはSQLインジェクションに対して脆弱です。リクエストで受け取ったSQLに文字列を埋め込む代わりに、準備されたステートメントを使用してください。 – trincot

答えて

0

$sql = "SELECT * FROM paintings_catalog WHERE id=".$id; 
$ret = []; 
if($res = $conn->query($sql)) { 
    $ret = $res->fetch_assoc(); 
} 

echo json_encode($ret); 
関連する問題