[英]Ajax, PHP, SQL, and JavaScript
I ran into an issue for which I cannot find the answer, I rarely ask questions here, but I am rather stumped. 我遇到了一个找不到答案的问题,我在这里很少问问题,但我很沮丧。 Any assistance shall be appreciated.
任何帮助将不胜感激。
This is the PHP that receives the Ajax call. 这是接收Ajax调用的PHP 。
<?php
session_start();
$_SESSION["my_data"] = $_POST['action'];
$DB_HOSTNAME = 'localhost';
$DB_USERNAME = 'username';
$DB_PASSWORD = 'password';
$link2 = mysqli_connect($DB_HOSTNAME,$DB_USERNAME,$DB_PASSWORD) or die('Unable to establish a DB1 connection');
mysqli_select_db($link2, '$DB_USERNAME');
$orderQuery = mysqli_query($link2, "SELECT * FROM table WHERE id='".$_SESSION['my_data']."'");
$orderQuery = mysqli_fetch_assoc($orderQuery);
$orderInfo = "
<table class='table table-striped'>
<tbody>
<tr>
<td>#: </td>
<td>". $_SESSION['my_data'] ."</td>
</tr>
<tr>
<td> Full name: </td>
<td>". $orderQuery['firstname'] . " " . $orderQuery['lastname'] ."</td>
</tr>
<tr>
<td> Address: </td>
<td> ". $orderQuery['shipping_address_1'] ."<br> ". $orderQuery['shipping_city'] . " " . $orderQuery['shipping_zone'] . " " . $orderQuery['shipping_postcode'] ." </td>
</tr>
<tr>
<td> Card Expiry Date Month: </td>
<td> 08 </td>
</tr>
</tbody>
</table><br>
";
echo $orderInfo/* . $_POST['action']*/; ?>
And this is the script that makes the call. 这是进行调用的脚本。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var myWindow;
function myFunction() {
myWindow = window.open('', '_blank');
myWindow.document.write("<link rel='stylesheet' type='text/css' href='stylesheet.css'>");
var orderNum;
orderNum = document.getElementsByClassName('summary_value')[0].innerHTML;
orderNum = orderNum.replace("#", "");
$.ajax( { type : 'POST',
data : {'action':orderNum},
url : 'process.php',
success: function ( data ) {
myWindow.document.write( data );
},
error: function ( xhr ) {
alert( "error" );
}
});
myWindow.document.write("<br>");
myWindow.document.write(document.getElementById("payInfor").innerHTML);
}
</script>
<button onclick='myFunction()' class="btn btn-default">Print Pay Info</button>
It could be a simple issue, but I can't see it. 这可能是一个简单的问题,但我看不到。
I know the Ajax is working because it displays some of the information ($_SESSION['my_data'])
, so I am thinking it's something with my SQL statements, but the syntax looks correct. 我知道Ajax正在工作,因为它显示了一些信息
($_SESSION['my_data'])
,所以我认为这与我的SQL语句有关,但语法看起来正确。
At first glance i think that mysqli_select_db($link2, '$DB_USERNAME');
乍一看,我认为
mysqli_select_db($link2, '$DB_USERNAME');
here is the error. 这是错误。
it must be mysqli_select_db($link2, $DB_USERNAME);
它必须是
mysqli_select_db($link2, $DB_USERNAME);
or mysqli_select_db($link2, $DB_NAME);
或
mysqli_select_db($link2, $DB_NAME);
mysqli_select_db()
expects parameter one to be the connection and parameter two to be the database name. mysqli_select_db()
期望参数一为连接,参数二为数据库名称。 In your case you are passing '$DB_USERNAME'
. 就您而言,您正在传递
'$DB_USERNAME'
。 So your code will look to connect to database named '$DB_USERNAME'
because of the single quotes. 因此,由于单引号,您的代码将看起来连接到名为
'$DB_USERNAME'
数据库。 Change it to the database name instead. 改为将其更改为数据库名称。 Either
$DB_NAME
or 'database_name'
should work. $DB_NAME
或'database_name'
应该都可以。 While in development mode, try enabling error_reporting(E_ALL)
to catch errors like this. 在开发模式下,尝试启用
error_reporting(E_ALL)
来捕获此类错误。
try to replace this mysqli_fetch_assoc
with mysqli_fetch_array
尝试将此
mysqli_fetch_assoc
替换为mysqli_fetch_array
and ($link2, "SELECT * FROM table WHERE id='".$_SESSION['my_data']."'")
和
($link2, "SELECT * FROM table WHERE id='".$_SESSION['my_data']."'")
with ($link2, "SELECT * FROM table WHERE id=".$_SESSION['my_data']."")
与
($link2, "SELECT * FROM table WHERE id=".$_SESSION['my_data']."")
note: remove single quote (' ') for id 注意:删除ID的单引号('')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.