2016-01-02 46 views
2

我從包含需要刪除的轉義斜槓的PHP文件返回數據。我創建了一個多維數組的返回對象。我用json_encode($data, JSON_UNESCAPED_SLASHES)返回它,但斜槓沒有被刪除。PHP不刪除轉義斜槓

我自己也嘗試了涉及使用雖然和stripslashes()甚至沒有工作在陣列中的每個單獨的串循環不同的方法。

我有沒有麻煩所需要的數據在正確的JSON結構返回,但我似乎無法擺脫這些斜線!

$data = array('bodyCopy' => array(), 'projects' => array()); 

$sql = "SELECT * FROM " . $table; 
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn)); 
while($row = mysqli_fetch_assoc($result)) { 
    $data['bodyCopy'] = $row; 
} 

$sql = "SELECT * FROM experienceSectionProjects"; 
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn)); 
while($row = mysqli_fetch_assoc($result)) { 
    array_push($data['projects'], $row); 
} 

echo json_encode($data, JSON_UNESCAPED_SLASHES); 

我返回對象洛斯這樣的:

編輯 這裏是包含斜槓一些真實的數據:

{ 
    "bodyCopy":{ 
    "title":"Experience's", 
    "headerIcon":"briefcase", 
    "introCopy":"Don't", 
    "gridHeader":"header" 
    }, 
    "projects":[ 
    { 
    "id":"1", 
    "company":"Medical Mutual", 
    "position":"UI Developer", 
    "logo":"medmutual.png", 
    "backgroundImg":"medMutual.jpg", 
    "projectSummary":"Online enrollment application for health insurance marketplace", 
    "description":"I work as the dedicated front end developer on the online enrollment application for the Medical Mutual Individual Insurance Marketplace and Medicare Advantage Marketplace. As front end developer my main responsibility is generating HTML/CSS/JS for the users interface. I collaborate with server side developers in an C#.NET MVC environment to create dynamic content and integrate the code from the UI into Sitecore. Aside from the Online Enrollment applications, I am involved with developing other public facing websites for the company including an internal recruitment website." 
    }, 
    { 
    "id":"2", 
    "company":"Macy\'s", 
    "position":"UI/AngularJS Developer", 
    "logo":"macys.png", 
    "backgroundImg":"macys.jpg", 
    "projectSummary":"CRM software for in store sales associates", 
    "description":"" 
    } 
] 
} 

編輯

這可能會更多信息。這是我正在寫值到數據庫:

$projects = $p['projects']; 
    if(is_array($projects)) { 
     foreach ($projects as $project) { 

      $id = $project['id']; 
      $company = mysqli_real_escape_string($conn, $project['company']); 
      $position = mysqli_real_escape_string($conn, $project['position']); 
      $logo = mysqli_real_escape_string($conn, $project['logo']); 
      $backgroundImg = mysqli_real_escape_string($conn, $project['backgroundImg']); 
      $projectSummary = mysqli_real_escape_string($conn, $project['projectSummary']); 
      $description = mysqli_real_escape_string($conn, $project['description']); 

      $Query = $conn->prepare("INSERT INTO experienceSectionProjects (id, company, position, logo, backgroundImg, projectSummary, description) 
      VALUES (?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE company = VALUES(company), position = VALUES(position), logo = VALUES(logo), backgroundImg = VALUES(backgroundImg), projectSummary = VALUES(projectSummary), description = VALUES(description)"); 
      $Query->bind_param('issssss', $id, $company, $position, $logo, $backgroundImg, $projectSummary, $description); 
      $Query->execute(); 
      $Query->close(); 
     } 
    } 
+2

你在哪裏看到斜槓? – Ctx

+0

爲了便於閱讀,我刪除了真實數據,因爲我不認爲這很重要,但如果您認爲它可以幫助您,我會添加一些真實數據。 – Ian

+0

您是否驗證了恆定是您認爲的常數? – Rooster

回答

3

mysqli_real_escape_string增加了斜線的字符串,這樣當你把它放在一個SQL查詢,特殊字符不會被作爲特殊字符處理。

bind_param將變量傳遞給準備好的語句,而不將它們直接放入SQL語句中。

做一個或另一個(提示:bind_param更安全)不是兩個

+0

謝謝你做到了! – Ian

3

更換含mysqli_real_escape_string()與此有關的所有行...

$company = $project['company']; 
$position = $project['position']; 
$logo = $project['logo']; 
$backgroundImg = $project['backgroundImg']; 
$projectSummary = $project['projectSummary']; 
$description = $project['description']; 

mysqli_real_escape_string()增加了在需要以便原始查詢不會翻倒斜線。在準備原始查詢之後,您已經使用準備好的語句來綁定變量,因此這不是必需的。

+0

謝謝你做到了!儘管他在幾分鐘內擊敗了你,但我必須首先回答綠色選中標記 – Ian