SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    these two scripts wont communicate

    Hi,

    I have these two scripts. Basically the first one prints out a list of blog titles with a link through to another page which will enable the user to edit the blog post. The first script is here and works fine:

    PHP Code:
    <?php include ('header.inc');
    ?>
    <ul>
        

    <?php
    require_once ('mysql_connect.php');

      
    $sql "SELECT * FROM entries ORDER BY date_submitted";
      
    $sqlresult mysql_query($sql);
      while (
    $row mysql_fetch_array($sqlresult)){
    ?>


     <li><a class="body" href="editentry.php?blog_id=<?php echo $row['blog_id']; ?>"><?php echo $row['title']; ?></a></li>
     <?php 
      
    }

     include (
    'footer.inc');
    ?>
    </ul>
    Now the second script is using the GET variable at the top of the page to find out which blog we're editing. I cant spot a problem with the code but i get this error:

    [18-Jun-2006 22:40:55] PHP Parse error: syntax error, unexpected $end in /Applications/MAMP/htdocs/editentry.php on line 76

    Anyways heres the code. Im new to php but have spent hours trying to debug to no avail. Perhaps my method of using the id in the url and using GET to, well get the id is not the correct method

    PHP Code:
    <?php 
    include ('header.inc');

    if(isset(
    $_GET['blog_id'])) {
      
    $blog_id $_GET['blog_id'];
    }
    else { 
      
    $blog_id NULL;
      echo 
    'Please specify a section';
    }

      
      
    $sql "SELECT * FROM entries WHERE blog_id = $blog_id";
      
    $result mysql_query($sql);
      
    $row mysql_fetch_array($result);
      while(
    $row) {
      
    $title $row['title'];
      
    $body $row['body'];


    ?>
    <div id="main">
        


    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?blog_id=<?php echo $blog_id?>" name="input" method="post">
                         <input type="text" size="50" name="title" maxlength="200" value="<?php echo $title?>" />
                         <input type="text" size="50" name="body" maxlength="200" value="<?php echo $body?>"/>
                         <input class="bodyform" type="submit" name="submitedit" value="SUBMIT"/>
    </form>

    <?php
      
    }
      
    ?>
      <?php

    if (isset($_POST['submitedit'])) {
    //form validation

    if (strlen($_POST ['title']) > 0) {
             
    $title $_POST ['title'];
           }
        else { 
            
    $title NULL;
        }
           
        if (
    strlen($_POST['body']) > 0) {
             
    $body $_POST['body'];
           }
        else { 
            
    $body NULL;
           }
    if(
    $title && $body) {
        
    //update the database with edited data
        
    $sql "UPDATE entries SET title = $title, body = $body WHERE blog_id = $blog_id";
      
    $result mysql_query($sql);

     
    }


    else { echo 
    'please fill out form fields';
    }


    include (
    'footer.inc');
    ?>
    any ideas?!?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,185
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    unexpected end

    Missing a closing } ??

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also, this is an infinite loop
    PHP Code:
    while($row) { 

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI guys,

    Thanks for the replies...i forgot to mention im a complete newb when it comes to programming.

    Yes the closing bracket fixed it and then the code worked but as was pointed out the while was an infinite loop and crashed my browser! although im not 100&#37; sure why it is an infinite loop. Is it becuase $row['title'] is always going to be true?

    Anyways so i did this:
    PHP Code:
    <?php 
    include ('header.inc');

    require_once (
    'mysql_connect.php');

    if(isset(
    $_GET['blog_id'])) {
      
    $blog_id $_GET['blog_id'];
    }


    else { 
      
    $blog_id NULL;
      echo 
    'Please specify a section';
    }



      
      
    $sql "SELECT title, body FROM entries WHERE blog_id = $blog_id";
     
      
    $result mysql_query($sql);
      
    $row mysql_fetch_array($result);
       
      
      
    $title $row['title'];
      
    $body $row['body'];


    ?>

        


    <form action="<?php echo $_SERVER['PHP_SELF']; ?>?blog_id=<?php echo $blog_id?>" name="input" method="post">
                         <input type="text" size="50" name="title" maxlength="200" value="<?php echo $title?>" />
                         <input type="text" size="50" name="body" maxlength="200" value="<?php echo $body?>"/>
                         <input class="bodyform" type="submit" name="submitedit" value="SUBMIT"/>
    </form>

    <?php
     
       
      ?>
      <?php

    if (isset($_POST['submitedit'])) {
    //form validation

    if (strlen($_POST ['title']) > 0) {
             
    $title $_POST ['title'];
           }
        else { 
            
    $title NULL;
        }
           
        if (
    strlen($_POST['body']) > 0) {
             
    $body $_POST['body'];
           }
        else { 
            
    $body NULL;
           }
    if(
    $title && $body) {
        
    //update the database with edited data
        
    $formdata"title = '$title', body = '$body'";
        
    $sql "UPDATE entries SET $formdata WHERE entries.blog_id = $blog_id";
      echo 
    $sql;
      
    $result mysql_query($sql);

     
    }


    else { echo 
    'please fill out form fields';
    }

    }


    include (
    'footer.inc');
    ?>
    Now this works and the database is updated if a user edits the copy. The problem with it though is that if a user edits the text and clicks submit the info in the fields changes back to what it was before it was edited. You have to click refresh before you can see the changes. I cant work this out! any ideas?

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    try this:
    PHP Code:
    <?php 
    include ('header.inc');
    require_once (
    'mysql_connect.php');
    if(isset(
    $_GET['blog_id'])) {
    $blog_id $_GET['blog_id'];
    }
    else { 
    $blog_id NULL;
    echo 
    'Please specify a section';
    }


      
      
    $sql "SELECT title, body FROM entries WHERE blog_id = $blog_id";

      
    $result mysql_query($sql);
      
    $row mysql_fetch_array($result);
       
      
      
    $title $row['title'];
      
    $body $row['body'];


    ?>

        


            <form name="input" method="post">
                         <input type="text" size="50" name="title" maxlength="200" value="<?php echo $title?>" />
                         <input type="text" size="50" name="body" maxlength="200" value="<?php echo $body?>"/>
                         <input type="hidden" name="blog_id" value="<?php echo $blog_id?>">
                 <input class="bodyform" type="submit" name="submitedit" value="SUBMIT"/>
    </form>

    <?php

       
      ?>
      <?php

    if (isset($_POST['submitedit'])) {
    //form validation
    if (isset($_POST['blog_id'])){
        
    $blog_id $_POST['blog_id'];
    }else{
    die(
    "Blog not selected");
    }

    if (
    strlen($_POST ['title']) > 0) {
             
    $title $_POST ['title'];
           }
        else { 
            
    $title NULL;
        }
           
        if (
    strlen($_POST['body']) > 0) {
             
    $body $_POST['body'];
           }
        else { 
            
    $body NULL;
           }
    if(
    $title && $body) {
        
    //update the database with edited data
        
    $formdata"`title` = '$title', `body` = '$body'";
        
    $sql "UPDATE `entries` SET $formdata WHERE blog_id = `".$blog_id."`";
      echo 
    $sql;
      
    $result mysql_query($sql);


    }


    else { echo 
    'please fill out form fields';
    }

    }


    include (
    'footer.inc');
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there,

    Thanks for the reply. Unfortunately it doesnt work. The datbase gets updated with the edited info but the screen shown after the submit button is pressed still shows the text before it was edited. The difference with your code is that when you refresh the page the text doesnt update to the edited version as my code before was.

    any ideas?

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    what happens if you refresh the page?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    try this:
    PHP Code:
    <?php 
    include ('header.inc'); 
    require_once (
    'mysql_connect.php'); 
    <?
    php 
    if (isset($_POST['submitedit'])) { 
    //form validation 
    if (isset($_POST['blog_id'])){ 
        
    $blog_id $_POST['blog_id']; 
    }else{ 
    die(
    "Blog not selected"); 


    if (
    strlen($_POST ['title']) > 0) { 
             
    $title $_POST ['title']; 
           } 
        else { 
            
    $title NULL
        } 
            
        if (
    strlen($_POST['body']) > 0) { 
             
    $body $_POST['body']; 
           } 
        else { 
            
    $body NULL
           } 
    if(
    $title && $body) { 
        
    //update the database with edited data 
        
    $formdata"`title` = '$title', `body` = '$body'"
        
    $sql "UPDATE `entries` SET $formdata WHERE blog_id = `".$blog_id."`"
      echo 
    $sql
      
    $result mysql_query($sql); 





    else { echo 
    'please fill out form fields'



    if(isset(
    $_GET['blog_id'])) { 
    $blog_id $_GET['blog_id']; 

    else { 
    $blog_id NULL
    echo 
    'Please specify a section'



       
      
    $sql "SELECT title, body FROM entries WHERE blog_id = $blog_id"

      
    $result mysql_query($sql); 
      
    $row mysql_fetch_array($result); 
        
       
      
    $title $row['title']; 
      
    $body $row['body']; 


    ?> 

         


            <form name="input" method="post"> 
                         <input type="text" size="50" name="title" maxlength="200" value="<?php echo $title?>" /> 
                         <input type="text" size="50" name="body" maxlength="200" value="<?php echo $body?>"/> 
                         <input type="hidden" name="blog_id" value="<?php echo $blog_id?>"> 
                 <input class="bodyform" type="submit" name="submitedit" value="SUBMIT"/> 
    </form> 
    include ('footer.inc'); 
    ?>
    It seems that you are updating the database after the page loads, but with the above code it will update the database before, making everything right!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, no i thought that was going to work when i tried it myself but it didnt. Ive also noticed that it also doesnt input i the database with this script.

    When you hit refresh in my original script the field would update to be correct. I just cant figure a way to get it to do it correctly

    I have tried putting <?php echo $row['title'] ?> in the value if thie field to see if that would work but it didnt.


Bookmarks

Posting Permissions

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