Jump to content

What's wrong with this script? ...


Zoofu

Recommended Posts

<?php

function profile($uid, $link = FALSE){
$sql1 = "SELECT * FROM `users` WHERE `id`='".$uid."'";
$res1 = mysql_query($sql1) or die(mysql_error());
if(mysql_num_rows($res1) == 0){
	return "Invalid User";
}else {
	$row1 = mysql_fetch_assoc($res);
	if(!$link){

	return $row1['username'];	
	}else {
		return $row1['username'];
	}
}
}


echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"20%\">\n";
echo "<tr align=\"left\"><td class=\"forum_header\">".profile($row1['username'])."</a><br>UserID: ".$uid."</td></tr>\n";
echo "<br>\n";
echo "</table>\n";

?>

 

It's returning invalid users. If this script is wrong, please can someone tell me... I'm making a profile page, and I need it to show the users name.

 

As in like, if the profile id is 2, it'll show User #2's name.

Link to comment
https://forums.phpfreaks.com/topic/171376-whats-wrong-with-this-script/
Share on other sites

Don't just return "Invalid user" avoid typo's and use constants.

 

define('PROFILE_ERR_INVALID_USER', 'invalid');

function profile($uid, $link = FALSE) {
    $sql1 = "SELECT * FROM `users` WHERE `id`='".$uid."'";
    $res1 = mysql_query($sql1) or die(mysql_error());
    if(mysql_num_rows($res1) == 0){
        return PROFILE_ERR_INVALID_USER;
    } else {
        $row1 = mysql_fetch_assoc($res1);
        if (!$link) {
            return $row1['username'];   
        } else {
            return $row1['username'];
        }
    }
}

 

Afterwards use:

 

$response = profile(..);
if (PROFILE_ERR_INVALID_USER !== $response) {

 

 

I will go simple to start.


$username = $_POST['username']; // Get Username via a form

$sql1 = "SELECT * FROM `users` WHERE `username`='$username' ";
    $res1 = mysql_query($sql1) or die(mysql_error());
    if(mysql_num_rows($res1) == 0){
        return PROFILE_ERR_INVALID_USER;
    } else {
        $row1 = mysql_fetch_assoc($res1);
        if (!$link) {
            return $row1['username'];   
        } else {
            return $row1['username'];
        }
    }

 

Try this.

 

If it works we can go a bit more more deeper

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.