2013-12-20 62 views
0

我想通過CODE1傳遞一個變量從我的HTML頁面到CODE2,並在我的瀏覽器(HTML頁面)中獲取輸出。我收到下面的錯誤。原因是什麼?如何解決這個問題?從HTML到MySQL的變量,並返回到HTML不工作

ERROR:
警告:mysqli_real_escape_string()預計參數1是mysqli的,空在/home/abc/abc/ajax/test.php給定線

HTML

<body> 
Name: <input type="text" id="thename"> 
<input type="submit" id="thename-submit" value="GRAB"> 
<div id="thename-data"></div> 

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="js/global.js"></script> 
</body> 

CODE1

$('input#thename-submit').on('click', function(){ 
    var ajaxname = $('input#thename').val(); 

    if ($.trim(ajaxname)!=''){ //if string is empty --- trim is to remove spaces only 
     $.post('ajax/name.php', {thename:ajaxname}, function(data){ 
      $('div#thename-data').text(data); 
     }); 
    } 
}); 

CODE2

<?php 
if(isset($_POST['thename']) === true && empty($_POST['thename']) === false) { 
    $getVal = mysqli_real_escape_string($conn_db, trim($_POST['thename'])); 
    require '../db/connection.php'; 
    $query = ("SELECT `photos`.`theurl` FROM `photos` WHERE `photos`.`thename` = '" . $getVal . "'") or die(mysqli_error($conn_db)); 
    $result = mysqli_query($conn_db, $query); 

    $queryA = ("SELECT id FROM photos"); 
    $resultA = mysqli_query($conn_db, $queryA); 
    $row_cnt = $resultA->num_rows; 

    echo ($row_cnt !== 0 ? mysqli_result($result, '0', 'theurl') : 'Not found.'); // syntax meaning:: echo condition ? if TRUE output : if FALSE output; 
} 

function mysqli_result($result, $ro, $field) { 
    $result->data_seek($ro); 
    $datarow = $result->fetch_array(); 
    return $datarow[$field]; 
} 
?> 

回答

1

您需要更改這兩個行的順序:

$getVal = mysqli_real_escape_string($conn_db, trim($_POST['thename'])); 
require '../db/connection.php'; 

我認爲connection.php分配$conn_db

+0

經驗表明..快速精確解先生.. :) –

+0

我剛纔讀的錯誤信息,這是完全清楚。 – Barmar

+0

@Barmar謝謝 - 這是問題所在。 :) – NathaliaZeed

0

你應該換這條線

$getVal = mysqli_real_escape_string($conn_db, trim($_POST['thename'])); 
require '../db/connection.php'; 

require '../db/connection.php'; 
$getVal = mysqli_real_escape_string($conn_db, trim($_POST['thename'])); 
+0

謝謝。它現在正在工作。 :) – NathaliaZeed