www.webdeveloper.com

View Poll Results: This thread help you any

Voters
1. You may not vote on this poll
  • Yes

    1 100.00%
  • No

    0 0%
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: [RESOLVED] View/Write/Delete To File

  1. #1
    Join Date
    Jun 2007
    Posts
    191

    resolved [RESOLVED] View/Write/Delete To File

    Ok this what I am trying to do. I have looked but can't find a good tutorial on how to do this. Here it is.

    I am building a web page that the user can access to update portions of their site. One particular spot is a scrolling text box that displays lines from a javascript file which the user does not edit the actual javascript, just the line that they want displayed. I have it set up so that the user can write to this file, but I also want the user to be able to delete or edit a line in the .js file. Basically this is how I want it set up:

    The user enters the data and submits it. When they go to a dedicated page they can see the line they just wrote along with the other lines that have already been written. Each one should display 'delete | edit'.

    Is there a way to do this?

    Thanks

  2. #2
    Join Date
    Jun 2007
    Posts
    348
    Sure, just not a real easy one. I've done this with CSV files. The .js file is a flat file -- thus, you cannot just insert or delete lines in the middle of it. Two ways -- all based on sequential line number (or a line tag which is part of the data): Read it all into memory and write it all out -- including or minus the inserted, updated, or deleted lines. Or, read a line at a time from one file and write a line at a time to a new file -- including or minus the inserted, updated, or deleted lines -- then delete the old file and rename the new file back to the old file name.

  3. #3
    Join Date
    Jun 2007
    Posts
    191
    English please

  4. #4
    Join Date
    Feb 2005
    Location
    Tauranga
    Posts
    2,062
    You could do this easily with php and a mysql database.

  5. #5
    Join Date
    Jun 2007
    Posts
    112
    That was pretty simple, actually. You could read the file (to an array) a line at a time until you found the line before or after which you wanted to add another line. Then, you'd add it to the array and continue reading. Once you reached the end of the file, you could pump it all back out to a different file, then delete the original and rename the new one to the same name.

    Why do you want to do things with Javascript, anyway?

  6. #6
    Join Date
    Jun 2006
    Location
    Down at the bottom of the garden
    Posts
    1,239
    Just read the whole file in to a multiline <textarea> then save the edited version to the file, newlines and all.

    That way you can insert lines anywhere you want by just pressing enter?

  7. #7
    Join Date
    Jun 2007
    Posts
    191
    Ok so I been reading and since everybody is saying I am going to use mysql. So now this question goes away from the original:

    When ever I write to my table it shows up as blank data. Here's my script can some help me find the problem.

    PHP Code:
    <?php
        $username 
    "username";
        
    $password "password";
        
    $database "test";

        
    $line "10";
        
    $information $_POST['description'];
            
        
    mysql_connect(localhost,$username,$password);
        @
    mysql_select_db($database) or die( "Unable to select database");

        
    $query "INSERT INTO info_scroller VALUES ('$_POST[$line]', '$_POST[$information]')";
        if (
    mysql_query($query)) {
            print 
    "successfully inserted record";
        }
        else {
          print 
    "Failed to insert record";
        }
        
    mysql_close();
    ?>

  8. #8
    Join Date
    Jun 2007
    Posts
    348
    Seems you want to change this:
    PHP Code:
    $query "INSERT INTO info_scroller VALUES ('$_POST[$line]', '$_POST[$information]')"
    to this:
    PHP Code:
    $query "INSERT INTO info_scroller VALUES ('" $_POST['line'] . "', '" $_POST['information'] . "')"
    However, for protection, you really want to scrub your form data before sticking it straight into an SQL query like that.

  9. #9
    Join Date
    Jun 2007
    Posts
    191
    What do you mean??

    and

    PHP Code:
    $query "INSERT INTO info_scroller VALUES ('" $_POST['line'] . "', '" $_POST['information'] . "')"
    didn't work.
    Last edited by monarch_684; 07-02-2007 at 02:36 PM.

  10. #10
    Join Date
    Jun 2007
    Posts
    348
    Quote Originally Posted by monarch_684
    What do you mean??

    and

    PHP Code:
    $query "INSERT INTO info_scroller VALUES ('" $_POST['line'] . "', '" $_POST['information'] . "')"
    didn't work.
    OK, based on this you posted:
    PHP Code:
        $line "10"
        
    $information $_POST['description']; 
    You would use this:
    PHP Code:
    $query "INSERT INTO info_scroller VALUES ('" $line "', '" $information "');"
    or this:
    PHP Code:
    $query "INSERT INTO info_scroller VALUES ('{$line}', '{$information});"

  11. #11
    Join Date
    Jun 2007
    Posts
    191

    Question

    Ok still not working. I even went and got my book out of the car and did it's tutorial except it has me adding the values directly in :
    PHP Code:
    <?php
            
        $connection 
    mysql_connect("localhost""username""password");
        @
    mysql_select_db("produce"$connection) or die( "Unable to select database" mysql_error());
        
        
    $query "INSERT INTO fruit (name, number) VALUES('apricots', '203')";
        
    $result mysql_query($query)
            or die (
    "Couldn't query data" mysql_error());
            
        
    $query "SELECT * FROM fruit";
        
    $result mysql_query($query)
            or die (
    "Couldn't query data" mysql_error());    
            
        echo 
    "<table border='1'>";
        echo 
    "<tr>";
        echo 
    "<th>Name</th><th>Number</th>";
        echo 
    "</tr>";
        
        while (
    $row mysql_fetch_array($result)) {
            echo 
    "<tr>";
            echo 
    "<td>"$row['name'], "</td><td>"$row['number'], "</td>";
            echo 
    "</tr>";
        }
        
        echo 
    "</table>";
        
        
    mysql_close();
    ?>
    I am wanting the values to be user selected using a form. I add this to the script:
    PHP Code:
    $name1 $_POST["fruit"];
    $number1 $_POST["fnumber"];
    .
    .
    .
    $query "INSERT INTO fruit (name, number) VALUES('$name1', '$fnumber1')"
    This you would think is the same thing but the first example adds it ok, the second adds a field but nothing there. Even adding the double quotes does not work.

  12. #12
    Join Date
    Jun 2007
    Posts
    348
    What does the HTML look like for these fields?

  13. #13
    Join Date
    Jun 2007
    Posts
    191
    HTML Code:
    <form method="post" action="rewrite.php">
            </p><input type="text" name="name1"> <input type="text" name="fnumber1"></p>
    	<p><input type="submit" value="Submit"><input type="reset" value="Reset" /></p>
    </form>

  14. #14
    Join Date
    Jun 2007
    Posts
    348
    Then this should work -- other than the lack of scrubbing:
    PHP Code:
    $name1 $_POST["fruit"]; 
    $number1 $_POST["fnumber"]; 



    $query "INSERT INTO `fruit` (`name`, `number`) VALUES('{$name1}', '{$fnumber1}');"

  15. #15
    Join Date
    Jun 2007
    Posts
    191
    Still no go. It just keeps on putting in blank data. It makes a new row and I can see the new row but no data.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles