简体   繁体   中英

Pull information from MySQL into textarea for editing

So I want to pull information from MySQL into a textarea box.

Basically each row of MySQL information, I want to pull a value from, and put it on a newline for editing.

So for example, I have a table with cat_id and cat_desc in it. I want to have a text area which will pull the information from every row in cat_desc, and allow it to be edited (and submitted back to the correct row)

Is this possible with just PHP, or will I need to invoke Javascript too?

Saving would be accomplished by clicking a submit button.

If you don't want to dynamically populate the textarea, it's possible with PHP.

When you want to make changes, you need to submit the data and refresh the page.

You can use contentEditable http://html5demos.com/contenteditable , add each field to seperate div

<div contentEditable="true">
  <div id="cat_1">content1</div>
  <div id="cat_2">content2</div>
</div>

Then later you can use regexp to extract data and write back to database.

that's wrong idea of editing database contents.

instead of editing everything in one textarea, you have to implement some CRUD application, providing basic functionality for listing entries, editing entries and creating entries, each one distinctly

first create a listing facility, creating something like this out of data contents:

<a href=edit.php?id=1>Descr 1</a>
<a href=edit.php?id=2>Descr 2</a>

next create a form to edit one entry, and populate it froom the database based on the passed id.

Here goes a simple example of such an application.
though already usable for your needs

a main file, called whatever, let it be whatever.php

<?
mysql_connect();
mysql_select_db("new");
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part:
  $name = mysql_real_escape_string($_POST['name']);
  if ($id = intval($_POST['id'])) {
    $query="UPDATE $table SET name='$name' WHERE id=$id";
  } else {
    $query="INSERT INTO $table SET name='$name'";
  }
  mysql_query($query) or trigger_error(mysql_error()." in ".$query);
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
  exit;
}
if (!isset($_GET['id'])) { //listing part:
  $LIST=array();
  $query="SELECT * FROM $table";
  $res=mysql_query($query) or trigger_error(mysql_error()." ".$query);
  while($row=mysql_fetch_assoc($res)) $LIST[]=$row;
  include 'list.php';
} else { // form displaying part:
  if ($id=intval($_GET['id'])) {
    $query="SELECT * FROM $table WHERE id=$id";
    $res=mysql_query($query);
    $row=mysql_fetch_assoc($res);
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
  } else {
    $row['name']='';
    $row['id']=0;
  }
  include 'form.php';
}

a listing template called list.php

<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>

a form template called... guess!

<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>

it is working example as long as you have a working database, and a table created like this

CREATE TABLE `test` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

it have no delete capability though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM