By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,747 Members | 1,701 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,747 IT Pros & Developers. It's quick & easy.

How can I retrieve, edit, and update text?

P: n/a
I'm trying to retrieve a text value from a MySQL database, put it into
an updateable form, allow edits, and send back the edited text back
into the MySQL database. I've been able to successfully retrieve the
text and put it into the editable form, using htmlspecialchars
function. But, I can't find a way of accepting the edited text and
sending back to the database.

Here's my code for retrieval.

<?php
@ $db = mysql_connect('localhost', '<database>', '<password>');
if (!$db)
{
echo "Error: Could not connect to database. Please try again
later.";
exit;
}
mysql_select_db("<database>");
$query = "select * from albert where ".$searchtype." like
'%".$searchterm."%'";

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$title = $row['title'];
$teaching = $row['teaching'];
$teaching = wordwrap($teaching, 60, "\n", 1);
$teachnum = $row['teachnum'];
}
if ($result)
{
echo '<textarea name="newteachnum" rows="1" cols="4"
wrap="off">';
echo htmlspecialchars($teachnum)."\n";
echo '</textarea>';
echo '<textarea name="newtitle" rows="1" cols="60"
wrap="off">';
echo htmlspecialchars($title)."\n";
echo '</textarea>';
echo '<textarea name="newteaching" rows="20" cols="80"
wrap="on">';
echo htmlspecialchars($teaching)."\n";
echo '</textarea>';
}
?>
<form action="teach_update.php" method="post">
<input type=submit value="Submit">
</form>

Here's the code to update the database.

<?php
@ $db = mysql_connect('localhost', '<database>', '<password>');
if (!$db)
{
echo "Error: Could not connect to database. Please try again
later.";
exit;
}
mysql_select_db("<database>");

$newtitle = addslashes($newtitle);
$newteaching = addslashes($newteaching);
$newteachnum = addslashes($newteachnum);

$query = "update albert set title =". $newtitle . " and teaching =
". $newteaching . " where teachnum =" . $newteachnum ;

print $query; //the print shows nulls for $newtitle, $newteaching,
and $newteachnum. It seems like those values are not being held in
memory from the calling program teach_edit.php

//$result = mysql_query($query) or die("Could not update!");

// if ($result)
// {
// echo mysql_affected_rows()." text item updated into
database.";
// }
?>

Can someone tell me what I'm missing? Thanks in advance.
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
jn
"Bob Kaku" <bo*****@yahoo.com> wrote in message
news:e4*************************@posting.google.co m...
I'm trying to retrieve a text value from a MySQL database, put it into
an updateable form, allow edits, and send back the edited text back
into the MySQL database. I've been able to successfully retrieve the
text and put it into the editable form, using htmlspecialchars
function. But, I can't find a way of accepting the edited text and
sending back to the database.

Here's my code for retrieval.

<?php
@ $db = mysql_connect('localhost', '<database>', '<password>');
if (!$db)
{
echo "Error: Could not connect to database. Please try again
later.";
exit;
}
mysql_select_db("<database>");
$query = "select * from albert where ".$searchtype." like
'%".$searchterm."%'";

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$title = $row['title'];
$teaching = $row['teaching'];
$teaching = wordwrap($teaching, 60, "\n", 1);
$teachnum = $row['teachnum'];
}
if ($result)
{
echo '<textarea name="newteachnum" rows="1" cols="4"
wrap="off">';
echo htmlspecialchars($teachnum)."\n";
echo '</textarea>';
echo '<textarea name="newtitle" rows="1" cols="60"
wrap="off">';
echo htmlspecialchars($title)."\n";
echo '</textarea>';
echo '<textarea name="newteaching" rows="20" cols="80"
wrap="on">';
echo htmlspecialchars($teaching)."\n";
echo '</textarea>';
}
?>
<form action="teach_update.php" method="post">
<input type=submit value="Submit">
</form>

Here's the code to update the database.

<?php
@ $db = mysql_connect('localhost', '<database>', '<password>');
if (!$db)
{
echo "Error: Could not connect to database. Please try again
later.";
exit;
}
mysql_select_db("<database>");

$newtitle = addslashes($newtitle);
$newteaching = addslashes($newteaching);
$newteachnum = addslashes($newteachnum);

$query = "update albert set title =". $newtitle . " and teaching =
". $newteaching . " where teachnum =" . $newteachnum ;

print $query; //the print shows nulls for $newtitle, $newteaching,
and $newteachnum. It seems like those values are not being held in
memory from the calling program teach_edit.php

//$result = mysql_query($query) or die("Could not update!");

// if ($result)
// {
// echo mysql_affected_rows()." text item updated into
database.";
// }
?>

Can someone tell me what I'm missing? Thanks in advance.


Make sure that register_globals is on, or use $_POST['varname'] instead.

You also need single quotes around the updated values in your query.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.