I am trying to change content of a mysql record, so I placed in a variable
which is used whithin textarea's html tags:
<textarea rows=4 cols=50 name="contents"> $current_content</textarea>
The content loads up inside the textbar. Now I have another submit button in
my file that is reponsible for UPDATE of the record. I thought once the user has
changed the texts inside the textarea the new stuff is called. However the same
$current_content is called (i think) because nothing happens -- since there are
no errors anymore I think the same old variable that was taken and applied to
the textarea is reapplied to the record and the stuff TYPED inside the textarea is
ignored. Here is my code (thanks in advanced): :p
Show Script
Enter the URL of the script below:
<?php
#########################################################
class05-sites4.php - Delete your Favorite Sites - Multiple
#########################################################
$scriptName = "simpleedit2.php";
$pageTitle = "this is trying to edit2";
Get our DB info
require "info.php";
Make sure we display errors to the browser
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
#########################################################
Connect to the database.
#########################################################
$connection = mysql_connect($mySqlHostname, $mySqlUsername, $mySqlPassword);
if (!$connection)
die("Error " . mysql_errno() . " : " . mysql_error());
Select the DB
$db_selected = mysql_select_db($mySqlDatabase, $connection);
if (!$db_selected)
die("Error " . mysql_errno() . " : " . mysql_error());
#########################################################
Define our widgets and initial values
#########################################################
edit widget
$submitName = "DataAction";
$submitDeleteValue = "Edit Tag"; # This is what it will say on our delete site button
update widget
$submitUpdateName = "updateAction";
$submitUpdateValue = "Update Tag"; # This is what it will say on our delete site button
Checkbox widgets
$checkboxPrefix = "cb_"; # All our radio buttons for voting will be named this
Status variables
$statusMsg = ""; # Gives response back to user (i.e. "Thank you for your ...")
$hasErrors = 0; # Keeps track of whether there are input errors
$numSelected = 0;
#########################################################
Update the votes for selected site if submit button was pressed
#########################################################
if ($_POST[$submitName]==$submitDeleteValue)
{
Submit button pressed, assume errors until we find a selected checkbox
$hasErrors = 1;
Compute the value for each checkbox by looping through the $_POST array
foreach ($POST as $key => $value)
{ if (substr($key,0,strlen($checkboxPrefix))==$checkboxPrefix && !empty($POST[$key]))
{
This POST name-value pair belongs to a checkbox so clear error flag and extract ID
$hasErrors = 0;
$id = str_replace($checkboxPrefix, "", $key);
# Create the SQL query
//$SqlStatement = "DELETE FROM tag_table WHERE id=$id";
$SqlStatement = "SELECT id, tag, content, date FROM tag_table WHERE id=$id";
# Run the query on the database through the connection
$result = mysql_query($SqlStatement,$connection);
if (!$result)
die("Error " . mysql_errno() . " : " . mysql_error());
while ($sel = mysql_fetch_array($result,MYSQL_NUM))
{ $current_id = $sel[0];
$current_tag = $sel[1];
$current_content = $sel[2];
$current_date = $sel[3];
}
$numSelected++;
}
}
# Compute the proper display msg
if (!$hasErrors)
{ $statusMsg = "$numDeleted tag(s) were prepared for edit.";}
else
{ $statusMsg = "Please select at least one tag to delete.";}
}
$UpdatedContent = $_POST["contents"];
if ($_POST[$submitUpdateName]==$submitUpdateValue)
{
// $SqlStatement = "UPDATE tag_table SET content = $current_content WHERE id = $id";
$SqlStatement = "UPDATE tag_table SET content = '$UpdatedContent' WHERE id = '$id'";
// $SqlStatement = "UPDATE tag_table SET content= '$myName' WHERE id=1 ";
Run the query on the database through the connection
$result = mysql_query($SqlStatement,$connection);
if (!$result)
die("Error " . mysql_errno() . " : " . mysql_error());
}
#########################################################
Visual Info
#########################################################
$table_header_color = "#99cccc";
$table_row_color = "#e8e8e8";
?>
<html>
<head>
<title><?=$pageTitle?></title>
<link rel="stylesheet" href="css/classnotes.css" type="text/css" media="all" />
</head>
<body>
<b><?=$pageTitle?></b>
<p>
Choose a website(s) to delete below.
<p>
<?php
If we put anything in the general status message, then print it
if (!empty($statusMsg))
{ print '<font color="#990000"><b>'.$statusMsg.'</b></font> <p>';
}
#########################################################
Use SELECT to show favorite sites
#########################################################
echo <<<END
<table cellspacing="1" cellpadding="5" border="0" class="arial13">
<form action="$scriptName" method="POST" enctype="application/x-www-form-urlencoded">
<tr bgcolor="$table_header_color">
<td align="left"><b> </b></td></td>
<td align="left"><b>TAG</b></td>
<td align="left"><b>CONTENT</b></td>
<td align="left"><b>DATE</b></td>
</tr>
END;
Retrieve all rows from the table and put in normal array.
$SqlStatement = "SELECT id, tag, content, date FROM tag_table
ORDER BY id DESC";
Run the query on the database through the connection
$result = mysql_query($SqlStatement,$connection);
if (!$result)
die("Error " . mysql_errno() . " : " . mysql_error());
while ($row = mysql_fetch_array($result,MYSQL_NUM))
{
echo <<<END
<tr bgcolor="$table_row_color">
<td align="left"><input type="checkbox" name="{$checkboxPrefix}{$row[0]}"></td>
<td align="left"><a target="newSite" href="$row[2]">$row[1]</a></td>
<td align="left">$row[2]</td>
<td align="left">$row[3]</td>
</tr>
END;
}
echo <<<END
<br>
<tr>
<td></td>
<td></td>
<td align="left"><textarea rows=4 cols=50 name="contents"> $current_content</textarea>
<input type="submit" name="$submitName" value="$submitDeleteValue"></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="$submitUpdateName" value="$submitUpdateValue"></td>
</tr>
</table>
</form>
END;
#########################################################
Disconnect from the database.
#########################################################
mysql_close($connection);
?>
</body>
</html>