Page 1 of 1

Initial textfield value problem..Please help!

Posted: Fri Jan 16, 2004 2:35 am
by bumple
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!

Code: Select all

<?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!

-bumple.

Posted: Fri Jan 16, 2004 2:44 am
by McGruff
Can you set E_ALL error reporting in php.ini? That might be a place to start - let me know what errors you get.

Posted: Fri Jan 16, 2004 4:50 am
by bumple
i dont quite have access to my php.ini =\

Posted: Fri Jan 16, 2004 4:51 am
by malcolmboston
how can you not have access to your php.ini?

if your site is hosted there will be a page that allows you to see what your version of PHP is capable of

Posted: Fri Jan 16, 2004 5:12 am
by markl999
You can also do error_reporting(E_ALL); at the top of the script without having to alter php.ini.

But the problem appears to be the lines..
Wish #1:<input name="w1" type="text" size="60" maxlength="70" value="<?php echo $w_wish1;?>"><br>

$w_wish1 isn't set anywhere, previously you correctly use $_POST['w1'] etc, so you should be using :

Wish #1:<input name="w1" type="text" size="60" maxlength="70" value="<?php echo $_POST['w1'];?>"><br>

You might also want to add some code to check each of the wishes was filled in so you don't get 'Undefined Index' errors.

Posted: Fri Jan 16, 2004 11:40 am
by McGruff
Do you have a local test server? I'm guessing you don't if you can't change php.ini. See http://www.hotscripts.com/PHP/Software_ ... tion_Kits/

E_ALL is essential for developing scripts (but of course you turn the error reportng down on a live site).

I think there might be a few things going wrong in the above script but E_ALL is a good place to start. I'm wondering if there might be an undefined index warning for $_SESSION...

Posted: Fri Jan 16, 2004 12:38 pm
by Gen-ik
malcolmboston wrote:how can you not have access to your php.ini?
if your site is hosted there will be a page that allows you to see what your version of PHP is capable of
That's not really correct.. most hosts will not allow you access to the php.ini file, especially if you are on a shared server. However, if you have a dedicated server you can do what you want (within reason) including accessing and editing your php.ini file.

Posted: Fri Jan 16, 2004 4:30 pm
by mwong
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());
}}
1)Ok the IF statement checks if the wish list exists, then updates it.

2)The else statement creats a new one if one does not exists.

3)When do you ever pull the wishes out of the database into variables if they do exist?

Quick fix? right before the table do a query of the database for the wish list. Then put them to variables.

Maybe to make the update / write simple: Check if the server request is POST then put an UPDATE mysql query within an if condition...if it comes up with false because it doesn't exist....THEN insert a new wish list.