Jump to content

no idea what my problem is..


shcKr-

Recommended Posts

Hello,

Im pretty new to php, but what im trying to do is create a basic permissions systems, where i can change what access the user has, this is the code I have, but I can't work out how to make it show more than one folder ???

With this code, it shows me just one folder, when I have 2 in the database..

 

<?php
include "_config.php";
include "_functions.php";
session_start();
$user_name = $_SESSION["username"];

$row=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE username = '$user_name' LIMIT 1"));
$user_id=stripslashes($row['id']);

$row2=mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'"));
$folderID=stripslashes($row2['folderID']);
$allowed=stripslashes($row2['allowed']);

$row3=mysql_fetch_array(mysql_query("SELECT * FROM `folders` WHERE id = '$folderID'"));
$name=stripslashes($row3['name']);	

echo $name;
?>

Link to comment
https://forums.phpfreaks.com/topic/154024-no-idea-what-my-problem-is/
Share on other sites

I changed my code to the following, but it still only displays 1 result  :(

 

<?php
include "_config.php";
include "_functions.php";
session_start();
$user_name = $_SESSION["username"];

$row=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE username = '$user_name' LIMIT 1"));
$user_id=stripslashes($row['id']);

$row2=mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'"));
$folderID=stripslashes($row2['folderID']);
$allowed=stripslashes($row2['allowed']);



// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT name FROM folders WHERE id='$folderID'",
    mysql_real_escape_string($name));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['name'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>

This

$query = sprintf("SELECT name FROM folders WHERE id='$folderID'",
    mysql_real_escape_string($name));

 

should be

 

$query = sprintf("SELECT name FROM folders WHERE id='%s'",
    mysql_real_escape_string($folderID));

 

Apart from that it looks fine. Are you sure this query returns more than one row? You can check using mysql_num_rows

Ok, I replaced what you put, and it doesn't give me any results now.

I added the num_rows.. this is my code:

 

this is what it gives: www.citiphones.co.uk/test/test.php

 

<?php
include "_config.php";
include "_functions.php";
session_start();
$user_name = "James";

$row=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE username = '$user_name' LIMIT 1"));
$user_id=stripslashes($row['id']);

$row2=mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'"));
$folderID=stripslashes($row2['folderID']);
$allowed=stripslashes($row2['allowed']);



// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT name FROM folders WHERE id='%s'",
    mysql_real_escape_string($folderID));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['name'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);


echo "<br><br>";


$result = mysql_query("SELECT * FROM folders");
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";
?>

This is what my tables look like:

PERMISSIONS:
  	id 	userID 	folderID 	allowed
	1 	   1 	            1 	            1
	2 	   1 	            2 	            1

USERS:
        id 	username 	password 	activated
        1 	James 	# 	             1

FOLDERS
  	id 	name
	1 	Test
	2 	Test2

Why are you mixing the way you are writing queries?

 

This is bad code embedding functions that can return incorrect values

$row2 =mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'"));

 

Better

 

$query = mysql_query("SELECT * FROM permissions WHERE userID = '".mysql_real_escape_string($user_id)."'");
$row2 = mysql_fetch_array($query);

 

 

Here also you have decided to use sprintf, why?

$query = sprintf("SELECT name FROM folders WHERE id='%s'", mysql_real_escape_string($folderID));

 

Again stick to a standard:

$query = mysql_query("SELECT name FROM folders WHERE id='".mysql_real_escape_string($row2['folderID'])."'");
while ($row = mysql_fetch_assoc($query)) {

}

 

 

I'm guessing you are copying and pasting code from a variety of sources

This is what my code stands at:

<?php
include "_config.php";
include "_functions.php";
session_start();
$user_name = "James";

$row=mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE username = '$user_name' LIMIT 1"));
$user_id=stripslashes($row['id']);

$row2=mysql_fetch_array(mysql_query("SELECT * FROM `permissions` WHERE userID = '$user_id'"));
$folderID=stripslashes($row2['folderID']);
$allowed=stripslashes($row2['allowed']);

$result = mysql_query("SELECT * FROM `folders` WHERE id = '$folderID'");
while($row = mysql_fetch_assoc($result))
  {
  echo $row['name'] . " " . $row['LastName'];
  echo "<br />";
  }
?>

 

and

 

This is what my tables look like:

PERMISSIONS:
  	id 	userID 	folderID 	allowed
	1 	   1 	            1 	            1
	2 	   1 	            2 	            1

USERS:
        id 	username 	password 	activated
        1 	James 	# 	             1

FOLDERS
  	id 	name
	1 	Test
	2 	Test2

 

Now this is what its showing me: www.citiphones.co.uk/test/test.php

 

but surely it should be displaying:

Test
Test2

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.