Jump to content

Recommended Posts

Recent newbie here hoping to fill some gaps.  I have a small database, and I have built a form for inserting records.  What I need is to also build an form to update records.  I thought I understood what seems to be a simple concept, but I seem to be missing a vital piece of the puzzle.  After much searching, all I seem to find are a) the bare SQL example statements, out of context of the PHP code, or b) php code to update user related data, which gets the id from the session id.

 

Is there a good resource that can explain how to pull the data from a record into an update form?

 

Thanks!

 

 

Link to comment
https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/
Share on other sites

Perhaps I'm getting the order of all that mixed up.  Here is the code that I have so far.

 

I use this to generate the link to the update form.

echo '<td><a href="editday.php?id=' . $row['fc_id'] . '">Edit</a>';

 

And here is the update form.

  if (isset($_GET['fc_id'])) {
    // Grab the data from the GET
    $fc_id = $_GET['fc_id'];
  }

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  if (isset($_POST['submit'])) {
    // Grab the  data from the POST
    $fc_id = mysqli_real_escape_string($dbc, trim($_POST['fc_id']));
    $fc_date = mysqli_real_escape_string($dbc, trim($_POST['fc_date']));
    $fc_speakers = mysqli_real_escape_string($dbc, trim($_POST['fc_speakers']));
    $fc_text = mysqli_real_escape_string($dbc, trim($_POST['fc_text']));
    $fc_topics = mysqli_real_escape_string($dbc, trim($_POST['fc_topics']));
    $fc_refdel = mysqli_real_escape_string($dbc, trim($_POST['fc_refdel']));

    // Update the data in the database
        // Only set the picture column if there is a new picture
          $query = "UPDATE tblfedconvn SET fc_date = '$fc_date', fc_speakers = '$fc_speakers', fc_text = '$fc_text', " .
            " fc_topics = '$fc_topics', fc_refdel = '$fc_refdel' WHERE fc_id = '" . $_GET['fc_id'] . "'";
        
        mysqli_query($dbc, $query);

        // Confirm success with the user
        echo '<p>The debate information has been successfully updated.</p>';

        mysqli_close($dbc);
        exit();
    
  } // End of check for form submission
  else {
    // Grab the profile data from the database
    $query = "SELECT fc_date, fc_speakers, fc_text, fc_topics, fc_refdel, FROM tblfedconvn WHERE fc_id = '" . $_GET['fc_id'] . "'";
    $data = mysqli_query($dbc, $query);
    $row = mysqli_fetch_array($data);

    if ($row != NULL) {
      $fc_id = $row['fc_id'];
      $fc_date = $row['fc_date'];
      $fc_speakers = $row['fc_speakers'];
      $fc_text = $row['fc_text'];
      $fc_topics = $row['fc_topics'];
      $fc_refdel = $row['fc_refdel'];
    }
  }

  mysqli_close($dbc);
?>

  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
      <legend>Debate data</legend>
  <input type="hidden" name="fc_id" value="<?php echo $fc_id; ?>" />
      <label for="fc_date">First name:</label>
      <input type="text" id="fc_date" name="fc_date" value="<?php if (!empty($fc_date)) echo $fc_date; ?>" /><br />
      <label for="fc_speakers">Last name:</label>
      <input type="text" id="fc_speakers" name="fc_speakers" value="<?php if (!empty($fc_speakers)) echo $fc_speakers; ?>" /><br />
      <label for="fc_text">fc_text:</label>
      <input type="text" id="fc_text" name="fc_text" value="<?php if (!empty($fc_text)) echo $fc_text; ?>" /><br />
      <label for="fc_topics">fc_topics:</label>
      <input type="text" id="fc_topics" name="fc_topics" value="<?php if (!empty($fc_topics)) echo $fc_topics; ?>" /><br />
      <label for="fc_refdel">fc_refdel:</label>
      <input type="text" id="fc_refdel" name="fc_refdel" value="<?php if (!empty($fc_refdel)) echo $fc_refdel; ?>" /><br />
    </fieldset>
    <input type="submit" value="Save Entry" name="submit" />
  </form>

 

I am getting two error codes:

"Undefined index: fc_id in C:\wamp\www\917\editday.php on line 54"

which is

$query = "SELECT fc_date, fc_speakers, fc_text, fc_topics, fc_refdel, FROM tblfedconvn WHERE fc_id = '" . $_GET['fc_id'] . "'";

.

 

"mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\917\editday.php on line 56"

which is

    $row = mysqli_fetch_array($data);

Thanks, Drummin.  That was a nuance I missed.  That got rid of the two errors.

 

Unfortunately one of the errors popped up elsewhere, "Undefined index: fc_id in C:\wamp\www\917\editday.php on line 58,"

which is

 

$fc_id = $row['fc_id'];

 

else {
    // Grab the profile data from the database
    $query = "SELECT fc_date, fc_speakers, fc_text, fc_topics, fc_refdel FROM tblfedconvn WHERE fc_id = '" . $_GET['fc_id'] . "'";
    $data = mysqli_query($dbc, $query);
    $row = mysqli_fetch_array($data);

    if ($row != NULL) {
      $fc_id = $row['fc_id'];
      $fc_date = $row['fc_date'];
      $fc_speakers = $row['fc_speakers'];
      $fc_text = $row['fc_text'];
      $fc_topics = $row['fc_topics'];
      $fc_refdel = $row['fc_refdel'];
    }
  }
  mysqli_close($dbc);
?>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
      <legend>Debate data</legend>
  <input type="hidden" name="fc_id" value="<?php echo $fc_id; ?>" />
      <label for="fc_date">Date:</label>
      <input type="text" id="fc_date" name="fc_date" value="<?php if (!empty($fc_date)) echo $fc_date; ?>" /><br />
      <label for="fc_speakers">Speakers:</label>
      <input type="text" id="fc_speakers" name="fc_speakers" value="<?php if (!empty($fc_speakers)) echo $fc_speakers; ?>" /><br />
      <label for="fc_text">Debate text:</label>
      <input type="text" id="fc_text" name="fc_text" value="<?php if (!empty($fc_text)) echo $fc_text; ?>" /><br />
      <label for="fc_topics">Topics:</label>
      <input type="text" id="fc_topics" name="fc_topics" value="<?php if (!empty($fc_topics)) echo $fc_topics; ?>" /><br />
      <label for="fc_refdel">Delegates referenced:</label>
      <input type="text" id="fc_refdel" name="fc_refdel" value="<?php if (!empty($fc_refdel)) echo $fc_refdel; ?>" /><br />
    </fieldset>
    <input type="submit" value="Save Entry" name="submit" />
  </form>

 

I tried adding fc_id to the queries.  Then I replaced the fc_id hidden input with a text input.  but, this caused the form to disappear.

Are you getting results from your query?  You already have your GET value turned into a variable so I would use it for the query if query results are not being obtained.

$query = "SELECT fc_date, fc_speakers, fc_text, fc_topics, fc_refdel FROM tblfedconvn WHERE fc_id=$fc_id";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.