2013-11-03 2 views
-2

여기에 PHP 코드가 삽입되어 있습니다.PHP 내부에 HTML 코드 삽입

그러나 실행하는 동안 오류가 발생합니다. 나는 많은 시간을 확인했지만 버그를 볼 수 없었다. 도움을 주시면 감사 : 내가 JSON 응답을 할 때

<?php 
    $url = $_POST['url']; 
    $user_id = $_POST ['userid']; 
    if(isset($_POST['rate'])) 
    { 
     $rate =$_POST['rate']; 
    } 
    else 
     $rate = 0; 
    $data = file_get_contents($url); 
    function get_title($html) 
    { 
     return preg_match('!<title>(.*?)</title>!i', $html, $matches) ? $matches[1] : ''; 
    } 

    function get_logo($html) 
    { 
     preg_match_all('/\bhttps?:\/\/\S+(?:png|jpg)\b/', $html, $matches); 
     //echo "mactch : $matches[0][0]"; 
     return $matches[0][0]; 
    } 

    function plaintext($html) 
    { 
     // remove comments and any content found in the the comment area (strip_tags only removes the actual tags). 
     $plaintext = preg_replace('#<!--.*?-->#s', '', $html); 

     // put a space between list items (strip_tags just removes the tags). 
      $plaintext = preg_replace('#</li>#', ' </li>', $plaintext); 

      // remove all script and style tags 
     $plaintext = preg_replace('#<(script|style)\b[^>]*>(.*?)</(script|style)>#is', "", $plaintext); 

     // remove br tags (missed by strip_tags) 
      $plaintext = preg_replace("#<br[^>]*?>#", " ", $plaintext); 

      // remove all remaining html 
      $plaintext = strip_tags($plaintext); 

     return $plaintext; 
    } 

    function trim_display($size,$string) 
    { 
     $trim_string = substr($string, 0, $size); 

     $trim_string = $trim_string . "..."; 
     return $trim_string; 
    } 

    $title = get_title($data); 
    $logo = get_logo($data); 
    $title_display = trim_display(30,$title); 
    $content = plaintext($data); 
    $Preview = trim_display(100,$content); //to Show first 100 char of the web page as preview 

    function MakeTinyUrl($url) 
    { 
     return md5($url); 
    } 

    $hash = MakeTinyUrl($url); 
    ob_start(); 
    $con = mysqli_connect('127.0.0.1', 'root', '', 'mysql'); 
    if (mysqli_connect_errno()) 
    { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     return; 
    } 

    $content=mysqli_real_escape_string($con,$content); 
    $Preview=mysqli_real_escape_string($con,$Preview); 
    $title_display=mysqli_real_escape_string($con,$title_display); 

    $result = mysqli_query($con,"SELECT COUNT(*) as val FROM post_data WHERE url ='".$url."' and userid='".$user_id."'"); 
    $bool= mysqli_fetch_assoc($result); 
    if($bool['val']) 
    { 
     echo '<div style="clear:both;"><i>You have already worked on this url..</i> </div>'; 
    } 
    else 
    { 
     $insertQuery = "INSERT INTO post_data(`userid`, `url`, `desc`, `preview`, `img_url`, `title` ,`hash`) VALUES ('".$user_id."','".$url."','".$content."','".$Preview."','".$logo."','".$title_display."','".$hash."')"; 
     if (!mysqli_query($con,$insertQuery)) 
     { 
      die('Error: ' . mysqli_error($con)); 
     } 
    } 

    $result = mysqli_query($con,"SELECT * FROM post_data WHERE userid ='".$user_id."' and url='".$url."'"); 
    //This will fetch only one row from db 
    while ($row = @mysqli_fetch_array($result)) 
    { 
     $title = $row['title']; 
     $url = $row['url']; 
     $preview = $row['preview']; 
     $image = $row['img_url'];  
    } 
    //Update Rate value in table 
    $update = "update post_data set rate='".$rate."' where url='".$url."'"; 
    if (mysqli_query($con, $update)) 
    { 
     //echo "updated"; 
    } 
    else 
    { 
     //echo "Not updated"; 
    } 



echo '<style type="text/css"> 
    .fragment 
    { 
     font-size: 12px; 
     font-family: tahoma; 
     height: 140px; 
     width: 400px; 
     border: 1px solid #ccc; 
     color: #555; 
     display: block; 
     padding: 10px; 
     box-sizing: border-box; 
     text-decoration: none; 
    } 

    .fragment:hover 
    { 
     box-shadow: 2px 2px 5px rgba(0,0,0,.2); 
    } 

    .fragment img 
    { 
     float: left; 
     margin-right: 10px; 
    } 

    .fragment h3 
    { 
     padding: 0; 
     margin: 0; 
     color: #369; 
    } 

    .fragment h4 
    { 
     padding: 0; 
     margin: 0; 
     color: #000; 
    } 

    .fragment tbox 
    { 
     padding: 1px; 
     height: 30px; 
     width: 424px; 
    } 
    #close 
    { 
     float:right; 
     display:inline-block; 
     padding:2px 5px; 
     background:#ccc; 
    } 

    </style> 

    <a class="fragment" href="google.com"> 
     <div> 
      <span id="close" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;">x</span>'; 
      echo '<img src =' $image echo '" height="116" width="116" alt="some description"/>'; 
      echo '<h3>'; $title echo '</h3>'; 
      echo '<h4>'; $url echo ' </h4>'; 
      echo '<p class="text">'; $preview 
       echo '</p> 

     </div> 
    </a>'; 


     $div1 = ob_get_clean(); 
     ob_start(); 
     $result = mysqli_query($con,"SELECT * FROM post_data WHERE userid ='".$user_id."'"); 
     echo "Records for user : $user_id"; echo "<br/>"; 
     while ($row = @mysqli_fetch_array($result)) 
     { 
      $title = $row['title']; 
      $url = $row['url']; 
      $preview = $row['preview']; 
      $image = $row['img_url'];  
      echo "Title: $title"; echo '<br/>'; 
      echo "URL: $url"; echo '<br/>'; 
      echo "Preview : $preview"; echo '<br/>'; 
      echo "Image url: $image"; echo '<br/>'; 
      //echo '</br>'; 
     }  
     $div2 = ob_get_clean(); 
     $resultArray = array("resultOne" => $div1,"resultTwo" => $div2); 
     echo json_encode($resultArray); 

    ?> 

https://gist.github.com/karimkhanp/b8bfb88db6e438fb8be6

를 참조하십시오, 그것은 오류가 아래에 있습니다 : 당신의 문자열을 CONCAT하는

SyntaxError: JSON.parse: unexpected character 


var data=JSON.parse(xmlhttp.responseText); 
+1

오류를 추측해야합니까? –

+0

데이타베이스 호출에서'@'연산자를 제거하십시오. 이들은 거의 필요하지 않으며 개발시 도움이되지 않습니다. – halfer

답변

2

사용 문자열 연결 (.을).

<a class="fragment" href="google.com"> 
     <div> 
      <span id="close" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;">x</span>'; 
      echo '<img src ='.$image.'" height="116" width="116" alt="some description"/>'; 
      echo '<h3>'.$title.'</h3>'; 
      echo '<h4>'.$url.'</h4>'; 
      echo '<p class="text">'.$preview.'</p> 
     </div> 
    </a>'; 

내가 메모장과 같은 더 나은 텍스트 편집기를 사용하는 것이 좋습니다 ++ 또는 어도비 드림위버 :

그냥 168-176th 라인을 교체합니다.

+0

우분투에서 작동하고 gedit을 사용합니다. – user123

+0

@Karimkhan 님, 환영합니다 :) [이 우분투에서 메모장 ++의 대안은 무엇입니까?] (http://askubuntu.com/questions/313973/what-are-the-alternatives -to-notepad-on-우분투) –

+0

@Sumit : Notepad ++에서 구문 오류를 강조 했습니까? DW는 그렇지 않습니다. NetBeans 또는 Eclipse와 같은 IDE가 더 나은 것으로 판단됩니다. – halfer