2017-12-09 3 views
0

ログインしたユーザーが追加したファイルの表示に問題があります。 SQLクエリに変数を正しく渡す方法がわかりません。 誰も私にこれを手伝ってもらえますか?phpを使ったsqlのセッション変数の使い方

現在、コードは次のようになります。

<?php 
include_once 'dbconnect.php'; 
?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>File Uploading With PHP and MySql</title> 
<link rel="stylesheet" href="style.css" type="text/css" /> 
</head> 
<body> 
<div id="header"> 
<label>File Uploading With PHP and MySql</label> 
</div> 
<div id="body"> 
<table width="80%" border="1"> 
    <tr> 
    <th colspan="4">your uploads...<label><a href="index.php">upload new files...</a></label></th> 
    </tr> 
    <tr> 
    <td>File Name</td> 
    <td>File Type</td> 
    <td>File Size(KB)</td> 
    <td>View</td> 
    </tr> 
    <?php 
$sql="SELECT * FROM files"; 
$result_set=mysql_query($sql); 
while($row=mysql_fetch_array($result_set)) 
{ 
    ?> 
     <tr> 
     <td><?php echo $row['file'] ?></td> 
     <td><?php echo $row['type'] ?></td> 
     <td><?php echo $row['size'] ?></td> 
     <td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td> 
     </tr> 
     <?php 
} 
?> 
    </table> 

</div> 
</body> 
</html> 

私はこのレコードに変更しようとしています:

$sql="SELECT file, type, size FROM files WHERE userId ='$_SESSION[userId]'";

$sql="SELECT * FROM files";

を私はまだやります正しいrを得られないesult。誰も助けることができますか?

+0

mysqlは廃止予定です。代わりにmysqliを使用してください。 –

+0

あなたは何を意味するのかを指定できますか? – martyniuk

+0

https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php –

答えて

1

この行の問題は、$ _SESSION変数をどのように含めるかにあります。 の引用符は、$_SESSION['userId']または{$_SESSION['userId']}のようにする必要があります。

さらに重要なのは、変数を直接MySQLクエリに入力しないことです。 MySQLの代わりにMySQLiまたはPDOを使用し、準備済みの文(たとえば、hereまたはhere)を調べることをお勧めします。

関連する問題