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.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Posts
    331
    Thanks
    9
    Thanked 6 Times in 6 Posts

    News script echoing array

    Is there something I did wrong in this script? Its echoing the word Array at the top of the page and keeps refreshing even though my account's level is 4. I've tried <=4 and !==4 but neither seem to work. It also echoes "news successfully added once the page has loaded without any info being entered into the form.
    Code:
    <?php
    session_start();
    include "/home/xtraz/public_html/test/includes/database.php";
    $level = "SELECT level from users WHERE userid='" . $_SESSION['userid'] . "'";
    $result = mysql_query($level) or die ('Error: '.mysql_error ());
    echo (mysql_fetch_array($result));
    if($level !=4)
    {
    echo "<meta http-equiv='refresh' content='0;url='http://test.xtraz.cz.cc/'>";
    }
    ?>
    
    <form name="news" action="" method="post">
    <input type="text" name="title" value="Title" />
    <textarea rows="2" cols="20" name"news">
    </textarea>
    <input type="submit" value="Submit" />
    
    <?php
    $query = "INSERT INTO news(title, news, date) VALUES('$_POST[title]','$_POST[news]', NOW())"; 
    mysql_query($query); 
    print "News successfully added."; 
    mysql_close($c);
    ?>

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    254
    Thanks
    1
    Thanked 63 Times in 63 Posts
    mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
    ^ that's the reason it's echoing an Array based on your
    PHP Code:
    echo (mysql_fetch_array($result)); 
    instead, do a
    PHP Code:
    var_dump(mysql_fetch_array($result)); 
    Based on my comments below, I doubt you wanted to do an echo in the first place. I believe what you wanted to achieve was retrieve the value of the "level" field from the result set and assign it to the $level variable.


    PHP Code:
    if($level !=4
    will ALWAYS occur because you're testing
    Code:
    "SELECT level from users WHERE userid='" . $_SESSION['userid'] . "'";
    against the value 4.
    Just do an
    PHP Code:
    echo $level
    before your "if" conditional; basic preliminary test.


    I'd suggest you go back and look up some basic tutorial on SQL queries. Should fix your problem. And before attempting anything else, try to do a simple query (like
    Code:
    SELECT * FROM users WHERE userid = 1;
    ) and print out the result to the screen. Once you get that part sorted out then you can move on.


  •  

    Posting Permissions

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