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 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Regular Coder
    Join Date
    Jun 2014
    Posts
    108
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Doesn't go to the selected ID but stays on the current.

    First time messing with Posts and Id's of them, anyways, i made this code
    PHP Code:
    include_once("db_conx.php");
    $sql= "SELECT * FROM posts ORDER BY Id DESC LIMIT 1"; 
    $query = mysqli_query($db_conx, $sql);
    // Now make sure that user exists in the table
    $numrows = mysqli_num_rows($query);
    if($numrows < 1){
    echo "That post does not exist, press back";
        exit(); 
      }

      while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    $title = $row["title"];
    $category = $row['category'];
    $element1 = $row['element1'];
    $element2 = $row['element2'];
    $element3 = $row['element3'];
    $element4 = $row['element4'];
    $element5 = $row['element5'];
    $pic1 = $row['picture1'];
    if ($pic1 == NULL) {
      $pic1 = '<img src = "Images/Add-pic.png" width="120px" height="120px"/>';
    }

    }
    <div id="postSelf">
      <h3 class="titleSelf"><?php echo $title ?></h3>
        <div class="elements">
           <?php echo $pic1?>
     <?php echo $element1;?>

      </div>
      </div>
    and here redirect to that
    PHP Code:
       $sql "INSERT INTO posts (poster) VALUES ( {$_SESSION["userid"]} )"
        echo 
    "$sql";
        
    mysqli_query($db_conx$sql);
        
    $sql "SELECT LAST_INSERT_ID()";
        
    $result mysqli_query($db_conx$sql);
        
    $photoid mysqli_fetch_array($resultMYSQLI_NUM)[0];
        echo 
    "$photoid";
        
    $_SESSION["photoid"] = $photoid;
       
    $sql "UPDATE posts SET category = '$Category', created = now() WHERE Id = {$photoid}";
          
    $query mysqli_query($db_conx$sql);
          
    header("Location: posts.php?id=".$photoid); 
    now, at the URL I've got id = (what id), when i change it to a different one it still displays the same page with the page ID, for instance - it says id=38 and shows the 38 id, shows everything correctly, when i change it to 37, in the database it has different values it still writes the one with the 38... What am i doing wrong here?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Are you referring to the first script?
    The first one is designed to show only the newest entry, which is what the ORDER BY and LIMIT are doing. If you want to show a specific id, you'd instead go like so:
    PHP Code:
    if (isset($_GET['id']) && $_GET['id'] > 0)
    {
        
    $sql "SELECT * FROM posts WHERE id = " . (int)$_GET['id'];
    }
    else
    {
        
    $sql"SELECT * FROM posts ORDER BY Id DESC LIMIT 1";  

    This way if you give it an ?id=x, it will load that post. Without, it will load the newest post.

    Edit:
    Also, I'm not sure I like the names of your properties. In particular, the incrementing number on a prefix name *usually* indicates an issue with database design normalization. Are you sure the structure is normalized (if not it could create major headache's when querying in the future)
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    It's normalized now, FouLu.

    The field POSTER in the POST table is a foreign key to ID in the USERS table. And that ID is what is stored in $_SESSION["userid']

    In the POSTS table, the ID field is the AUTOINCREMENT and PRIMARY KEY field. He refers to that in the PHP code as $photoid. (It ought to be $postid, but that was my fault, when I thought the table name was PHOTOS, not POSTS.)
    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.

  • #4
    Regular Coder
    Join Date
    Jun 2014
    Posts
    108
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    It's normalized now, FouLu.

    The field POSTER in the POST table is a foreign key to ID in the USERS table. And that ID is what is stored in $_SESSION["userid']

    In the POSTS table, the ID field is the AUTOINCREMENT and PRIMARY KEY field. He refers to that in the PHP code as $photoid. (It ought to be $postid, but that was my fault, when I thought the table name was PHOTOS, not POSTS.)
    Actually, it isn't a foreign key, you gave me later a different script, showed me something that I don't need to make it foreign.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    You are wrong. I was *NOT* showing you how to make it not a foreign key! IT STILL SHOULD BE! It just shouldn't be a UNIQUE key, which is a very, very, very different thing! Once again, if you would use SHOW CREATE TABLE post; and show me the result of that we could straighten this out.
    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
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    This code is UTTERLY BOGUS:
    Code:
    $sql= "SELECT * FROM posts ORDER BY Id DESC LIMIT 1";  
    $query = mysqli_query($db_conx, $sql); 
    // Now make sure that user exists in the table 
    $numrows = mysqli_num_rows($query); 
    if($numrows < 1){ 
    echo "That post does not exist, press back"; 
        exit();  
      }
    ALL THIS DOES is get the LATEST post. PERIOD.

    It might be a post by ANYBODY. Completely unrelated to any posts by the current user.

    It most certainly does NOT "make sure that user exists in the table" !!

    And this code is completely bogus if you are attempting to display an image:
    Code:
    <div id="postSelf"> 
      <h3 class="titleSelf"><?php echo $title ?></h3> 
        <div class="elements"> 
           <?php echo $pic1; ?> 
     <?php echo $element1;?> 
    
      </div> 
      </div>
    All that the line echo $pic1; will do is display the name of the image.

    What is needed is
    Code:
    <img src="/imageDirectory/<?php echo $pic1; ?>" alt=""/>
    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
    Regular Coder
    Join Date
    Jun 2014
    Posts
    108
    Thanks
    15
    Thanked 0 Times in 0 Posts
    posts CREATE TABLE `posts` (
    `Id` int(11) NOT NULL AUTO_INCREMENT,
    `title` varchar(100) DEFAULT NULL,
    `category` varchar(255) DEFAULT NULL,
    `element1` varchar(255) DEFAULT NULL,
    `picture1` varchar(255) DEFAULT NULL,
    `element2` varchar(255) DEFAULT NULL,
    `picture2` varchar(255) DEFAULT NULL,
    `element3` varchar(255) DEFAULT NULL,
    `picture3` varchar(255) DEFAULT NULL,
    `element4` varchar(255) DEFAULT NULL,
    `picture4` varchar(255) DEFAULT NULL,
    `element5` varchar(255) DEFAULT NULL,
    `picture5` varchar(255) DEFAULT NULL,
    `created` datetime DEFAULT NULL,
    `poster` int(11) DEFAULT NULL,
    PRIMARY KEY (`Id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Okay, so this is wrong. You need to make POSTER a FOREIGN KEY and also INDEX it.

    Not hard to do: Just use these commands
    Code:
        ALTER TABLE posts ADD CONSTRAINT (poster) ON users(userid);
        CREATE INDEX posts_poster ON posts(poster);
    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
    Regular Coder
    Join Date
    Jun 2014
    Posts
    108
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Okay, so this is wrong. You need to make POSTER a FOREIGN KEY and also INDEX it.

    Not hard to do: Just use these commands
    Code:
        ALTER TABLE posts ADD CONSTRAINT (poster) ON users(userid);
        CREATE INDEX posts_poster ON posts(poster);
    ALTER TABLE posts ADD CONSTRAINT (poster) ON users(userid); on this line it tells me Unxpected '(', expecting check_sym or foreign

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Oh, crud. My goof. Left out the type of constraint! Doofus.

    ALTER TABLE posts ADD CONSTRAINT FOREIGN KEY (poster) ON users(userid);
    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
    Regular Coder
    Join Date
    Jun 2014
    Posts
    108
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Alright, did that. so how do i make it work now, to go to the selected post ID, By the Way, its not userid its ID, userid is the session for the ID, ID is name in the DB

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Code:
    $postid = $_REQUEST["postid"];
    $sql = "SELECT * FROM posts WHERE id = $postid";
    BUT...

    But that will get you *all* the photos in that one post (up to 5 of them, with your current design).

    If you want to get a *particular* one of them (say number 3) you would need to send that along also. So maybe:
    [code]
    Code:
    $postid = $_REQUEST["postid"];
    $photonum = $_REQUEST["photonum"]
    $sql = "SELECT picture$photonum AS thePicture FROM posts WHERE id = $postid";
    $result = mysql_query($sql);
    $file = mysql_fetch_assoc($result)["thePicture"];
    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.

  • #13
    Regular Coder
    Join Date
    Jun 2014
    Posts
    108
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    $postid = $_REQUEST["postid"];
    $sql = "SELECT * FROM posts WHERE id = $postid";
    BUT...

    But that will get you *all* the photos in that one post (up to 5 of them, with your current design).

    If you want to get a *particular* one of them (say number 3) you would need to send that along also. So maybe:
    [code]
    Code:
    $postid = $_REQUEST["postid"];
    $photonum = $_REQUEST["photonum"]
    $sql = "SELECT picture$photonum AS thePicture FROM posts WHERE id = $postid";
    $result = mysql_query($sql);
    $file = mysql_fetch_assoc($result)["thePicture"];
    can i just fetch the whole row and use /uploads/$pic1 like you showed before?
    and it gives me an undefined index when i put the first code, by the way if you remember, it is named photoid and not postid, doesnt have too much problems with me, it doesnt matter, i changed it to photoid, i get undefined index, when using isset function it does the else statement, so isset = false by checking.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Look, the name of the field in *EITHER* table is "id".

    You have posts.id and users.id fields.

    The name $photoid or $postid or $id or WHATEVER you use in your PHP code DOES NOT MATTER! You could use $zambonis_do_it_better and it WOULD NOT MATTER.

    The IMPORTANT part is that you *MUST* be passing the needed id from the PRIOR PAGE. From the <form> on that page. And then you must use $_REQUEST["xxxx"] and replace "xxxx" with the actual name of the <form> field on that prior page.

    You have been trying to run a coding marathon before you have learned to do a coding crawl. You really need to go study up on the basics of <form>s in HTML and how they interact with PHP and how PHP then interacts with MySQL.
    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.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    can i just fetch the whole row and use /uploads/$pic1 like you showed before?
    Yes, of course.

    You seemed to be asking a different question, so I gave you an alternate 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.


  •  
    Page 1 of 2 12 LastLast

    LinkBacks (?)

    1. 09-15-2014, 11:53 PM

    Posting Permissions

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