繁体   English   中英

php,从mysql获取一行并设置变量以显示链接

[英]php, getting a row from mysql and setting a variable to show a link

好的,谁能看到我在这里做错了什么? 只是因为我的编码是垃圾哈哈。 我所拥有的是一个会员网站,这些链接是基于用户是否登录而制作的。 实际上我想要实现的是通过检查用户在注册时选择的帐户类型来更改配置文件链接

我已经在这个绝对的几个小时,我不知道还有什么我可以尝试,我确定我在正确的轨道,但我的PHP技能留下了很多不足之处

继承我的代码,如果有人可以帮助我将永远伟大,忘记我试图传递的是$logOptions_account来实现链接和数据库中的accounttype字段是枚举类型,如果它有所作为

<?php
/* 
 * Date: january 20, 2012
   *------------------------------------------------------------------------------------------------*/
session_start(); // Start Session First Thing
// Force script errors and warnings to show on page in case php.ini file is set to not display them
error_reporting(E_ALL);
ini_set('display_errors', '1');
//-----------------------------------------------------------------------------------------------------------------------------------
include_once "scripts/connect_to_mysql.php"; // Connect to the database
$dyn_www = $_SERVER['HTTP_HOST']; // Dynamic www.domainName available now to you in all of your scripts that include this file
//------ CHECK IF THE USER IS LOGGED IN OR NOT AND GIVE APPROPRIATE OUTPUT -------
$logOptions = ''; // Initialize the logOptions variable that gets printed to the page
// If the session variable and cookie variable are not set this code runs
if (!isset($_SESSION['idx'])) { 
  if (!isset($_COOKIE['idCookie'])) {
     $logOptions = '<a href="//' . $dyn_www . '/register.php">Register Account</a>
 &nbsp;&nbsp; | &nbsp;&nbsp; 
 <a href="http://' . $dyn_www . '/login.php">Log In</a>';
   }
}
// If session ID is set for logged in user without cookies remember me feature set
if (isset($_SESSION['idx'])) { 

$decryptedID = base64_decode($_SESSION['idx']);
$id_array = explode("p3h9xfn8sq03hs2234", $decryptedID);
$logOptions_id = $id_array[1];
$logOptions_username = $_SESSION['username'];
$logOptions_username = substr('' . $logOptions_username . '', 0, 15); // cut user name down in length if too long

// Check if this user has any new PMs and construct which envelope to show
$sql_pm_check = mysql_query("SELECT id FROM private_messages WHERE to_id='$logOptions_id' AND opened='0' LIMIT 1");
$num_new_pm = mysql_num_rows($sql_pm_check);
if ($num_new_pm > 0) {
    $PM_envelope = '<a href="pm_inbox.php"><img src="images/pm2.gif" width="18" height="11" alt="PM" border="0"/></a>';
} else {
    $PM_envelope = '<a href="pm_inbox.php"><img src="images/pm1.gif" width="18" height="11" alt="PM" border="0"/></a>';
}
// Ready the output for this logged in user
$act = "";
$logOptions_account = $act;
$act = mysql_query("SELECT accounttype FROM memberstable WHERE accounttype='$logOptions_account' ");
if ($act == "a") {
    $logOptions_account = '<a href="fanprofile.php">';}
    else if($act == "b") {
    $logOptions_account = '<a href="artistprofile.php">';}
    else if($act == "c") {
    $logOptions_account = '<a href="bandprofile.php">';}
    else if($act == "d") {
    $logOptions_account = '<a href="businssprofile.php">';}
    else if($act == "e") {
    $logOptions_account = '<a href="venueprofile.php">';}
    else if($act == "f") {
    $logOptions_account = '<a href="serviceprofile.php">';}
    else {
    }



$logOptions = $PM_envelope . ' &nbsp; &nbsp;
<a href="http://' . $dyn_www . '">Home</a>
&nbsp;&nbsp; |&nbsp;&nbsp; 
<a href="http://' . $dyn_www . '/' . $logOptions_account . '?id=' . $logOptions_id . '">Profile</a>
&nbsp;&nbsp; |&nbsp;&nbsp;
<div class="dc">
<a href="#" onclick="return false">Account &nbsp; <img src="images/darr.gif" width="10" height="5" alt="Account Options" border="0"/></a>
 <ul>
<li><a href="http://' . $dyn_www . '/edit_profile.php">Account Options</a></li>
<li><a href="http://' . $dyn_www . '/pm_inbox.php">Inbox Messages</a></li>
<li><a href="http://' . $dyn_www . '/pm_sentbox.php">Sent Messages</a></li>
<li><a href="http://' . $dyn_www . '/logout.php">Log Out</a></li>

</ul>
</div>
';

} else if (isset($_COOKIE['idCookie'])) {// If id cookie is set, but no session ID is set yet, we set it below and update stuff

$decryptedID = base64_decode($_COOKIE['idCookie']);
$id_array = explode("nm2c0c4y3dn3727553", $decryptedID);
$userID = $id_array[1]; 
$userPass = $_COOKIE['passCookie'];
// Get their user first name to set into session var
$sql_uname = mysql_query("SELECT firstname, lastname, email, accounttype FROM memberstable WHERE id='$userID' AND password='$userPass' LIMIT 1");
$numRows = mysql_num_rows($sql_uname);
if ($numRows == 0) {
    // Kill their cookies and send them back to homepage if they have cookie set but are not a member any longer
    setcookie("idCookie", '', time()-42000, '/');
    setcookie("passCookie", '', time()-42000, '/');
    header("location: index.php"); // << makes the script send them to any page we set
    exit();
}
while($row = mysql_fetch_array($sql_uname)){ 
    $firstname = $row["firstname"];
    $surname = $row["lastname"];
    $useremail = $row["email"];
    $logOptions_account = $row["accounttype"];
}

$_SESSION['id'] = $userID; // now add the value we need to the session variable
$_SESSION['idx'] = base64_encode("g4p3h9xfn8sq03hs2234$userID");
$_SESSION['firstname'] = $firstname;
$_SESSION['surname'] = $surname;
$_SESSION['useremail'] = $useremail;
$_SESSION['userpass'] = $userPass;

$logOptions_id = $userID;
$logOptions_uname = $firstname;
$logOptions_uname = $surname;
$logOptions_uname = substr('' . $logOptions_uname . '', 0, 15); 
///////////          Update Last Login Date Field       /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
mysql_query("UPDATE memberstable SET last_log_in=now() WHERE id='$logOptions_id'"); 
// Ready the output for this logged in user
// Check if this user has any new PMs and construct which envelope to show
$sql_pm_check = mysql_query("SELECT id FROM private_messages WHERE to_id='$logOptions_id' AND opened='0' LIMIT 1");
$num_new_pm = mysql_num_rows($sql_pm_check);
if ($num_new_pm > 0) {
    $PM_envelope = '<a href="pm_inbox.php"><img src="images/pm2.gif" width="18" height="11" alt="PM" border="0"/></a>';
} else {
    $PM_envelope = '<a href="pm_inbox.php"><img src="images/pm1.gif" width="18" height="11" alt="PM" border="0"/></a>';
}
// Ready the output for this logged in user
 $act = "";
$logOptions_account = $act;
$act = mysql_query("SELECT accounttype FROM memberstable WHERE accounttype='$logOptions_account' LIMIT 1");
if ($act == 'a') {
    $logOptions_account = '<a href="fanprofile.php">';}
    else if($act == 'b') {
    $logOptions_account = '<a href="artistprofile.php">';}
    else if($act == 'c') {
    $logOptions_account = '<a href="bandprofile.php">';}
    else if($act == 'd') {
    $logOptions_account = '<a href="businssprofile.php">';}
    else if($act == 'e') {
    $logOptions_account = '<a href="venueprofile.php">';}
    else if($act == 'f') {
    $logOptions_account = '<a href="serviceprofile.php">';}
    else {
    }
 $logOptions = $PM_envelope . ' &nbsp; &nbsp;
 <!--<a href="http://' . $dyn_www . '">Home</a>
&nbsp;&nbsp; |&nbsp;&nbsp; -->
 <a href="http://' . $dyn_www . '/' . $logOptions_account . '?id=' . $logOptions_id . '">Profile</a>
&nbsp;&nbsp; |&nbsp;&nbsp;
<div class="dc">
<a href="#" onclick="return false">Account &nbsp; <img src="../images/darr.gif" width="10" height="5" alt="Account Options" border="0"/></a>
<ul>
<li><a href="http://' . $dyn_www . '/edit_profile.php">Account Options</a></li>
<li><a href="http://' . $dyn_www . '/pm_inbox.php">Inbox Messages</a></li>
<li><a href="http://' . $dyn_www . '/pm_sentbox.php">Sent Messages</a></li>
<li><a href="http://' . $dyn_www . '/logout.php">Log Out</a></li>
</ul>
</div>';
}



?>

我真的很抱歉,如果我做错了什么或愚蠢的,但我现在一整天都看着这个,因为我说如果有人可以提供帮助,我会永远很高兴

mysql_query()返回资源,而不是文本。

$act = mysql_query // this is not text, so never "a, b, c ..."

我也想给你一些建议。 这是约$act 您将它用作String,MySQL数据集,并再次用作Char。 因此,如果您在此处对不同的数据类型使用单独的变量,则可以轻松管理。 当然你可以用PHP做到这一点。 但我认为我的建议是一个很好的做法。

无论如何,在考虑你的代码。 你能尝试改变波纹管代码吗?

$act = "";
$logOptions_account = $act;
$act = mysql_query("SELECT accounttype FROM memberstable WHERE accounttype='$logOptions_account' ");
if ($act == "a") 

在这里,我看到了几个问题。

$act = "";
$logOptions_account = $act;

请参阅$act是一个空字符串。 然后将其分配给$logOptions_account 所以最终你的查询将是。

SELECT accounttype FROM memberstable WHERE accounttype=''

所以你不会得到结果。 你需要改变它。 我无法预测$logOptions_account中的确切值。

但是如果你纠正了。 您可以继续使用以下类型的代码。

$act = "";
$result = mysql_query("SELECT accounttype FROM memberstable WHERE accounttype='$logOptions_account' ");
while($row = mysql_fetch_array($result))
{
    $act = $row['accounttype'];
    if ($act == "a") {
    $logOptions_account = '<a href="fanprofile.php">';}
    else if($act == "b") {
    $logOptions_account = '<a href="artistprofile.php">';}
    else if($act == "c") {
    $logOptions_account = '<a href="bandprofile.php">';}
    else if($act == "d") {
    $logOptions_account = '<a href="businssprofile.php">';}
    else if($act == "e") {
    $logOptions_account = '<a href="venueprofile.php">';}
    else if($act == "f") {
    $logOptions_account = '<a href="serviceprofile.php">';}
    else {
    }
}

上面的代码只是您理解的一个例子。

通常,每个用户应该只有一行,所以这应该这样做...请参阅此处给出的示例:

http://de.php.net/manual/en/function.mysql-fetch-row.php

这完全是胡说八道:

$act = ""; // is empty
$logOptions_account = $act; // both are empty
$act = mysql_query("SELECT accounttype FROM memberstable WHERE accounttype='$logOptions_account' "); // why you query for $logOptions_account(=$act) only to get $act again?
if ($act == "a") { // it is still empty, not "a"

最后得到一个:

// make sure, that $logOptions_account has a valid value
$result = mysql_query("SELECT accounttype FROM memberstable WHERE accounttype='$logOptions_account' ");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
}
else {
  $row = mysql_fetch_row($result);
  if ($row['accounttype'] == "a") { ... }
  elseif ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM