Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8085233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:03:07+00:00 2026-06-05T18:03:07+00:00

I have a basic PHP form (a few fields and 2 checkboxes, I want

  • 0

I have a basic PHP form (a few fields and 2 checkboxes, I want to add more checkboxes later, but for now I’m just using 2 for testing) in PHP. The fields and checkboxes save to a MySQL database and it works great.

I also have an ‘edit’ form that mirrors the original input form except that the values for the fields are loaded from the MySQL database based on the rows ID as is pretty standard.

The problem is that, at the moment, the last field of the edit form is just another text input field. What I would like to do is change that for checkboxes that will either be checked or unchecked based on the input from the original form.

I’m thinking there is a need to use ischecked but I’m unsure if I need to change the logic for the original form, or if I need to change the database itself (I hope not!).

The database is laid out thusly:

-------------------------------------------------------------------------------------------
| ID (int) | ARTICLEAUTHOR (varchar) | ARTICLEORGANIZATION (varchar) | ARTICLETAGS (varchar)|
 -------------------------------------------------------------------------------------------

And the code for the two forms is below.

First, the new entry form:

            // addnew.php

            <?php
             function renderForm($articletitle, $articleorganization, $articledate, $articleurl, $articletags )
            . . .
            </head>

            <body>
            . . .
                  <form id="form" name="form" action="" method="post">
                    <h1>Create a new entry in the database</h1>
                    <table width="100%" border="0" cellpadding="6">
                      <tr>
                        <td colspan="2"><legend>Article details</legend></td>
                      </tr>
                      <tr>
                        <td width="20%" align="right"><span class="field">Article Title:</span></td>
                        <td width="80%" align="left"><span class="field">
                          <input name="articletitle" type="text" value="<?php echo $articletitle; ?>" size="50"/>
                        </span></td>
                      </tr>
                      <tr>
                        <td align="right"><span class="field">Article Author:</span></td>
                        <td align="left"><span class="field">
                          <input name="articleorganization" type="text" value="<?php echo $articleorganization; ?>" size="50"/>
                        </span></td>
                      </tr>
                      <tr>
                        <td align="right"><span class="field">Article Date:</span></td>
                        <td align="left"><span class="field">
                          <input name="articledate" type="text" value="<?php echo $articledate; ?>" size="50"/>
                        </span></td>
                      </tr>
                      <tr>
                        <td align="right"><span class="field">Article URL:</span></td>
                        <td align="left"><span class="field">
                        <input name="articleurl" type="text" value="<?php echo $articleurl; ?>" size="50"/>
                        </span></td>
                      </tr>
                    </table>
            . . .
                      <input type="checkbox" name="articletags[]" value="checkbox" id="articletags_0" />
                      <input type="checkbox" name="articletags[]" value="checkbox 2" id="articletags_1" />

                      </div>
                    </fieldset>
                    <footer><input type="submit" name="submit" value="Add this Article"></footer>
            . . .
                    </form>
                </div>
              </div>
            . . .
            </body>
            </html>
            <?php 
             }

             // connect to the database
             include('settings.php');

             if(count($articletags) > 0)
            {
             $articletags_string = implode(",", $articletags);
            }
             // check if the form has been submitted. If it has, start to process the form and save it to the database
             if($_SERVER['REQUEST_METHOD'] == 'POST')
             { 
             // get form data, making sure it is valid
             $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
             $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
             $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
             $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
             $articletags = implode(',', $_POST['articletags']);

            . . .

             mysql_query("INSERT articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' ")
             or die(mysql_error()); 

             // once saved, redirect to success page
             header("Location:addsuccess.php"); 
             }
             }
             else
             // if the form hasn't been submitted, display the form
             {
             renderForm('','','','');
             }
            ?>

And now the edit form:

            <?php
             . . .
             function renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags)
             {
             ?>
            . . .
              <div class="content">
                <div id="stylized" class="myform">
                  <form id="form" name="form" action="" method="post">
                  <input type="hidden" name="id" value="<?php echo $id; ?>"/>
                    <h1>Edit Details for &nbsp; &nbsp;<?php echo $articletitle; ?></h1>
                    <table width="100%" border="0" cellpadding="6">
                      <tr align="center" valign="middle">
                        <td colspan="2"><legend>Article details</legend></td>
                      </tr>
                      <tr>
                        <td width="26%" align="right"><span class="field">Article Title</span></td>
                        <td width="74%" align="left"><span class="field">
                          <input name="articletitle" type="text" value="<?php echo $articletitle; ?>" size="50"/>
                        </span></td>
                      </tr>
                      <tr>
                        <td align="right"><span class="field">Article Author</span></td>
                        <td align="left"><span class="field">
                          <input name="articleorganization" type="text" value="<?php echo $articleorganization; ?>" size="50"/>
                        </span></td>
                      </tr>
                      <tr>
                        <td align="right"><span class="field">Article Date</span></td>
                        <td align="left"><span class="field">
                          <input name="articledate" type="text" value="<?php echo $articledate; ?>" size="50"/>
                        </span></td>
                      </tr>
                      <tr>
                        <td align="right"><span class="field">Article Url:</span></td>
                        <td align="left"><span class="field">
                          <input name="articleurl" type="text" value="<?php echo $articleurl; ?>" size="50"/>
                        </span></td>
                      </tr>
                      <tr>
                        <td align="right"><span class="field">Article Tags:</span></td>
                        <td align="left"><span class="field">
                          <input name="articletags" type="text" value="<?php echo $articletags; ?>" size="50"/>
                        </span></td>
                      </tr>
                      <tr align="center" valign="middle">
                        <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
                      </tr>
                    </table>
             . . .
                    </fieldset>
                    <footer></footer></form>
                </div>
              </div>
              <div class="footer">
            . . .
                <!-- end .footer --></div>
            </body>
            </html>
            <?php
             }



             // connect to the database
             include('settings.php');

             // check if the form has been submitted. If it has, process the form and save it to the database
             if (isset($_POST['submit']))
             { 
             // confirm that the 'id' value is a valid integer before getting the form data
             if (is_numeric($_POST['id']))
             {
             // get form data, making sure it is valid
             $id = $_POST['id'];
             $articletitle = mysql_real_escape_string(htmlspecialchars($_POST['articletitle']));
             $articleorganization = mysql_real_escape_string(htmlspecialchars($_POST['articleorganization']));
             $articledate = mysql_real_escape_string(htmlspecialchars($_POST['articledate']));
             $articleurl = mysql_real_escape_string(htmlspecialchars($_POST['articleurl']));
             $articletags = mysql_real_escape_string(htmlspecialchars($_POST['articletags']));

             . . .

             mysql_query("UPDATE articles SET articletitle='$articletitle', articleorganization='$articleorganization', articledate='$articledate', articleurl='$articleurl', articletags='$articletags' WHERE id=$id")
             or die(mysql_error()); 

            . . .
             {

             // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
             if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
             {
             // query db
             $id = $_GET['id'];
             $result = mysql_query("SELECT * FROM articles WHERE id=$id")
             or die(mysql_error()); 
             $row = mysql_fetch_array($result);

             // check that the 'id' matches up with a row in the databse
             if($row)
             {

             // get data from db
             $articletitle = $row['articletitle'];
             $articleorganization = $row['articleorganization'];
             $articledate = $row['articledate'];
             $articleurl = $row['articleurl'];
             $articletags = $row['articletags'];

             // show form
             renderForm($id, $articletitle, $articleorganization, $articledate, $articleurl, $articletags, '');
             }
            . . .
            ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:03:08+00:00Added an answer on June 5, 2026 at 6:03 pm

    You would be better off having two new tables to track tags.

    Your article table would lose the tags field

    Tags table with the following structure:

    Tags Table
    -----
    id (int)
    tagname varchar(64)
    

    ArticleHasTags crossmap table with the following structure

    ArticleHasTags
    ----
    article_id (int)
    tag_id (int)
    

    Then you build checkboxes based on the tags table entries. When storing an article, you are also going to add entries to the ArticleHasTags table based on the checkboxes in the entry form.

    When displaying the edit form, you pull the data from both the Article table and the ArticleHasTags table, either with multiple joins or it might be easier to do a second select:

    select * from ArticleHasTags where article_id = $id 
    

    (where $id is the id of the article you are editing)

    You then pull all the rows from the Tags database again to show your checkboxes. As you show each checkbox you can check when the article has that tag by checking for Tag id against your retrieved ArticleHasTag results using in_array().

    I can go into greater depth, however I would judge that it will be of more value to you in the long term to use these pointers and figure the details out yourself 😉

    This is all very procedural, but without being patronizing, this looks like the level you’re at with your existing code. Be encouraged and keep learning! You’ve come to the right spot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a real basic form (code below) with a bunch of back-panel PhP.
My knowledge of xml and php is somewhat basic but have been able to
Pretty basic programming question, I know PHP have a function for it, but does
I have a basic form consisting of input fields as well as a file
I have a basic html form that adds some data to the page using
I have a basic form that I am using as a dummy to test
I have the basic html form echoed through php: <html> <body> <?php if (isset($_GET[pk]))
Hey all, I have a basic HTML contact form that goes to a .php
I'm using PHP for my server side language and have a form that returns
I have a basic PHP form here that sends an email when submitted: <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.