Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Edit Mysql Table With A Form

  • This topic is empty.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #161674
    Anonymous
    Inactive

    I want to edit mysql data using a contact form. When the user clicks edit on the specific post they want to change, it takes them to a form that has the post values in the input fields, and they can change whatever they want about the post. I already have this part working but now i need the change to actually take effect once the user clicks submit. How can i accomplish this?

    Here is the PHP that displays all the text from the blog post in mysql into the input fields when the user clicks edit.

    <?php
                include "../php/db_connect.php";
    
                if (isset($_GET['id'])) {
    
                    $id = $_GET['id'];
    
                    $sql = mysql_query("SELECT * FROM articles WHERE id='$id'");
                    while($row = mysql_fetch_array($sql)) {
                        $id = $row['id'];
                        $title = $row['title'];
                        $content = $row['content'];
                        $written_by = $row['written_by'];
                        $category = $row['category'];
                    }
    
                ?>
    
            <div class="content_wrapper">
                <h1 class="content_heading">Edit A Blog Post</h1>
                <form method="post" action="php/edit_article_process.php" enctype="multipart/form-data" id="upload_article_form">
                    <span>Title</span>
                    <input type="text" name="title" class="input" value="<?php echo $title; ?>"/>
                    <span>Article</span>
                    <textarea rows="20" name="ckeditor1" class="input" style="resize:none;"><?php echo $content ?></textarea>
                    <span>Author</span>
                    <input type="text" name="written_by" class="input short_input"  value="<?php echo $written_by; ?>"/>
                    <span>Category - <i>Spell it right nigga</i></li></span>
                    <input type="text" name="category" class="input short_input"  value="<?php echo $category?>"/>
                    <input type="submit" name="update" class="input submit_button"/>
                </form>
    
                <?php } ?>
    
            </div>
    

    Now all i need is for the change to take effect when the user clicks submit.

    #161764
    Anonymous
    Inactive

    Still looking for a solution :P

    #161786
    Anonymous
    Inactive

    In my last attempt i tried this code. I think its something like that but there is something wrong with it.

            <?php
                include "../php/db_connect.php";
    
                                // show values for the form inputs
                if (isset($_GET['id'])) {
    
                    $id = $_GET['id'];
    
                    $sql = mysql_query("SELECT * FROM articles WHERE id='$id'");
                    while($row = mysql_fetch_array($sql)) {
                        $id = $row['id'];
                        $title = $row['title'];
                        $content = $row['content'];
                        $written_by = $row['written_by'];
                        $category = $row['category'];
                }
    
                            // update 
                if(isset($_POST['submit'])) {
                    $id = $_GET['id'];
                    $title = $_POST['title'];
                    $content = mysql_real_escape_string($_POST['ckeditor1']);
                    $written_by = $_POST['written_by'];
                    $category = $_POST['category'];
    
                    $update = mysql_query(" UPDATE articles SET title='$title', content='$content', written_by='$written_by', category='$category' WHERE id='$id'");
    
                    mysql_query($update) or die(mysql_error());
                }
    
    
            ?>
    
            <div class="content_wrapper">
                <h1 class="content_heading">Edit A Blog Post</h1>
                <form method="post" action="edit_article_form.php" enctype="multipart/form-data" id="upload_article_form">
                    <span>Title</span>
                    <input type="text" name="title" class="input" value="<?php echo $title; ?>"/>
                    <span>Article</span>
                    <textarea rows="20" name="ckeditor1" class="input" style="resize:none;"><?php echo $content ?></textarea>
                    <span>Author</span>
                    <input type="text" name="written_by" class="input short_input"  value="<?php echo $written_by; ?>"/>
                    <span>Category - <i>Spell it right nigga</i></li></span>
                    <input type="text" name="category" class="input short_input"  value="<?php echo $category?>"/>
                    <input type="submit" name="submit" class="input submit_button"/>
                </form>
    
                <?php } ?>
    
            </div>
    
    #161787
    __
    Participant

    I posted in this thread last night… wonder what happened. Let me follow up on it

    #161808
    chrisburton
    Participant

    but there is something wrong with it

    Can you elaborate? Is it producing errors?

    #161810
    Anonymous
    Inactive

    Not at all. There aren’t any errors, yet its not updating, i echoed “hello” incase the form wasn’t linking the page but it is.

    #161863
    __
    Participant

    My answers still aren’t appearing. See here instead.

    #161868
    Anonymous
    Inactive

    Thats weird. I saw you replied a few days ago but there wasn’t a post. You should contact Chris about this. And thank you i’m looking at it now.

    #161869
    Alen
    Participant

    This is an ongoing issue, I’ve replied to few discussions yesterday and soon gave up after many posts were not showing up.

    #161871
    chrisburton
    Participant

    Not to hijack but when I submit a reply, it takes almost, sometimes over, a minute for it to post.

    #161878
    Alen
    Participant

    Not to hijack but when I submit a reply, it takes almost, sometimes over, a minute for it to post.

    Same here.

    I think it might be time to ditch WordPress…

    #161881
    __
    Participant

    Not to hijack but when I submit a reply, it takes almost, sometimes over, a minute for it to post.

    In this case, my post did appear (at least, it appeared on my screen). I even checked back about a half-hour later. But it’s gone now… : )

    Thats weird. I saw you replied a few days ago but there wasn’t a post. You should contact Chris about this.

    As @Alen said, it’s an ongoing issue. It was worse when the new forums came up, then it seemed to get better, then it came up again, and so on. I actually suspect it might be some sort of spam filter that people haven’t quite figured out how to manage yet (I really have nothing to support that idea, but it does seem to happen more often with longer posts/more links/after edits).

    #161696
    __
    Participant

    First:

    $id = $_GET['id'];
    $sql = mysql_query("SELECT * FROM articles WHERE id='$id'");
    

    This code is vulnerable to SQL injection and/or simple SQL syntax errors. Best case, your script crashes; worst, someone else owns your server.

    You must never trust user input – always validate/sanitize all data that comes from the user. For example, if you expect $_GET["id"] to be an integer, you should make sure it is before putting it into your SQL statement:

    if( ! ctype_digit( $_GET["id"] ) ){
        /* BAD VALUE - don't use it */
        $id = NULL;
        /* no SQL query */
    }
    else{
        /* good. */
        $id = $_GET["id"];
        /* SQL query */
    }
    

    Better yet, use prepared statements (this link is for PDO, but MySQLi can use them too). More info about SQL Injection attacks in PHP.

    Second:

    Do not use the mysql_* functions. They are deprecated and should not be used if at all possible. Especially if you are just learning, you should look at MySQLi or PDO instead.

    More info about choosing a MySQL api.

    Third:

    while($row = mysql_fetch_array($sql)) {
        $id = $row['id'];
        $title = $row['title'];
        $content = $row['content'];
        $written_by = $row['written_by'];
        $category = $row['category'];
    }
    

    Fourth:

    The way you are currently writing this, anyone is allowed to edit anything. If that’s not what you intended, you will need to authenticate and authorize your users before accepting their form submissions.

    Do you expect more than one row to be returned?

    If so, be aware that you will only print the data from the last row (because your variables are overwritten on each loop).

    If not, why are you using a loop at all?

    And finally,

    I am not clear as to what your actual question is. Can you explain further? What are you trying to accomplish next? What problems did you encounter?

Viewing 13 posts - 1 through 13 (of 13 total)
  • The forum ‘Back End’ is closed to new topics and replies.