用户选择日期、月份和年份,然后按下按钮,根据他选择的日期向他显示数据库的值我不知道代码中的问题在哪里
<style>
.button {border-radius: 8px;}
.button1 {font-size: 20px;}
</style>
<form>
إختر التاريخ : <input type="date" name="bday" id="dat"><br>
<button type=button class="button button1" onclick="document.getElementById('data').innerHTML = myFunction(document.getElementById('dat').value)">إظهار الإستهلاك اليومي</button>
<hr>
<span id="data" ></span>
</form>
<script>// <![CDATA[
function myFunction($x) {
$current_user=get_current_user_id();// to fetch the right data for the right user
// ياخذ اليوم والشهر والسنه الي اختارها اليوزر
global $wpdb;
$d=date('j');//define variable to store the day
$m=date('m');//define variable to store the month
$y=date('Y');//define variable to store the year
$daily_amount= $wpdb->get_var("SELECT daily_amount FROM arduino_period_use where ID=$current_user and day=d and month=$m and year=$y ");
//print_r($daily_amount);
}
// ]]></script
也许如果你有一些错误检查,你会被告知你的SQL语法错误:
$daily_amount= $wpdb->get_var("[..snip..] and day=d and month=$m and year=$y ");
^---- missing $
数据库中不太可能有d
字段,因此查询将失败并出现未知字段错误。
问题是PHP是一种服务器端语言,其运行方式(根本不)与javascript完全相同,看起来您似乎将两者混淆了。
查看使用 javascript 与服务器端 PHP 代码通信的 ajax 请求以访问您的数据库记录