我想通過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];
}
?>
經驗表明..快速精確解先生.. :) –
我剛纔讀的錯誤信息,這是完全清楚。 – Barmar
@Barmar謝謝 - 這是問題所在。 :) – NathaliaZeed