I have a slight problem with my PHP script. I have a page, after a user logs in, to have a list of six wishes. When they input their wishes into the textfields and click submit, it successfully puts them in the database...
However, I want to make it so that when they return to the page, it will already have their wishes in the textfields (as the initial textfield value).
My page consists of a USERS table (with username, password, and registration info), and a WISHES table (with user's wishes).
Here is what my code looks like so far...adding to the database works perfectly, it's just echoing the wishes afterwards that has problems. I get no errors, it's just a blank textfield. Also, it has nothing to do with the textfield, if I want to echo the wish by itself, it still wont work...
Here is my code... please examine and tell me what's wrong...thanks!
<?php
require('db.php'); // database connect script, includes session_start() and $logged_in.
if ($logged_in == 0) {
die('Sorry you are not logged in, this area is restricted to registered members. You may choose to <a href="login.php">log in</a> again!');
}
if (isset($_POST['submit'])) { // if form has been submitted ?>
<?php echo $_SESSION['username'];?>, your wishes have been updated! <br>
<?php $idiot = $db_object->query("SELECT id FROM users WHERE username = ".$db_object->quote($_SESSION['username']));
$idiot = $idiot->fetchRow();
// now we can add them to the database.
// check if wish exists
$query = "SELECT w_id from wishes WHERE w_id = " . $idiot["id"];
$result = mysql_query($query);
if ($tmp = mysql_fetch_row($result)) { // wishes exist, update it
$query = 'UPDATE wishes
SET w_id = '.$idiot['id'].',
w_username = "'.$_SESSION['username'].'",
w_wish1 = "'.$_POST['w1'].'",
w_wish2 = "'.$_POST['w2'].'",
w_wish3 = "'.$_POST['w3'].'",
w_wish4 = "'.$_POST['w4'].'",
w_wish5 = "'.$_POST['w5'].'",
w_wish6 = "'.$_POST['w6'].'"
WHERE
w_id = '.$idiot['id'].'';
mysql_query($query) or die(mysql_error());
} else { // wishes dont exist, add new wishes
$query = 'INSERT INTO wishes(
w_id,
w_username,
w_wish1,
w_wish2,
w_wish3,
w_wish4,
w_wish5,
w_wish6)
VALUES (
'.$idiot['id'].',
"'.$_SESSION['username'].'",
"'.$_POST['w1'].'",
"'.$_POST['w2'].'",
"'.$_POST['w3'].'",
"'.$_POST['w4'].'",
"'.$_POST['w5'].'",
"'.$_POST['w6'].'")';
mysql_query($query) or die(mysql_error());
}}
//everything up to here should work fine, it's just below that it won't echo $w_wish(x). Do I have to point it to the WISHES table?
else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Wish #1:<input name="w1" type="text" size="60" maxlength="70" value="<?php echo $w_wish1;?>"><br>
Wish #2:<input name="w2" type="text" size="60" maxlength="70" value="<?php echo $w_wish2;?>"><br>
Wish #3:<input name="w3" type="text" size="60" maxlength="70" value="<?php echo $w_wish3;?>"><br>
Wish #4: <input name="w4" type="text" size="60" maxlength="70" value="<?php echo $w_wish4;?>"><br>
Wish #5:<input name="w5" type="text" size="60" maxlength="70" value="<?php echo $w_wish5;?>"><br>
Wish #6: <input name="w6" type="text" size="60" maxlength="70" value="<?php echo $w_wish6;?>"><br><br>
<input type="submit" name="submit" value="Update!">
//none of these textfields work...it gives no error, but also has no initial values for the textfields when they do exist in the database.
-THANKS SO MUCH ALL!
-influx.