Update via Textarea

I’m trying to learn to update a table in mysql from a textarea. <textarea rows=“30” cols=“60” name=“content” /><?php echo $row[‘content’] ?></textarea>. This is the mysql command i use. mysql_query(“UPDATE pages SET content=‘$content’ WHERE id=‘$id’”);. I’m not sure why it’s not doing it. Also I’m using tinymce as the editor and the area is hard to edit. If anyone has any suggestions please let me know.

without seeing all your code, all I can suggest is doing some basic debugging

This is all the code:

<?php
session_start();
if(!$_SESSION[‘username’]){
header(‘Location: …/login.php’);
} else {
include_once ‘…/functions/index.php’;
$sql = “SELECT * FROM pages;”;
$result = mysql_query($sql);
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />
<script type=“text/javascript” src=“tiny_mce/tiny_mce.js”></script>
<script type=“text/javascript”>
tinyMCE.init({
mode : “textareas”
});
</script>
<style type=“text/css”>
.button {
border: 1px solid #006;
background: #9cf;
}
</style>
</head>
<body>

<?php
if($_POST[‘updatePost’])
{
$id = $_POST[‘id’];
$content = $_POST[‘content’];
mysql_query(“UPDATE pages SET content=‘$content’ WHERE id=‘$id’”);
exit;
}

if($_POST[‘edit’])
{
$id = $_POST[‘id’];
$sql = “SELECT * FROM pages WHERE id=‘$id’”;
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
?>
<form method=“post”>
<fieldset style=“width:500px;”>
<legend>Front Page</legend>
<table>
<tr>
<td width=“20%”><label>Title:</label></td>
<td><?php echo $row[‘title’] ?></td>
</tr>
<tr>
<td width=“20%” valign=“top”><label>Content: </label></td>
<td><textarea rows=“30” cols=“60” name=“content” /><?php echo $row[‘content’] ?></textarea></td>
<input type=“hidden” name=“id” value=“’ . $row[‘id’] . '” />
</tr>
<tr>
<td></td><td align=“center”><input type=“submit” name=“updatePost” id=“updatePost” value=“Update Post” style=“width:200px;” class=“button”/></td>
</tr>
</table>
</fieldset>
</form>
<?php
exit;
}
}
?>
<form method=“post”>
<p>Edit content by entering number
<input type=“text” name=“id” id=“id” style=“width:30px” />
<input type=“submit” name=“edit” id=“edit” value=“Edit” class=“button”/>
</p>
</form>
<?php
echo ‘<table’;
while($row=mysql_fetch_array($result))
{
echo ‘<tr>
<td width=“50”>Title:</td>
<td width=“400”>’ . $row[‘title’] . ‘</td>
<td>’ . $row[‘id’] . ‘</td>
</tr>’;
}
echo ‘</table>’;
?>

</body>
</html>
<?php
}
?>

Actually just like the edit button they have here on the forums.

you have some obvious html syntax errors in your code.

I would fix those first and then I would normally follow the steps in the link I posted to debug your code.

I’m using Adobe Dreamweaver CS5 and it shows no errors in code view. Also I tested it all through from the html page to echoing out the vars.

my Netbeans picks up the syntax errors.

you can also validate the generated html in your browser using the w3c validator

Change your query


if($_POST['updatePost'])
{
$id = $_POST['id'];
$content = $_POST['content'];
mysql_query("UPDATE pages SET content='$content' WHERE id='$id'");
exit;
}

to


// see if you are getting what you expect posted
echo '<pre>'; print_r(&_POST); echo '</pre>';
$q = "UPDATE pages SET content='$content' WHERE id='$id'";
// does the query look good?
echo $q;
  if (mysql_query($q)) {
    echo('<p>updated</p>');
  } else {
  // finally spit out the error
    die('<p>Error updating: ' . mysql_error() . '</p>');
  }

Didn’t look at Kalons link but this is how I de-bone a script.