SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Getting data from db into form field.

    I am currently working with an article manager. I am in middle of it.

    Currently the sql connection are open and I have some data stored in the database table field called subject.

    The data was sent to the db using the same article manager.

    NOW I AM MODIFYING THE ALREADY EXISTING DATA IN DB; in doing so I want to extract the data existing in the database to get printed in the form field.

    Code:
    <input id="subject" name="subject" type="text" size="45" value=
    "<?php echo stripslashes($_GET['subject']);?>" />
    When I write the code above, i donot get any data printed in the form. Any solution.

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Location
    Bel Air, Maryland.
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Um the $_GET method is going to grab a var from your URL, typically submitted via a form from the previous page. If your looking to extract data from the DB field you'll need to query your DB and use something like mysql_fetch_array() to put the data from that row into a variable. Then you echo the corresponding array value (like $rowinfo['subject']) into your input field value.

    Is this at all close to what your trying for?
    ---
    Paul S. Smith
    technetic | design & code

  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Quote Originally Posted by technetic View Post
    Um the $_GET method is going to grab a var from your URL, typically submitted via a form from the previous page. If your looking to extract data from the DB field you'll need to query your DB and use something like mysql_fetch_array() to put the data from that row into a variable. Then you echo the corresponding array value (like $rowinfo['subject']) into your input field value.
    Is this at all close to what your trying for?

    I did understand what you mean, but i am not able to implement it my database is mysql, the table is news and the field value which is required is "subject"

    can you post the code integrating with
    Code:
    <?php
    mysql_connect("localhost", "mysql_user", "mysql_password") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");
    
    $result = mysql_query("SELECT id, name FROM mytable");
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        printf("ID: %s  Name: %s", $row["id"], $row["name"]);
    }
    
    mysql_free_result($result);
    ?>
    I just want the data in the subject field in db to get printed in the form
    can you write bit of the code that will work... is there any other way rather than using mysql_fetch_array()

    any simpler solution???

  4. #4
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres a bit of a script I wrote recently to change articles. Change this for how you need it.

    PHP Code:
    if (isset($_POST['content'])) {
    // data has been sent

        // get data and make it safe for query
        $id = mysql_real_escape_string($_POST['id']);
        $article_name = mysql_real_escape_string($_POST['article_name']);
        $content = mysql_real_escape_string($_POST['content']);
        $image = mysql_real_escape_string($_POST['image']);
        $url = mysql_real_escape_string($_POST['url']);
        $category = mysql_real_escape_string($_POST['category']);
        $popular = mysql_real_escape_string($_POST['popular']);
        $meta_keywords = mysql_real_escape_string($_POST['meta_keywords']);

        // query to edit opening
        $edit = "UPDATE articles SET
        article_name = '$article_name',
        content = '$content',
        image = '$image',
        url = '$url',
        category = '$category',
        popular = '$popular',
        meta_keywords = '$meta_keywords'
        WHERE id = $id";
        $result = mysql_query($edit) or die (mysql_error());
        
        // did it work?
        if ($result) {
            echo 'Article editted succesfully<br /><br />';
            echo 'Go to <a href="/articles/'.$url.'">article</a>?';
        } else {
            echo 'Error! The values sent are below.<br /><br />';
            echo $content;
        }

    } else if (isset($_GET['id'])) {

        $edit = "SELECT * FROM articles WHERE id = {$_GET['id']}";
        $result = mysql_query($edit) or die (mysql_error());
        $row = mysql_fetch_array($result);
        ?>

        <form method="post" action="edit-article.php">
        <div>
        
        <b>Title:</b><br /><input type="text" name="article_name" value="<?php echo $row['article_name'?>" /><br /><br />

        <b>Content:</b><br /><textarea name="content" cols="60" rows="20"><?php echo $row['content'?></textarea><br /><br />

        <b>Image:</b><br /><input type="text" name="image" value="<?php echo $row['image'?>" /><br /><br />

        <?php
        
    if (!empty($row['image'])) {
        echo 
    '<img src="/content-images/articles/'urlencode($row['image']) .'" alt="" class="imgcenter" />
        <br /><br />'
    ;
        }
        
    ?>

        <b>URL:</b><br /><input type="text" name="url" value="<?php echo $row['url'?>" /><br /><br />
        
        <b>Category:</b><br /><input type="text" name="category" value="<?php echo $row['category'?>" /><br /><br />

        <b>Popular (<?php echo $row['views']; ?> views):</b><br />
        <select name="popular" style="width:100px;">
        <option value="1"<?php echo ($row['popular'] == ' selected="selected"' ''); ?>>Yes</option>
        <option value="0"<?php echo ($row['popular'] == ' selected="selected"' ''); ?>>No</option>
        </select><br /><br />
        
        <b>Meta Keywords:</b><br /><input type="text" name="meta_keywords" value="<?php echo $row['meta_keywords'?>" /><br /><br />

        <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
        <input type="submit" value="Edit" />

        </div>
        </form>
        <?php

    } else {
    // show all articles

        
    $sql "SELECT id, article_name, views, popular FROM articles ORDER BY views DESC";
        
    $result mysql_query($sql) or die (mysql_error());

        
    // display each child
        
    while ($row mysql_fetch_array($result)) {
            echo 
    '
            <a href="edit-article.php?id='
    .$row['id'].'">'.$row['article_name'].'</a> ('.$row['views'].' views';
            echo (
    $row['popular'] == 1) ? ', popular' '';
            echo 
    ')<br />';
        }

    }
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Quote Originally Posted by AlienDev View Post
    Heres a bit of a script I wrote recently to change articles. Change this for how you need it.

    PHP Code:
    if (isset($_POST['content'])) {
    // data has been sent

        // get data and make it safe for query
        $id = mysql_real_escape_string($_POST['id']);
        $article_name = mysql_real_escape_string($_POST['article_name']);
        $content = mysql_real_escape_string($_POST['content']);
        $image = mysql_real_escape_string($_POST['image']);
        $url = mysql_real_escape_string($_POST['url']);
        $category = mysql_real_escape_string($_POST['category']);
        $popular = mysql_real_escape_string($_POST['popular']);
        $meta_keywords = mysql_real_escape_string($_POST['meta_keywords']);

        // query to edit opening
        $edit = "UPDATE articles SET
        article_name = '$article_name',
        content = '$content',
        image = '$image',
        url = '$url',
        category = '$category',
        popular = '$popular',
        meta_keywords = '$meta_keywords'
        WHERE id = $id";
        $result = mysql_query($edit) or die (mysql_error());
        
        // did it work?
        if ($result) {
            echo 'Article editted succesfully<br /><br />';
            echo 'Go to <a href="/articles/'.$url.'">article</a>?';
        } else {
            echo 'Error! The values sent are below.<br /><br />';
            echo $content;
        }

    } else if (isset($_GET['id'])) {

        $edit = "SELECT * FROM articles WHERE id = {$_GET['id']}";
        $result = mysql_query($edit) or die (mysql_error());
        $row = mysql_fetch_array($result);
        ?>

        <form method="post" action="edit-article.php">
        <div>
        
        <b>Title:</b><br /><input type="text" name="article_name" value="<?php echo $row['article_name'?>" /><br /><br />

        <b>Content:</b><br /><textarea name="content" cols="60" rows="20"><?php echo $row['content'?></textarea><br /><br />

        <b>Image:</b><br /><input type="text" name="image" value="<?php echo $row['image'?>" /><br /><br />

        <?php
        
    if (!empty($row['image'])) {
        echo 
    '<img src="/content-images/articles/'urlencode($row['image']) .'" alt="" class="imgcenter" />
        <br /><br />'
    ;
        }
        
    ?>

        <b>URL:</b><br /><input type="text" name="url" value="<?php echo $row['url'?>" /><br /><br />
        
        <b>Category:</b><br /><input type="text" name="category" value="<?php echo $row['category'?>" /><br /><br />

        <b>Popular (<?php echo $row['views']; ?> views):</b><br />
        <select name="popular" style="width:100px;">
        <option value="1"<?php echo ($row['popular'] == ' selected="selected"' ''); ?>>Yes</option>
        <option value="0"<?php echo ($row['popular'] == ' selected="selected"' ''); ?>>No</option>
        </select><br /><br />
        
        <b>Meta Keywords:</b><br /><input type="text" name="meta_keywords" value="<?php echo $row['meta_keywords'?>" /><br /><br />

        <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
        <input type="submit" value="Edit" />

        </div>
        </form>
        <?php

    } else {
    // show all articles

        
    $sql "SELECT id, article_name, views, popular FROM articles ORDER BY views DESC";
        
    $result mysql_query($sql) or die (mysql_error());

        
    // display each child
        
    while ($row mysql_fetch_array($result)) {
            echo 
    '
            <a href="edit-article.php?id='
    .$row['id'].'">'.$row['article_name'].'</a> ('.$row['views'].' views';
            echo (
    $row['popular'] == 1) ? ', popular' '';
            echo 
    ')<br />';
        }

    }
    Are you showing the existing content in the db before you edit the article. if yes that is what i what. How???
    I mean if i have a subject field in db having data "The is the subject"
    Now if you are editing the subject then the existing subject "The is the subject" should be printed on the screen.
    How to do it ??

  6. #6
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why bother making a website if you refuse to learn and try to fix your own problems? I gave you code that took me a while to write so you can use it to learn from.

    People arent going to do all your work.
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by AlienDev View Post
    Why bother making a website if you refuse to learn and try to fix your own problems? I gave you code that took me a while to write so you can use it to learn from.

    People arent going to do all your work.
    I never wanted to hurt your feelings... sorry if i did..

    The article you wrote gets the data which has just been posted using a form .
    However i want the data which already exists in db to to extracted and shown in the form field..

    For example wen u edit anything on this forum you get the data which is already in the db of the forum available in the form... Understand what i exactly want

  8. #8
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats exactly what mine does.

    Please actually look at the script fully.
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  9. #9
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    krishnakhanna, listen to elders

    AlienDev gave you whole script, all what you would ever need to be able to edit your articles.

    This little piece of code does that:
    PHP Code:
    } else if (isset($_GET['id'])) {

        
    $edit "SELECT * FROM articles WHERE id = {$_GET['id']}";
        
    $result mysql_query($edit) or die (mysql_error());
        
    $row mysql_fetch_array($result); 
    you have to indicate ID of your article by, for example, clicking its name from the list of all articles. $_GET variable called 'id' gets its value when the site reloads, and then script connects to the DB and grabs all info related to that very article: $edit = "SELECT * FROM articles WHERE id = {$_GET['id']}";

    All you have to do then, is just print out variables within text field.
    PHP Code:
    <input type="text" name="article_name" value="<?php echo $row['article_name'?>">
    And voila!



    My Q to AlienDev:
    while selecting data from DB I always use this syntax:
    $edit = "SELECT * FROM articles WHERE id = '" . $_GET['id'] . "'";
    but you closed the variable within curly braces:
    $edit = "SELECT * FROM articles WHERE id = {$_GET['id']}";

    what is the difference between single quotes ( ' ' ) and curly braces ( {} )? Any significant difference?

    Thanks!

    ~g.

  10. #10
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Location
    Bel Air, Maryland.
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes the script from AlienDev does this exactly. Once you get the data from the row into the variable ($row) then simple print the html for the input and echo the subject value ($row['subject']) inside like this:

    PHP Code:
    echo "<b>SUBJECT:</b><br /> <textarea name="subject" cols="60" rows="20">{$row['subject']}</textarea>"
    ---
    Paul S. Smith
    technetic | design & code

  11. #11
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In sql, for example...
    Code:
    feild = $value
    ...this will work if $value is a number. But if $value is a string of text then there will be an error. Eg...
    Code:
    feild = 1
    this will work
    
    feild = a string of text
    this wont work
    
    feild = 'a string of text'
    this will work
    Strings of text have to be wrapped in ' ' in sql but numbers dont.

    The {} brackets arent sql, they are for PHP to tell whether or not the text wrapped in {} is a variable or not.

    PHP Code:
    <?php

    $value
    ['example'] = 'bla';

    echo 
    "iuhawefuih$value['example']iasdu32r";
    // php will get confused here and wont know whether or not to
    // output the value of $value['example']

    echo "iuhawefuih{$value['example']}iasdu32r";
    // the {} force php to parce whatever is inside the {} as a variable
    // so here it wont get confused.
    Hope that explains it!
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  12. #12
    SitePoint Addict greg76's Avatar
    Join Date
    Aug 2004
    Location
    Poland
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    gotcha!

    I was actually using this other method to tell PHP what is a var and what is not, using either single or double quotes and always breaking the string to add a var:

    <?php echo 'This is some text and this is a nice var ' . $var . ', the end'; ?>

    Thanks for the explanation, gonna try braces, too

    cheers,
    Greg


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
  •