2013-11-03 4 views
3

이전 질문에 대한 사람들의 제안이 도움이되지 않습니다. 그래서, 여기에 전체 코드에 다시 게시 생각 : mysql_depricated.php :이스케이프 & 문자열에?

<script> 
function fun() 
{ 
var ajaxRequest; // The variable that makes Ajax possible! 

    try{ 
     // Opera 8.0+, Firefox, Safari 
     ajaxRequest = new XMLHttpRequest(); 
    } catch (e){ 
     // Internet Explorer Browsers 
     try{ 
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } catch (e) { 
      try{ 
       ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
      } catch (e){ 
       // Something went wrong 
       alert("Your browser broke!"); 
       return false; 
      } 
     } 
    } 

    var name = document.getElementById("name").value; 
    var url = "mysql_depricated2.php"; 
    var param = "name=" + name; 

    ajaxRequest.open("POST", url, true); 

//Send the proper header information along with the request 
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
ajaxRequest.setRequestHeader("Content-length", param.length); 
ajaxRequest.setRequestHeader("Connection", "close"); 

    ajaxRequest.send(param); 
} 
</script> 

<input type="text" id="name"> 
<input type="button" value="ajax&" onclick="fun()"> 

mysql_depricated2 : 나는 & 기호를 포함하는 문자열을 삽입하려고하면

<?php 
     $host="localhost"; // Host name 
     $username="root"; // Mysql username 
     $password=""; // Mysql password 
     $db_name="example"; // Database name 

    // Connect to server and select databse. 
    $con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 


$content = $_POST['name']; 
$content=mysql_real_escape_string($content); 

$sql = "insert into e values('$content')"; 
mysql_query($sql); 
?> 

그것은 작동하지 않습니다. 어떤 해결책을 주시겠습니까? 이 하나

ajaxRequest.send(param); 

:

+1

고객님의 질문이 중복됩니다 : [PHP를 통해 MySQL에 삽입 할 수 없습니다] (http://stackoverflow.com/questions/19753471/cant-be-inserted-in-mysql-through-php). 추가 정보를 제공하고 싶다면 기존 질문을 ** 수정 **하고 다시 묻지 마십시오. – Quentin

답변

4

은 다음 줄을 교체

ajaxRequest.send(encodeURIComponent(param)); 

그래서 당신이 %26 자사의 URL 인코딩 형식으로 & 특수 문자를 인코딩합니다.

+0

데이터베이스에 성공적으로 삽입 할 수 있도록 해독하려면 어떻게해야합니까? @Nelson – rosemary

+0

디코딩에 특별한 조치를 취할 필요는 없습니다. PHP가 자동으로 처리합니다. – Nelson

+0

encodeURIComponent를 사용하고 echo $ content를 사용하는 경우 빈 문자열 만 표시합니다. – rosemary

관련 문제