Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 5 123 ... LastLast
Results 1 to 15 of 73
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts

    News Script problems

    I have a news script which is only suppose to allow admins to edit,delete, add, etc news. Anyone else the script is suppose to redirect. My problem is that the script redirects me even though I am an admin. I've tried !=4 as well but still have problems.

    Code:
    $level = get_level();
    if ($level <=4) 
    header('Location: http://www.example.com/');
    Code:
    function get_level()
    {
    $sql = mysql_query("SELECT level from users WHERE userid='" . $_SESSION['userid'] . "'") or die(mysql_error());
    $result = $sql;
    mysql_fetch_array($result);
    }
    This is the entire script.
    Code:
    <?php
    session_start();
    include "database.php";
    include "/functions/index.php";
    $level = get_level();
    if ($level <=4) 
    header('Location: http://www.example.com/');
    ?>
    
    <?php
    if (empty($_POST['title'])) 
    echo '<p>You need to enter a title.</p>';
     else 
    $title = $_POST['title'];
    
    if (empty($_POST['news'])) 
    echo '<p>You need to enter a message.</p>';
     else 
    $news = $_POST['news'];
     
    if ($title && $news) {
    $query = "INSERT INTO news (title, news, date) VALUES ('$title', '$news', NOW())";
    $result = @mysql_query($query);
    }
    
    if ($result) 
    echo '<p>News was successfully added!</p>';
     else 
    echo '<p>Error #101: News could not be added. Please try again.</p>';
    
    ?>
     
    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="input" name="title" size="25" maxlength="60" value="<?php if (isset($_POST['title'])) echo $_POST['title']; else echo 'Title'; ?>" />
    <textarea rows="7" cols="55" name="news"><?php if (isset($_POST['message'])) echo $_POST['message']; else echo 'News Message'; ?></textarea>
    <input type="submit" name="submit" value="Submit" />
    <input type="hidden" name="submitted" value="TRUE" />
    </form>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    Your function never returns any value at all, let alone the value of the level from the array.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by Old Pedant View Post
    Your function never returns any value at all, let alone the value of the level from the array.
    how would I get it to return a value? I've already tried
    echo $row['level']

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    PHP Code:
    $level get_level(); 
    if (
    $level <=4)  header('Location: http://www.example.com/'); 
    Looks like you just need to do some Basic Debugging 101

    echo the value of $level that is being returned and I think you'll find an issue there.

    Code:
    $level = get_level(); 
    echo 'level = '.$level; die();
    if ($level <=4)  header('Location: http://www.example.com/');

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    Bullant just repeated what I said.

    I'm not a PHP person, but as I said, you need to RETURN a value from your function.

    Remembering that I don't use PHP, but that I can read manuals, I think you need this:
    Code:
    function get_level()
    {
        $result = mysql_query("SELECT level from users WHERE userid='" . $_SESSION['userid'] . "'") or die(mysql_error());
        $row = mysql_fetch_array($result);
        $level = $row[0];
        mysql_free_result($result);
        return $level;
    }
    Based on reading this:
    http://www.php.net/manual/en/functio...etch-array.php
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    Actually, for completeness you probably want this:
    Code:
    function get_level()
    {
        $result = mysql_query("SELECT level from users WHERE userid='" . $_SESSION['userid'] . "'") or die(mysql_error());
        $level = 0; // assume the userid isn't found?
        if ( $row = mysql_fetch_array($result) ) { 
            $level = $row[0]; // only get the level is there was at least one record
        }
        mysql_free_result($result);
        return $level;
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Old Pedant View Post
    Bullant just repeated what I said.
    I did more than that. I showed the op how he/she could check for themself where the problems are.

    Since you admit you don't use php then if you want to risk misleading op's like you did in another thread (post 2), where your suggested code was riddled with errors, that is up to you. It's not my role to go round cleaning up your php mistakes when you make them
    Last edited by bullant; 05-30-2011 at 11:10 AM.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    All you did was show him code that would have echoed out a completely blank value for $level, since his get_level() code was returning nothing.

    It was a correct comment, but pretty much useless to him if he doesn't know how to return a value from a function and/or get a value from a SQL query.

    I was HOPING that you would correct my PHP if it's wrong and show him how to return an actual value.

    Instead, you take me to task for attempting an answer. If it's wrong, it's wrong. But at least it's an answer.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    I also posted a link showing how I go about testing/debugging my code.

    Quote Originally Posted by Old Pedant View Post
    I was HOPING that you would correct my PHP if it's wrong and show him how to return an actual value.
    As I said, it's not my role to go behind you cleaning up your code.

    You have posted code that has turned out to be garbage on too many occasions for me to justify wasting my time checking your code. (Blue52's factorising thread and the link I posted earlier).

    If you can't handle your code with errors in it being highlighted here and elsewhere then maybe consider getting it right before you post it or don't post it at all. I assume you are prepared to stand by whatever you post.

    If you are genuinely interested in learning php and not post error riddled code then maybe Read This For More

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    I have ZERO interest in learning PHP.

    The same way you have zero interest in being TRULY helpful and instead enjoy attacking people more than helping others.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by bullant View Post
    As I said, it's not my role to go behind you cleaning up your code.
    When someone doesn't know PHP yet is trying to help the O.P with the skills they have available to them, it's a common courtesy for someone more experienced in a certain area to step in and help.

    Anyhow, with regards to the function:

    Code:
    function get_level()
    {
        $result = mysql_query('SELECT level from users WHERE userid=\''.$_SESSION['userid'].'\'') or die(mysql_error());
    
        return ((mysql_num_rows($result)) ? mysql_result($result, 0) : 0);
    }
    Then change this part:

    Code:
    $level = get_level();
    if ($level <=4)
    to:

    Code:
    if (get_level() <= 4)
    No use assigning it to a var when that's the only place you use it.

  • #12
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Old Pedant View Post
    I have ZERO interest in learning PHP.

    The same way you have zero interest in being TRULY helpful and instead enjoy attacking people more than helping others.
    no problem - you are entitled to an opinion

    I just call it as I see it.

  • #13
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by MattF View Post
    When someone doesn't know PHP yet is trying to help the O.P with the skills they have available to them, it's a common courtesy for someone more experienced in a certain area to step in and help.
    Yes that is true, but when someone posts garbage code on more than 1 occasion I refuse to waste more time cleaning up after them.

    It's a question of priorities for me. I would prefer to spend time helping someone else directly rather than clean up someone else's "solution" that contains errors. Imo everyone should take responsibility for their own posts and be accountable for them.

    Like I said, it's not my role at all to go cleaning up other peoples "solutions". Maybe it's the moderators' or some other members' role

    If you take Old pedant's opinion
    Quote Originally Posted by Old Pedant View Post
    If it's wrong, it's wrong. But at least it's an answer.
    that sounds a bit narcissistic to me.

    If everyone took the attitude that any answer, correct or not, is better than no answer then the forums would be riddled with rubbish.
    Last edited by bullant; 06-01-2011 at 04:11 AM.

  • #14
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    This is the MySql forum all said and done.

    If the OP requires php help, then it would've been better to move the thread to the php forum.

    If the question involved both, php and MySQL, then the MySql could be fixed here and the OP then sent to the php forum for other assistance. It makes much more sense doing it that way when others may come to CF later to search for stuff. otherwise why should there be numerous forums.

    If we look back at Old_Pedants many previous threads, it is clear that any php 'advice' is a pointer at least and imho, is therefore useful even if not conclusive.


    my 2c.
    Last edited by bazz; 06-01-2011 at 04:55 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #15
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    [ot]
    yep , agree this is more of a php issue.

    Old Pedant made an unconditional statement saying he hoped I would correct any errors in his code.

    I simply gave an honest reason, with supporting evidence (Blue52's factorising thread and the link I posted earlier), why I won't waste anymore time cleaning up after Old Pedant.

    If people want to post "solutions" about something they know little about, that is fine . But imo they still should take responsibility for any errors they post and be accountable for them and not "hope" (as old pedant appears to) that I or someone else will come up behind them to clean up any errors. I refuse to clean up after them since it does nothing more than waste my time.

    If someone else (moderators or other members) has the time and is willing to clean up after Old Pedant, or anyone else for that matter, I have no issue with that all .

    [/ot]
    Last edited by bullant; 06-01-2011 at 06:10 AM.


  •  
    Page 1 of 5 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •