header() not redirecting for some reason...

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

User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i've found that there is series or order where sql commands must be executed. so i changed my WHERE statement to the place it's supposed to be... but it STILL gives me an error!
You have an error in your SQL syntax near 'groomLname = '', event_month = '', event_day = '' event_year = '' WHERE uID = I2' at line 1
here's my code...

Code: Select all

$sql = "UPDATE my_search_table ".
		 "SET brideFname = '$brideFname', brideLname = '$brideLname', groomFname = '$groomFname' ".
		     "groomLname = '$groomLname', event_month = '$event_month', event_day = '$event_day' ".
		     "event_year = '$event_year' ".
		 "WHERE uID = I2ZMwC61kdJuzEBTqJi2AQYZJK8qU";
	mysql_query($sql) or die(mysql_error());
	mysql_close($db);
what's going ON! i even entered the uID manually just to see if that was the problem... it isn't.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

You need some commas...

Code: Select all

$sql = "UPDATE my_search_table ".
		 "SET brideFname = '$brideFname', brideLname = '$brideLname', groomFname = '$groomFname', ".
		     "groomLname = '$groomLname', event_month = '$event_month', event_day = '$event_day', ".
		     "event_year = '$event_year' ".
		 "WHERE uID = I2ZMwC61kdJuzEBTqJi2AQYZJK8qU";
	mysql_query($sql) or die(mysql_error());
	mysql_close($db);
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

alright, thanks, ninja... i spent like 30 minutes looking at my code and didn't realize i was missing a couple of commas. thank you. but, to no surprise, i'm given another error....
Unknown column 'I2ZMwC61kdJuzEBTqJi2AQYZJK8qU' in 'where clause'

uID is the column. I2ZMwC61kdJuzEBTqJi2AQYZJK8qU is used to locate the correct ROW of information in the entire table to update. what's the deal? why is it looking for I2ZMwC61kdJuzEBTqJi2AQYZJK8qU as a column name?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

probably needs some quotes:

Code: Select all

$sql = "UPDATE my_search_table ".
                 "SET brideFname = '$brideFname', brideLname = '$brideLname', groomFname = '$groomFname', ".
                     "groomLname = '$groomLname', event_month = '$event_month', event_day = '$event_day', ".
                     "event_year = '$event_year' ".
                 "WHERE uID = 'I2ZMwC61kdJuzEBTqJi2AQYZJK8qU'";
        mysql_query($sql) or die(mysql_error());
        mysql_close($db);
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Syntax for UPDATE:

Code: Select all

UPDATE `table_name` SET `field1` = 'string_value_in_quotes', `field2` = int_value_not_in_quotes WHERE `some_field` = 'some_value';
Remember to put commas between each value assignment. Your query should look like ...

Code: Select all

$sql = "UPDATE my_search_table 
        SET 
            brideFname = '$brideFname', 
            brideLname = '$brideLname', 
            groomFname = '$groomFname', 
            groomLname = '$groomLname', 
            event_month = '$event_month', 
            event_day = '$event_day',
            event_year = '$event_year' 
        WHERE uID = 'I2ZMwC61kdJuzEBTqJi2AQYZJK8qU'";
EDIT | Dayum you guys are fast...
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Whapow! Image
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

nevermind... i put single quotes around it and now it doesn't give me an error... but it STILL WON'T SAVE THE NEW EDITED INFO!!!!! what could i possibly be doing wrong man!!! here's the code at the top of my page... the editinfo.php page is now set to <form action SELF, but when it does that, it's supposed to $_POST the editions in the input fields, and then replace them, and THEN spit out the new results again in input fields.

Code: Select all

<?php
	$brideFname    = $_POST['brideFname'];
	$brideLname    = $_POST['brideLname'];
	$groomFname    = $_POST['groomFname'];
	$groomLname    = $_POST['groomLname'];
	$_POST['event_month2'];
	$_POST['event_day2'];
	$_POST['event_year2'];
	$event_month   = $_POST['event_month'];
	$event_day     = $_POST['event_day'];
	$event_year    = $_POST['event_year'];
	#$_POST['editID'];
	#$_POST['ship_add'];
	#$_POST['ship_city'];
	#$_POST['ship_state'];
	#$_POST['ship_zip'];

	if($_POST['event_month2'] != NULL){
		$event_month = $_POST['event_month2'];
	}if($_POST['event_day2'] != NULL){
		$event_day = $_POST['event_day2'];
	}if($_POST['event_year2'] != NULL){
		$event_year = $_POST['event_year2'];
	}
	

	@ $db = mysql_connect("localhost", "apache", "nopass");
	mysql_select_db("registry_DB", $db);
	if(!$db){
		echo "Error: Could not connect to the database. Please try again later.";
		exit;
	}
	$sql = "UPDATE my_search_table ".
		 "SET brideFname = '$brideFname', brideLname = '$brideLname', groomFname = '$groomFname', ".
		     "groomLname = '$groomLname', event_month = '$event_month', event_day = '$event_day', ".
		     "event_year = '$event_year' ".
		 "WHERE uID = '". $row['uID'] ."'";
	mysql_query($sql) or die(mysql_error());
	mysql_close($db);
it kinda works. it's actually replacing the data in the SQL table now FINALLY! except, it's replacing it with NULL values... i'll be in editinfo.php, with all the nice pre-populated input fields... i'll make a couple of changes... click submit... and then it brings up input fields with nothing in them. then i check the database, and sure enough... it's replaced it... with nothing. so now, the ONLY problem i have is a problem i've had since the beginning... getting this info to actually $_POST the new values... right now it thinks that the new values are NULL... so, yet again my friends, what could i possibly be doing wrong???
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, here are some comments for you...

Code: Select all

<?php
        $brideFname    = $_POST['brideFname'];
        $brideLname    = $_POST['brideLname'];
        $groomFname    = $_POST['groomFname'];
        $groomLname    = $_POST['groomLname'];

        // What exactly are these little beauties doing?
        $_POST['event_month2'];
        $_POST['event_day2'];
        $_POST['event_year2'];
        // End sarcastic question

        $event_month   = $_POST['event_month'];
        $event_day     = $_POST['event_day'];
        $event_year    = $_POST['event_year'];

        // Just remove these if you are not going to use them
        #$_POST['editID'];
        #$_POST['ship_add'];
        #$_POST['ship_city'];
        #$_POST['ship_state'];
        #$_POST['ship_zip'];

        // Lets clean this out a bit... Also, you should default these vars because 
        // if they fail the if then they will be considered unitialized
        if ($_POST['event_month2'] != NULL) {
                $event_month = $_POST['event_month2'];
        }

        if ($_POST['event_day2'] != NULL) {
                $event_day = $_POST['event_day2'];
        }

        if ($_POST['event_year2'] != NULL) {
                $event_year = $_POST['event_year2'];
        }
       
        // Removed error suppression and cleaned up error checking
        if (!$db = mysql_connect("localhost", "apache", "nopass"))
                die('Error: Could not connect to the database server. Please try again later: ' . mysql_error());
        }

        if (!mysql_select_db("registry_DB", $db)) {
                die('Error: Could not connect to the database. Please try again later: ' . mysql_error());
        }

        $sql = "UPDATE my_search_table 
                SET 
                    brideFname = '$brideFname', 
                    brideLname = '$brideLname', 
                    groomFname = '$groomFname', 
                    groomLname = '$groomLname', 
                    event_month = '$event_month', 
                    event_day = '$event_day', 
                    event_year = '$event_year' 
                WHERE uID = '". $row['uID'] ."'"; // What is $row['uID'] and where is this set?
        mysql_query($sql) or die(mysql_error());
        mysql_close($db);
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

ok, the event_....2 variables. when the input fields are pre-populated, by retrieving the data from the sql table, the value of the date is echoed. however, if the admin wishes to change it, they have a drop-down HTML menu for the day, month, and year. THIS is event_day2/month2/year2. as you'll notice in my code, IF these variables (each one) is not empty, replace it the original value of the variable, then, store the new variable value into the database.

as far as removing the commented variables... i do plan on using them, but at the moment they are causing more harm than good. when i try to pre-populate an input field with ship_add... it will only print the value until it gets to a whitespace... so, for instance, in the database the ship_add may be '1234 Copperfield Dr.'... but when i try to put it in an input field it comes out '1234'... so i'll figure that out later, but for now, i've got bigger problems to deal with...

$row['uID'] stands for unique ID. it's a necessary randomly generated unique ID given to each entry in the database table. $row[''] is being printed from a previous while() statement. anyway, it works... when i debug this stuff, it echoes correctly. so it's not that. but it is imperative so the sql command can have an identifier. a location of the row to replace, retrieve, update, or delete the data. and right now, it's finding the right row, but it thinks that all the new values are NULL, and then it replaces the correct row with the wrong information...

does that shed some light on the situation?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
        // These are all values from the form
        // This assumes cleanliness and value (no empties)
		$brideFname    = $_POST['brideFname'];
        $brideLname    = $_POST['brideLname'];
        $groomFname    = $_POST['groomFname'];
        $groomLname    = $_POST['groomLname'];
        
        // Setting this here for use later
		$uID = $row['uID']; 

        // Lets clean this out a bit... Also, you should default these vars because
        // if they fail the if then they will be considered unitialized
        if ($_POST['event_month2'] !== NULL) {
            $event_month = $_POST['event_month2'];
        } else {
            $event_month = $_POST['event_month'];
        }

        if ($_POST['event_day2'] !== NULL) {
            $event_day = $_POST['event_day2'];
        } else {
            $event_day = $_POST['event_day'];
        }

        if ($_POST['event_year2'] !== NULL) {
            $event_year = $_POST['event_year2'];
        } else {
            $event_year = $_POST['event_year'];
        }
       
        // Removed error suppression and cleaned up error checking
        if (!$db = mysql_connect("localhost", "apache", "nopass")) {
            die('Error: Could not connect to the database server. Please try again later: ' . mysql_error());
        }

        if (!mysql_select_db("registry_DB", $db)) {
                die('Error: Could not connect to the database. Please try again later: ' . mysql_error());
        }

        // This assumes that all the data from the form was not empty!
        // Also, if the event data parts or uID are numbers, remove the quotes
        $sql = "UPDATE my_search_table
                SET
                    brideFname = '$brideFname',
                    brideLname = '$brideLname',
                    groomFname = '$groomFname',
                    groomLname = '$groomLname',
                    event_month = '$event_month',
                    event_day = '$event_day',
                    event_year = '$event_year'
                WHERE uID = '$uID'";
        if (!mysql_query($sql) || !mysql_affected_rows()) {
            die(mysql_error());
        }

        mysql_close($db);
?>
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i'm a little confused. if the $_POST variables come from the form, then why is it replacing them with NULL values? furthermore, why isn't it replacing it with the NEW edited values?

the event_day/month/year values AND the uID are all VARCHARs.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would guess there is maybe a conflict in field names from the form. Can you post your form code?
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

for sure. here's the entire page...

Code: Select all

<?php
//editCouple.inc
	$brideFname    = $_POST['brideFname'];
	$brideLname    = $_POST['brideLname'];
	$groomFname    = $_POST['groomFname'];
	$groomLname    = $_POST['groomLname'];
	$_POST['event_month2'];
	$_POST['event_day2'];
	$_POST['event_year2'];
	$event_month   = $_POST['event_month'];
	$event_day     = $_POST['event_day'];
	$event_year    = $_POST['event_year'];
	#$_POST['editID'];
	#$_POST['ship_add'];
	#$_POST['ship_city'];
	#$_POST['ship_state'];
	#$_POST['ship_zip'];

	if($_POST['event_month2'] != NULL){
		$event_month = $_POST['event_month2'];
	}if($_POST['event_day2'] != NULL){
		$event_day = $_POST['event_day2'];
	}if($_POST['event_year2'] != NULL){
		$event_year = $_POST['event_year2'];
	}
	

	@ $db = mysql_connect("localhost", "apache", "nopass");
	mysql_select_db("registry_DB", $db);
	if(!$db){
		echo "Error: Could not connect to the database. Please try again later.";
		exit;
	}
	$sql = "UPDATE my_search_table ".
		 "SET brideFname = '$brideFname', brideLname = '$brideLname', groomFname = '$groomFname', ".
		     "groomLname = '$groomLname', event_month = '$event_month', event_day = '$event_day', ".
		     "event_year = '$event_year' ".
		 "WHERE uID = '". $row['uID'] ."'";
	mysql_query($sql) or die(mysql_error());
	mysql_close($db);

	echo "<B>Bride's First Name:</B> <input type=text name=brideFname value=". $row['brideFname'] ."><br />";
	echo "<B>Bride's Last Name:</B> <input type=text name=brideLname value=". $row['brideLname'] ."><br /><br />";
	echo "<B>Groom's First Name:</B> <input type=text name=groomFname value=". $row['groomFname'] ."><br />";
	echo "<B>Groom's Last Name:</B> <input type=text name=groomLname value=". $row['groomLname'] ."><br /><br />";
	echo "<B>Event date:</B> ". $row['event_month'] ."/". $row['event_day'] ."/". $row['event_year'] ."<br /><br />";
?>

<TABLE><TR><TD>
<form action=editCouple.php?editID=<?php echo $row['uID']; ?> method=post>
<SELECT NAME="event_month2">
<OPTION VALUE="">select a month
<OPTION VALUE="01">January
<OPTION VALUE="02">February
<OPTION VALUE="03">March
<OPTION VALUE="04">April
<OPTION VALUE="05">May
<OPTION VALUE="06">June
<OPTION VALUE="07">July
<OPTION VALUE="08">August
<OPTION VALUE="09">September
<OPTION VALUE="10">October
<OPTION VALUE="11">November
<OPTION VALUE="12">December
</SELECT>
&nbsp;
<SELECT NAME="event_day2">
<OPTION VALUE="">select a day
<OPTION VALUE="01">01
<OPTION VALUE="02">02
<OPTION VALUE="03">03
<OPTION VALUE="04">04
<OPTION VALUE="05">05
<OPTION VALUE="06">06
<OPTION VALUE="07">07
<OPTION VALUE="08">08
<OPTION VALUE="09">09
<OPTION VALUE="10">10
<OPTION VALUE="11">11
<OPTION VALUE="12">12
<OPTION VALUE="13">13
<OPTION VALUE="14">14
<OPTION VALUE="15">15
<OPTION VALUE="16">16
<OPTION VALUE="17">17
<OPTION VALUE="18">18
<OPTION VALUE="19">19
<OPTION VALUE="20">20
<OPTION VALUE="21">21
<OPTION VALUE="22">22
<OPTION VALUE="23">23
<OPTION VALUE="24">24
<OPTION VALUE="25">25
<OPTION VALUE="26">26
<OPTION VALUE="27">27
<OPTION VALUE="28">28
<OPTION VALUE="29">29
<OPTION VALUE="30">30
<OPTION VALUE="31">31
</SELECT>
&nbsp;
<SELECT NAME="event_year2">
<OPTION VALUE="">select a year
<OPTION VALUE="2002">2002
<OPTION VALUE="2003">2003
<OPTION VALUE="2004">2004
<OPTION VALUE="2005">2005
<OPTION VALUE="2006">2006
<OPTION VALUE="2007">2007
<OPTION VALUE="2008">2008
<OPTION VALUE="2009">2009
<OPTION VALUE="2010">2010
</SELECT></TD></TR>
<TR><TD>
<?php
/*********************DEBUG************************
	echo $row['ship_add'] ."<br />";
	echo "<B>Address:</B> <input type=text name=ship_add value=". $row['ship_add'] ."></br />";
	echo "<B>City:</B> <input type=text name=ship_city  value=". $row['ship_city'] ."></br />";
	echo "<B>State:</B> <input type=text name=ship_state value=". $row['ship_state'] ."></br />";
	echo "<B>Zip Code:</B> <input type=text name=ship_zip value=". $row['ship_zip'] ."></br /><br />";	
**************************************************/
?>
<TABLE width=400><TR>
<BR>

<?php
	$_POST['brideFname'];
	$_POST['brideLname'];
	$_POST['groomFname'];
	$_POST['groomLname'];
	$_POST['ship_add'];
	$_POST['ship_city'];
	$_POST['ship_state'];
	$_POST['ship_zip'];

	$_POST['event_month2'];
	$_POST['event_day2'];
	$_POST['event_year2'];
	$_POST['event_month'];
	$_POST['event_day'];
	$_POST['event_year'];
	$_POST['editID'];
?>

<TD align=center valign=left><input type=submit value="Save"></form></TD>
<TD align=center valign=right>
<form method=post action=updateRegistry.php?regID=<?php echo $uID; ?>><input type=submit value="Continue to Registry >>">
</form>
</TD></TR></TABLE>

</TD></TR></TABLE>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Looking at what you just posted, there are a host of things that are going to make that page not work. The most prominent is the use of the $row array, mainly because it is not set anywhere in the page. A var_dump() of row should show you what is in the var.

At this point, you are going to need to be able to provide the script with the information for $row. From there it should be a snap. Show the code that sets $row so we can move from there.
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

i changed the value= in the input fields from $row[''] to $_POST['']. and it still didn't work. it does the same thing. but i think i figured it out.... i don't have the names in the form....
Post Reply