datanut Posted June 29, 2011 Share Posted June 29, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/ Share on other sites More sharing options...
xyph Posted June 29, 2011 Share Posted June 29, 2011 Select the data, populate the data into variables, echo those variables in the value attribute of the form. Store the ID in a hidden element. Post the page to an update script, that uses the hidden ID to update that specific row. Quote Link to comment https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/#findComment-1236463 Share on other sites More sharing options...
datanut Posted June 29, 2011 Author Share Posted June 29, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/#findComment-1236483 Share on other sites More sharing options...
Drummin Posted June 29, 2011 Share Posted June 29, 2011 You are using ?id not fc_id for the GET Quote Link to comment https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/#findComment-1236488 Share on other sites More sharing options...
datanut Posted June 29, 2011 Author Share Posted June 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/#findComment-1236512 Share on other sites More sharing options...
Drummin Posted June 29, 2011 Share Posted June 29, 2011 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"; Quote Link to comment https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/#findComment-1236521 Share on other sites More sharing options...
EdwinPaul Posted June 29, 2011 Share Posted June 29, 2011 If you don't SELECT your field fc_id from your table, you can't use it with $row['fc_id']. The mysql_fetch_array() fetches only the fields mentioned in the SELECT... Quote Link to comment https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/#findComment-1236554 Share on other sites More sharing options...
datanut Posted June 30, 2011 Author Share Posted June 30, 2011 Thanks a lot everyone! On a side note, the form wasn't disappearing but getting pushed way over to the right, off-screen. But, that's easy enough tame. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/240728-get-and-update-assistance/#findComment-1236656 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.