php를 사용하여 내 html 페이지에 알림 목록을 만들었습니다.
목록 항목을 클릭 할 때 관련 데이터를로드해야합니다. (예 : "[customer_name] need to register"를 클릭하면 해당 고객 이름으로 다른 모든 데이터를 별도의 페이지에로드해야 함) 다음과 같이 시도했습니다. 코드이지만 원하는 결과를 얻지 못했습니다.
문제를 해결하도록 도와주세요.
<?php
$dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));
function notification() {
global $dbc;
$query = "SELECT `customer_name` FROM `customer` WHERE `confirmation`IS NULL LIMIT 0, 30 ";
$result = mysqli_query($dbc,$query);
global $row;
while($row=mysqli_fetch_array($result))
{
echo"<a href='CusRegReport.php'>"."<i class='fa fa-users text-red'>"." ".$row['customer_name']." "."needs to register"."</i>"."</a>";
global $x;
$x = $row['customer_name'];
}
}
function details(){
global $x;
$sql = "SELECT * FROM `customer` WHERE `customer_name` = '$x'";
$r = mysqli_query($dbc,$sql);
while($line=mysqli_fetch_array($r))
{
$d = "<br/>".$line['customer_name']."<br/>".$line['ad_line_one']."</a>";
}
}
?>
방금 코드를 약간 다듬 었습니다.
index.php
: 여기에 알림이 표시됩니다.
<?php
$dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));
function notification(){
global $dbc;
$query = "SELECT `customer_id`, `customer_name` FROM `customer` WHERE `confirmation`IS NULL LIMIT 0, 30 ";
$result = mysqli_query($dbc,$query);
while($row=mysqli_fetch_array($result))
{
echo "<a href='CusRegReport.php?id=" . $row['customer_id'] . "'><i class='fa fa-users text-red'> " . $row['customer_name'] . " needs to register</i></a>";
}
}
?>
<!doctype html>
<html>
<body>
<?php notification(); ?>
</body>
</html>
CusRegReport.php
: 여기에 특정 ID가있는 고객의 세부 정보가 표시됩니다.
<?php
$dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));
function details(){
global $dbc; // we need this in all our functions. Then only we will be able to do the db operations within the function
$id = $_GET['id']; // we are using the id that was passed in the URL
$id = $mysqli->real_escape_string( $id ); // clean the id that the user passed via url, so that we could use it in our SQL query. Otherwise it is unsafe to use directly because it can lead to SQL injections
$sql = "SELECT * FROM `customer` WHERE `customer_id` = '$id'";
$r = mysqli_query($dbc,$sql);
while($line=mysqli_fetch_array($r))
{
echo "<br/>" . $line['customer_name'] . "<br/>" . $line['ad_line_one'] . "</a>";
}
}
?>
<!doctype html>
<html>
<body>
<?php details(); ?>
</body>
</html>
기억해야 할 사항 :
$dbc
페이지 맨 위에 mysqli 객체 를 선언했습니다 . 따라서 해당 객체를 사용하여 db에 액세스하려면 함수 내부에서 다음 global
과 같은 키워드 를 사용해야합니다 .global $dbc;
키워드를 과도하게 사용하지 마십시오 global
. 각 변수를 선언하는 데 사용했습니다! 그것은 필요하지 않습니다.
<a>
고객 세부 정보 페이지에 대한 링크를 표시 할 때 customer_id
URL에 ?
. 이렇게 전달한 데이터는 $_GET
결과 페이지 의 배열에서 사용할 수 있습니다 .
항상 정수 값을 사용하여 고객을 식별하십시오. 두 명 이상의 고객이 같은 이름을 가질 수 있기 때문입니다! 이미 db에 고객에 대한 세부 정보가 있으므로 PRIMARY KEY
해당 테이블에 대해을 사용했다고 customer_id
생각합니다. 그래서 그것을 사용하십시오.
원치 않는 연결을 피하십시오. 나중에 혼란을 줄 수 있습니다. 첫 번째 echo
줄에서는 문자열 만 연결 했음에도 불구하고 많은 연결을 사용했습니다.
SQL 쿼리에서 사용하기 전에 사용자로부터 수락 한 데이터를 이스케이프하십시오. 사용자가 제공하는 데이터가 SQL 주입 공격을 일으킬 수 있기 때문입니다. 따라서 항상 사용자 입력을 정리하십시오.
이것들은 당신을위한 간단한 기본적인 것들입니다.
도움이되기를 바랍니다. 참고 : 코드를 약간만 다듬 었습니다. 테스트 나 아무것도하지 않았습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다