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 htmlspecialchar s
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 ".$searchty pe." like
'%".$searchterm ."%'";
$result = mysql_query($qu ery);
while ($row = mysql_fetch_arr ay($result)) {
$title = $row['title'];
$teaching = $row['teaching'];
$teaching = wordwrap($teach ing, 60, "\n", 1);
$teachnum = $row['teachnum'];
}
if ($result)
{
echo '<textarea name="newteachn um" rows="1" cols="4"
wrap="off">';
echo htmlspecialchar s($teachnum)."\ n";
echo '</textarea>';
echo '<textarea name="newtitle" rows="1" cols="60"
wrap="off">';
echo htmlspecialchar s($title)."\n";
echo '</textarea>';
echo '<textarea name="newteachi ng" rows="20" cols="80"
wrap="on">';
echo htmlspecialchar s($teaching)."\ n";
echo '</textarea>';
}
?>
<form action="teach_u pdate.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($new title);
$newteaching = addslashes($new teaching);
$newteachnum = addslashes($new teachnum);
$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($qu ery) 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.
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 htmlspecialchar s
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 ".$searchty pe." like
'%".$searchterm ."%'";
$result = mysql_query($qu ery);
while ($row = mysql_fetch_arr ay($result)) {
$title = $row['title'];
$teaching = $row['teaching'];
$teaching = wordwrap($teach ing, 60, "\n", 1);
$teachnum = $row['teachnum'];
}
if ($result)
{
echo '<textarea name="newteachn um" rows="1" cols="4"
wrap="off">';
echo htmlspecialchar s($teachnum)."\ n";
echo '</textarea>';
echo '<textarea name="newtitle" rows="1" cols="60"
wrap="off">';
echo htmlspecialchar s($title)."\n";
echo '</textarea>';
echo '<textarea name="newteachi ng" rows="20" cols="80"
wrap="on">';
echo htmlspecialchar s($teaching)."\ n";
echo '</textarea>';
}
?>
<form action="teach_u pdate.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($new title);
$newteaching = addslashes($new teaching);
$newteachnum = addslashes($new teachnum);
$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($qu ery) 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.
Comment