Forum Moderators: coopster

Message Too Old, No Replies

Problem with Update from a textfield

How to get the text out of the textfield

         

PHP_rookie

11:29 am on Aug 16, 2004 (gmt 0)



The problem is that if I want to Edit(=Bearbeiten) a field nothing happens because only the old content of the textfield is handed over. Somehow I have to submit the new content first but how? Hope someone can help me with this problem, I am goin crazy!

<?PHP // FILL THESE IN WITH YOUR SERVER'S DETAILS
$mysqlhost = 'localhost';
$mysqlusr = 'test';
$mysqlpass = 'test';
?>
<html>
<head>
<title> Andy`s Database </title>
<link rel="stylesheet" media="screen" href="spots.css">
</head>

<body>

<?php if (isset($_REQUEST['addjoke'])): // If the user wants to add a joke?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Überschrift:<br>
<textarea name="eventheader" rows="1" cols="40" wrap></textarea><br>
<p>Ort und Postleitzahl:<br>
<textarea name="wo" rows="1" cols="40" wrap></textarea><br>
<p>Beschreibung:<br>
<textarea name="joketext" rows="10" cols="40" wrap></textarea><br>
<p>BildLink:<br>
<textarea name="bildlink" rows="1" cols="40" wrap></textarea><br>
<input type="submit" name="submitjoke" value="SUBMIT"></p>
</form>

<?php else:

// Connect to the database server
$dbcnx = @mysql_connect($mysqlhost, $mysqlusr, $mysqlpass);
if (!$dbcnx) {
echo("<p>Unable to connect to the database server at this time.</p>");
exit();
}

// Select the jokes database
if (! @mysql_select_db("andy")) {
echo("<p>Unable to locate the joke database at this time.</p>");
exit();
}

// If a joke has been submitted, add it to the database.
if (isset($_POST['joketext'])) {
$sql = "INSERT INTO Jokes SET BildLink='$_POST[bildlink]', Wo='$_POST[wo]', EventHeader='$_POST[eventheader]', JokeText='$_POST[joketext]', JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo("-------Termin wurde hinzugefügt-------</br>");
} else {
echo("<P>Error adding submitted Termin: " . mysql_error() . "</P>");
}
}

// EDIT ------------------------------------------------------------------------------------------------
if (isset($_REQUEST['bearbeitenjoke'])) {
$sql = "UPDATE Jokes SET JokeText='$_GET[joketext]' WHERE ID=$_GET[bearbeitenjoke].";

if (@mysql_query($sql)) {
echo("<p>Der Termin wurde bearbeitet</p>");
} else {
echo("<p>Error bearbeiten joke: " . mysql_error() . "</p>");
}
}

// If a joke has been deleted, remove it from the database.
if (isset($_REQUEST['deletejoke'])) {
$sql = "DELETE FROM Jokes WHERE ID=$_REQUEST[deletejoke]";
if (@mysql_query($sql)) {
echo("<p>Der Termin wurde gelöscht</p>");
} else {
echo("<p>Error deleting joke: " . mysql_error() . "</p>");
}
}
echo("<h1>Unsere Termine:</h1>");
// Request die ID, Header und Text von allen Terminen
$sql = "SELECT ID, BildLink, EventHeader, JokeText, Wo FROM Jokes";

if (isset($_POST['searchtext'])) {
$sql .= " WHERE JokeText LIKE '%".$_POST['searchtext']."%' OR EventHeader LIKE '%".$_POST['searchtext']."%'";
}

$result = @mysql_query($sql);
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}

// Display the text of each joke in a paragraph with a "Delete and Edit this Joke" link next to each.
while ($row = mysql_fetch_array($result)) {
$jokeid = $row["ID"];
$bildlink = $row["BildLink"];
$eventheader = $row["EventHeader"];
$joketext = $row["JokeText"];
$wo = $row["Wo"];
echo("<table width='50%'><th colspan='2'>$eventheader</th>" .
"<tr><td colspan='4'>$wo</td></tr>" .
"<tr><td width='150px'><img src='$bildlink'></td>
<td><textarea name='joketext' rows='9' cols='40'>$joketext</textarea><br></td>" .
//here is the problem, the link does not contain the new content!--------------------------------------------------------------------------------------
"<td width='10%'><a href='$_SERVER[PHP_SELF]?bearbeitenjoke=$jokeid&joketext=$joketext'>" . "Bearbeiten</a></td>" .
"<td width='10%'><a href='$_SERVER[PHP_SELF]?deletejoke=$jokeid'>" . "Delete</a></td></tr></table>");
}

// When clicked, this link will load this page with the joke submission form displayed.
echo("</blockquote><a href='$_SERVER[PHP_SELF]?addjoke=1'>Termin hinzufügen</a>");

endif;?>

</br></br>
<table>
<th>Suchen:</th>
<tr><td>
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="post">
<P>Zeige Termine die den folgenden Text enthalten:<BR>
<INPUT TYPE=TEXT NAME="searchtext" VALUE="<?php if (isset($_POST['searchtext'])) echo($_POST['searchtext']);?>">
<INPUT TYPE=SUBMIT NAME="submit" VALUE="Go">
</P>
</FORM>
</td></tr>
</table>
</body>
</html>

[edited by: coopster at 3:02 pm (utc) on Aug. 16, 2004]
[edit reason] removed url [/edit]

Warboss Alex

2:50 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



$sql = "UPDATE Jokes SET JokeText='$_GET[joketext]' WHERE ID=$_GET[bearbeitenjoke].";

This sql could be better handled as:

$sql = "UPDATE Jokes set JokeText='".mysql_escape_string($_GET['joketext'])."' WHERE ID = '".intval($_GET['bearbeitenjoke']."'";

Is the data not updating properly, or is it being sent at all?

I suggest this:
if (isset($_REQUEST['bearbeitenjoke'])) {

//THIS LINE PRINTS OUT ALL REQUEST VARIABLES, IE WHATEVER GOT SENT TO THE SERVER
exit(print_r($_REQUEST));
// SCRIPT THEN EXISTS

$sql = "UPDATE Jokes set JokeText='".mysql_escape_string($_GET['joketext'])."' WHERE ID = '".intval($_GET['bearbeitenjoke']."'";

if (@mysql_query($sql)) {
echo("<p>Der Termin wurde bearbeitet</p>");
} else {
echo("<p>Error bearbeiten joke: " . mysql_error() . "</p>");
}
}

The code I added to your script will halt the script and print out all $_POST, $_GET, etc. arrays, so you can see more clearly what's not being done right. Is the updated text being sent to the server? If so, use the new query I gave you and see what happens. Otherwise, your data isn't being sent in the first place!

PHP_rookie

3:54 pm on Aug 16, 2004 (gmt 0)



Sorry I think you didN´t get me right.
Your little addition: exit(print_r($_REQUEST)); shows it: The data isn't being sent to the server in the first place. Perhaps I missed something by using the textfield again. Somehow I must post the new content to the server but how?
Common guys isn`t this an easy task!?

Warboss Alex

5:11 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



<FORM action="<?=$_SERVER['PHP_SELF']?>" method="post">
<P>Zeige Termine die den folgenden Text enthalten:<BR>
<INPUT TYPE=TEXT NAME="searchtext" VALUE="<?php if (isset($_POST['searchtext'])) echo($_POST['searchtext']);?>">
<INPUT TYPE=SUBMIT NAME="submit" VALUE="Go">
</P>
</FORM>

Are you using that form? If so, your text field is named 'searchtext', not 'joketext'..

What form d'you use to edit a joke? Note that your other form has a value of 'joketext' not 'Joketext'. Variables are case-sensitive, could that be the problem?