Initial textfield value problem..Please help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Initial textfield value problem..Please help!

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
bumple
Forum Commoner
Posts: 34
Joined: Thu Jan 08, 2004 4:38 am

Post by bumple »

i dont quite have access to my php.ini =\
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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...
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
mwong
Forum Commoner
Posts: 34
Joined: Sun Dec 28, 2003 2:58 am

Post 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.
Post Reply