Jump to content

Recommended Posts

Hi there, I'm doing a project for my own personal website and trying to learn along the way a bit of PHP and MySQL. So far what I have is a edit page (edit/index.php) and a submit page (edit/submit.php). The edit page right now takes all of the data from the user's input in the forms and places it into the MySQL database, and the submit page uploads all the data into the MySQL database, everything works great.

 

On the top of the edit page I have a dropdown box that is populated by the titles of the blog entries, and a button next to it that saying edit.

 

What I would like it to do is based on the title that you select from the drop down box, when you click on edit it will populate the textboxes, textarea and checkboxes, and when you click submit on the bottom of the page, it will update the necessary information into the mysql database.

 

index.php:

<?php
  //Header information
  include("../include/header.php");
?>

<div id="row1">
<div id="generalarea">

<form action="submit.php" method="post">
Did you want to edit something?: 
<select>
<?
  //connect to database
  mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());
  mysql_select_db($database) or die("ERROR DB:".mysql_error());
  
  //query the database
  $query=("select * from blog order by date desc");
  $result=mysql_query($query) or die (mysql_error() );
  
  //fetch the array title 
  while($row=mysql_fetch_array($result)){
echo "<option value=".$row['id'].">".$row['title']."</option>";
  }
?>
</select>
<input type="button" value="Edit">
<br /><br />
Author: <input type="text" name="author" size="25" value="<?php echo $result["author"]; ?>"><br />
Title: <input type="text" name="title" size="25" value="<?php echo $result["title"]; ?>"><br />
Content: <textarea rows="20" cols="100" name="content" value="<?php echo $result["content"]; ?>"></textarea><br />
<u>Categories:</u><br />
Music: <input type="checkbox" name="categories[]" value="Music"> 
Technology: <input type="checkbox" name="categories[]" value="Technology"> 
Movies: <input type="checkbox" name="categories[]" value="Movies"><br /><br />
Disclude from categories: <input type="checkbox" name = "disclude_categories[]" value="disclude_categories"> *Set to yes if your content has large images/videos*<br />
Insert in The Loop: <input type="checkbox" name = "theloop[]" value="theloop"><br />
<input type="submit" value="Submit">
<input type="submit" value="Delete">
</form>





</div>
</div>

<?php
  include("../include/footer.php");
?>

 

submit.php:

<?php


  //Header information
  include("../include/header.php");
  
  //Set variables from form data on edit/index.php
  $date = date('Y-m-d H:i:s');
  $author = $_POST['author'];
  $title = $_POST['title'];
  $disclude_categories = $_POST['disclude_categories'];
  $theloop = $_POST['theloop'];
  $content = $_POST['content'];
  $edited_content = nl2br($content); 			//PHP function to convert line breaks to <br />
  /* Start category checkbox to category string conversion */
  $category_check = $_POST['categories'];
  foreach ($category_check as $category) {      //Foreach loop for every element in the array, do the next line
$source .= $category.", ";                  //Insert coma and space after each element in the array
  }
  $categories = substr($source, 0, -2);         //Starting from the first spot(0), get all of the items except the last two from $source
  /* End conversion */
  
  if (isset($_POST['disclude_categories'])) {$disclude_categories = '1';} else {$disclude_categories = '0';}; //check to see if disclude_categories is set/null, if it is set it to 0
  if (isset($_POST['theloop'])) {$theloop = '1';} else {$theloop = '0';};                                     //check to see if theloop is set/null, if it is set it to 0

  
  //Connect to database
  mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());
  mysql_select_db($database) or die("ERROR DB:".mysql_error());
  
  //Insert variables into the correct field of the database
  mysql_query("INSERT INTO `blog` (`date`, `title`, `author`, `content`, `categories`, `disclude_categories`, `theloop`) VALUES ('$date', '$title', '$author', '$edited_content', '$categories','$disclude_categories', '$theloop')") or die(mysql_error());
  echo "<div id='row1'><div id='column1_1'><p>Your submission was successful<br /><br />Want to go back <a href='../'>home</a><br />or back to <a href='index.php'>edit</a>?</p></div></div>";
  
  //Footer information
  include("../include/footer.php");

?>

Link to comment
https://forums.phpfreaks.com/topic/183794-populate-forms-from-mysql-data/
Share on other sites

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.