Reputation: 95
I already have the entire SQL database set up.
The profile page already works but right now it just displays the profile information of the person who is logged in.
I want to set this up so that it is profile.php?ID=1 so anyone could view any profile in the database.
I have tried a couple of different tutorials, and looked at answers given to others on here but I still can't get it to work. I am stuck.
<?php
session_start();
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_Recordset1 = "-1";
if (isset($_SESSION['MM_Username'])) {
$colname_Recordset1 = $_SESSION['MM_Username'];
}
mysql_select_db($database_login_form, $login_form);
$query_Recordset1 = sprintf("SELECT * FROM users WHERE username = %s", GetSQLValueString($colname_Recordset1, "text"));
$Recordset1 = mysql_query($query_Recordset1, $login_form) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
include 'logout_script.php';
?>
Upvotes: 0
Views: 1557
Reputation: 95
I was able to resolve this by changing to code from MM_Username to id and was able to plug in the appropriate details from there. Thank you!
Upvotes: 0
Reputation: 12505
Try the code below. I have notated the important parts:
session_start();
// Try making a class that has a more current database connection
// mysql_ doesn't cut it anymore. This is just a basic example
class Database
{
private static $singleton;
private function __construct()
{
}
public static function connect($host = "host",$user = "user",$pass="password",$db = "database_name")
{
if(!empty(self::$singleton))
return self::$singleton;
try {
self::$singleton = new mysqli($host,$user,$pass,$db);
}
catch(Exception $e) {
die($e);
}
return self::$singleton;
}
}
// Create a fetch user function to make your querying easier on you
function get_user($userid = false)
{
$con = Database::connect();
$user = (!$userid)? "":" where ID = ?";
if($query = $con->prepare("select * from users{$user}")) {
/* bind parameters for markers */
$query->bind_param("s", $userid);
/* execute query */
$query->execute();
/* bind result variables */
$result = $query->get_result();
if($result) {
while($row = $result->fetch_assoc())
{
$new[] = $row;
}
}
$query->close();
}
return (empty($new))? 0 : $new;
}
// First check if an id is set and if it's a number
if(!empty($_GET['ID']) && is_numeric($_GET['ID']))
$userid = $_GET['ID'];
// If not, try and get the logged in user id
elseif(!empty($_SESSION['ID']))
$userid = $_SESSION['ID'];
// Set as false (for error purposes)
else
$userid = false;
// IF not false
if($userid)
//get the profile
$profile = get_user($userid);
// If empty, let user know
if($profile == 0)
echo 'Profile doesn\'t exist.';
// If good, include profile page
else
include("profile.php");
// No reference for this so, not sure what this does
include 'logout_script.php';
Upvotes: 1