Reply to can't get variable into the query

Your name:

Reply:


Posted by cashmere on 10/10/38 11:50

ok I'm a semi-newbie. I've been wrestling with this for a few days now!
btw this is on my local machine, or rather on my local lan.
I'm trying to modify a php script that I found (add and delete jokes
to/from a database), so that it will add text like a small-ad, to an
existing row. The delete part I want to get rid of later when I've
solved the present probs.
What happens is, the user searches a table for a particular name and
this returns one or more rows, each with a 5-digit id. When the
selected id is clicked, this is passed as

http://192.168.0.51/msg_form.php?id=52080

via the url to the next php page which creates a row in a different
table, called multiple. Multiple has 2 columns, id (of course not
auto-incremented) and content (text).
This works fine, the new row is created in the multiple table with the
id from the url, and the content field is of course as yet empty.
There is a form on the same page that is for the user to enter some
text that is supposed to update the empty content field.
My prob:
No matter what I do, a new row with a zero in the id column is created,
and with the text in the content field, whilst the row that was
supposed to be updated is untouched, i.e it has the id from the
previous page and the content feld still empty.
here's what I currently have:

<?php
session_start(); // in desperation I have tried sessions but don't
really know what I'm doing...
echo 'Welcome to whatever';
$_SESSION['id'] = '$_POST';
?>
<html><head></head><body>
<?php
$id = $_GET['id'];
mysql_pconnect("localhost", "root", "whatever") or die(mysql_error());
mysql_select_db("members") or die(mysql_error());
mysql_query("INSERT INTO `multiple` VALUES ('$id','')");
//inserts the id OK into the table (i.e. creates a new row with that
id), but from here NG (no good)
print "<br>";
print "<br>";
print $_SESSION['id']; // this prints out OK
?>

<?php if (isset($_GET['addjoke'])): // User wants to add a joke uh make
that message
?>

<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<P>Type your joke uh message here:<BR>
<TEXTAREA NAME="joketext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>

<?php
else:
// Connect to the database server
$dbcnx = @mysql_connect('localhost', 'root', 'whatever');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}

// Select the jokes database
if (!@mysql_select_db('members')) {
exit('<p>Unable to locate the joke ' .
'database at this time.</p>');
}
$id = $_SESSION['id'];
print "<br>"; // these lines are just some of the
things I have tried
print "after this should be the session id";
print "<br>";
echo $_SESSION['id']; // this echoes out OK
print "<br>";
print "before this should be the session id"; // yeah,
it was OK
print "<br>";
// If a joke has been submitted,
// add it to the database.
if ("SUBMIT" == $submitjoke) {
$sql = "UPDATE multiple SET " .
"content='$joketext' WHERE id= '$id' ";
if (mysql_query($sql)) {
echo("<P>Your content has been added.</P>");
} else {
echo("<P>Error adding submitted joke: " .
mysql_error() . "</P>");
}
}

// If a joke has been deleted,
// remove it from the database.
if (isset($_GET['deletejoke'])) {
$jokeid = $_GET['deletejoke'];
$sql = "DELETE FROM multiple
WHERE id=$id";
if (@mysql_query($sql)) {
echo '<p>The joke has been deleted.</p>';
} else {
echo '<p>Error deleting joke: ' .
mysql_error() . '</p>';
}
}
echo("<P> Add your msg for </P>");
echo("<P> your id $id here: </P>");
print "<br>";
print $_POST['joketext'];
print "<br>";
print $_REQUEST['joketext'];
print "<br>";
// print $_GET['joketext'];
print "<br>";
// Request the text of all the jokes
$result = mysql_query(
"SELECT content FROM multiple");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit(); }
// Display the text of each joke in a paragraph
// with a "Delete this joke" link next to each.
while ($row = mysql_fetch_array($result)) {
$jokeid = $row['id'];
$joketext = $row['content'];
echo '<p>' . $joketext .
' <a href="' . $_SERVER['PHP_SELF'] .
'?deletejoke=' . $jokeid . '"></a></p>';
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<P><A HREF='$PHP_SELF?addjoke=1'>" .
"Add a message.</A></P>");
endif;
?>
</BODY>
</HTML>

Everything works except that the value in $id is obviously 0 or there
is no value being passed.
I have tried a lot of stuff but no luck!
Cheers Dave

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация