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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2006
    Location
    Newcastle, england.
    Posts
    38
    Thanks
    2
    Thanked 0 Times in 0 Posts

    multiple variables not working

    Hi folks, I know this is a big ask but I have been trying to figure this one out for a few days now and it is starting to drive me mad. I'm sure this is something simple that I have done wrong but I can't for the life of me figure out what is wrong here. I'm just learning PHP so any pointers would be very greatfully appreciated.

    Some background.

    I decided I needed to learn PHP, I really enjoy it but keep getting stuck on things like this due to my lack of knowledge. So I set myself a challenge to write a PHP CMS system using PHP, mySQL, XHTML and CSS. All was going fine (all be it rather slowly) until I tried to write the page to edit articles in the CMS.

    I have the 'ID', 'title', 'venue' and 'article' stored in the database fine. I can pull entries out and display them fine. I now want to edit them using a browser. So I thought I would use the same form I used to add mySQL entries and change ti so that it loads in the article into the form fields depending on the id. (This is chosen through a link saying 'edit).

    Code:
    Code:
    <?php
    
    include('includes/connect.php');
    	
    //$id = $_POST['ID'];
    //$title=$_POST['title'];	
    $article=$_POST['article'];
    
    include_once('includes/nav.php');
    
    print"<div id=wrapper>";
    	
    $result = mysql_query("SELECT * FROM cmsarticles");
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    print 	
    "<div class=item><div id=gigtxt><h2>".$row{'title'}."</h2>
    <h3>".$row{'venue'}."</h3>
    <h4>".$row{'article'}."</h4>
    
    <a href=delete_article.php?id=".$row{'ID'}."&title=".$row{'title'}."><img src='img/delete.jpg' /></a>
    
    <a href=edit.php?id=".$row{'ID'}."&title=".$row{'title'}."&venue=".$row{'venue'}."&article=".$row{'article'}."><img src='img/edit.jpg' /></a></div></div>";
    }
    mysql_close();
    print"</div>";	
    ?>
    I'm trying to pass multiple variables using a URL in the form of:

    edit.php?id=46&title=34.4.56&venue=Whitley&article=ydddjadjkgdgsdjsa

    I'm only actually passing the variable;

    edit.php?id=46&title=34.4.56&venue=Whitley

    The delete button works fine BTW Smiley

    I need to add the article into the URL as well, but it isn't adding it. I wondered if there is a limit to how many variables can be passed in one URL but I have looked around and found lots of sites with very long and complicated variables. - So this can't be the problem.

    I tried re-arranging the variables to see if it was just the last one being removed for some reason. It is not. It is the 'article' variable which never works.

    I then tried adding a 'spare' variable in at the end of the url to see if this would force the 'article' variable to work, but this did not work either.

    Here is the form:

    Code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>edot3 - simpleCMS</title>
    <link href="db.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    
    <?php
    include_once('includes/nav.php');
    
    $title=$_GET{'title'};
    $venue=$_GET{'venue'};
    $article=$_GET{'article'};
    
    ?>
    
    <div id="wrapper">
    <form id="form1" name="add" method="post" action="admin/edit_article.php">
    <div class="article">  
    <div class="title">
    <h2>Edit an article</h2></div>
    
    <label><p>Title (Date)</p>
      <input type="text" name="title" value="
     <?php
      echo $title;
      ?> 
      "></input>
      </label>
    
        <label><p>Venue (Location)</p>
        <input type="text" name="venue" value="
    	<?php
      echo $venue;
      ?> 
     "></input>
        </label>
    
        <label><p>Article (Gig information)</p>
        <textarea name="article" cols=30 rows=10><?php echo $article; ?></textarea>
        </label>
    <br />
        <label>
        <input type="submit" name="submit" value="Submit" />
        </label>
      </div>
    </form>
    </div>
    </body>
    </html>
    Any help would be amazing. Any eagle-eyes out there can spot what I've done?

    Thanks

    Mark

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Try your code with a one word "title", "venue" and "article", that will probably work, if not you've got more problems!
    I think the problem lies in your edit link:
    Code:
    <a href=edit.php?id=".$row{'ID'}."&title=".$row{'title'}."&venue=".$row{'venue'}."&article=".$row{'article'}."><img src='img/edit.jpg' /></a></div></div>";
    You aren't encoding any of the variables you are putting into the url. To see the actual link you've created on your page, right click and "view source" then look for your edit link href.

    Take a look at the php function urlencode. This will probably help, however you should re-assess whether you actually need to pass all that data via the url. Can't you just pass the ID of the article and then have the edit page grab the data from the database? I think this is the normal way to do things.

    Oh and for information about url limits check here or search google for url length limit.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    OOPS
    Just spotted you haven't put your url in quotes, so any spaces will result in a truncated url!
    try this:
    Code:
    print "
    <a href=\"edit.php?id=".$row{'ID'}."&title=".$row{'title'}."&venue=".$row{'venue'}."&article=".$row{'article'}."\"><img src='img/edit.jpg' /></a></div></div>";

  • #4
    New Coder
    Join Date
    Jul 2006
    Location
    Newcastle, england.
    Posts
    38
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks sooo much. I viewed source on the edit link. I thought that was what was causing the problem myself and found that because there was a space in the 'article' the url was being cut at that point.

    If I try a one word title it works fine.

    Now I have to try and figure out how to allow multiple word titles but still pass it. Actually scratch that. I'm going to simply pass the ID and then select the data from the DB on the next page like you suggested. This must be a better method?

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    The php function, urlencode, encodes all sorts of charcaters for transmission over a url, including spaces. You should use this function whenever you create a url that includes a php variable.

    But in this case you should just pass the ID, it is much easier and more efficient.


  •  

    Posting Permissions

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