PHP code not updating database, is it my sql or my php?

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
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

PHP code not updating database, is it my sql or my php?

Post by robster »

Hi everyone :)

i've got a snippet of my code below for you to peruse. The problem I'm having with it is the modification of the database.

If you read the section in hte large code lump below that says...

Code: Select all

//if the user presses Apply changes button they update database 
if ($Submit == "Apply your changes")
...you will see the area I'm talking about.

I press the EDIT button, the info is displayed in my input boxes, I edit those boxes but the database does not update. I've looked and looked and looked at it for days now but can't see it. I'm hoping it's something really easy.

If anyone can suggest anything I'd really appreciate some guidance.

Thanks :)

Rob

Code: Select all

$Submit = (isset($_POST['Submit'])?$_POST['Submit']:""); 

		

		if (is_loggedin()) 
		{
		
		
		
		
		print "<form name="editentry" method="post" action="current_upload_edit.php">";
		
			//get the number of the entry we are to edit from the form on the last page
			$entrynumber = stripslashes($_POST['entrynumber']);
			
			// connect to current table		
			$connection = mysql_connect($dbhost, $dbusername, $dbpassword);
			
			
			mysql_select_db($dbname);
			$sql = "SELECT * FROM current WHERE id = $entrynumber";
			
			
			
			$content = mysql_query($sql);
			$Xcontent = mysql_fetch_array($content);
			
			
			
					$id = $Xcontent["id"];									
					$uid = $Xcontent["uid"];				
					$year = $Xcontent["year"];
					$month = $Xcontent["month"];
					$number = $Xcontent["number"];
					$title =  $Xcontent["title"];
					$animator =  $Xcontent["animator"];
					$hardware = $Xcontent["hardware"];
					$software = $Xcontent["software"];
					$creationtime = $Xcontent["creationtime"];
					$rendertime =  $Xcontent["rendertime"];
					$viewrecommend =  $Xcontent["viewrecommend"];
					$animdescription = $Xcontent["animdescription"];
					$descriptionofcreation = $Xcontent["descriptionofcreation"];
					$popularity =  $Xcontent["popularity"];
					$mpg =  $Xcontent["mpg"];
					$jpg =  $Xcontent["jpg"];
					$rating =  $Xcontent["rating"];
					$ratingtally =  $Xcontent["ratingtally"];
					$ratingavg =  $Xcontent["ratingavg"];
					$ratingafter =  $Xcontent["ratingafter"];

		
		
	
	
	
	
	
				//if the user presses Apply changes button they update database 
				if ($Submit == "Apply your changes")
				{
				
						$animator = stripslashes($_POST['animator']);
						$title = stripslashes($_POST['title']);	
						$hardware = stripslashes($_POST['hardware']);	
						$software = stripslashes($_POST['software']);	
						$creationtime = stripslashes($_POST['creationtime']);	
						$rendertime = stripslashes($_POST['rendertime']);	
						$viewrecommend = stripslashes($_POST['viewrecommend']);	
						$descriptionofcreation = stripslashes($_POST['descriptionofcreation']);	
						$animdescription = stripslashes($_POST['animdescription']);	
						$forum_uid = stripslashes($_POST['forum_uid']);	
						$new_id = stripslashes($_POST['new_id']);
						$sound_id = stripslashes($_POST['sound_id']);
						$sound_year = stripslashes($_POST['sound_year']);
						$sound_month = stripslashes($_POST['sound_month']);	


					
						$animdescription = nl2br($animdescription);
						$descriptionofcreation = nl2br($descriptionofcreation);

						//Update the database 
						$Update = mysql_db_query ($dbname, "UPDATE current SET animator=trim('$animator') WHERE id = $forum_uid");
						$Update = mysql_db_query ($dbname, "UPDATE current SET title=trim('$title') WHERE id = $forum_uid");
						$Update = mysql_db_query ($dbname, "UPDATE current SET hardware=trim('$hardware') WHERE id = $forum_uid");
						$Update = mysql_db_query ($dbname, "UPDATE current SET software=trim('$software') WHERE id = $forum_uid");
						$Update = mysql_db_query ($dbname, "UPDATE current SET creationtime=trim('$creationtime') WHERE id = $forum_uid");
						$Update = mysql_db_query ($dbname, "UPDATE current SET rendertime=trim('$rendertime') WHERE id = $forum_uid");
						$Update = mysql_db_query ($dbname, "UPDATE current SET viewrecommend=trim('$viewrecommend') WHERE id = $forum_uid");
						$Update = mysql_db_query ($dbname, "UPDATE current SET descriptionofcreation=trim('$descriptionofcreation') WHERE id = $forum_uid");
						$Update = mysql_db_query ($dbname, "UPDATE current SET animdescription=trim('$animdescription') WHERE id = $forum_uid");																																																
						
			
					
				}  //end of if Submit == apply your changes

					
					//if the user presses the edit text button they get this 
					if ($Submit == "Edit your text info")
					{
					
						$animator = stripslashes($_POST['animator']);
						$title = stripslashes($_POST['title']);	
						$hardware = stripslashes($_POST['hardware']);	
						$software = stripslashes($_POST['software']);	
						$creationtime = stripslashes($_POST['creationtime']);	
						$rendertime = stripslashes($_POST['rendertime']);	
						$viewrecommend = stripslashes($_POST['viewrecommend']);	
						$descriptionofcreation = stripslashes($_POST['descriptionofcreation']);	
						$animdescription = stripslashes($_POST['animdescription']);	
						$forum_uid = stripslashes($_POST['forum_uid']);	
						$new_id = stripslashes($_POST['new_id']);
						$sound_id = stripslashes($_POST['sound_id']);
						$sound_year = stripslashes($_POST['sound_year']);
						$sound_month = stripslashes($_POST['sound_month']);	
					
						echo "<br /> Here you can edit your animation information...<br /><br />";
						$action = "";
						
						
	

						//Name
						print "<b>Your Name</b><br>";
						print "<input type="text" name="animator" size="45" value="$animator"><br /><br />";
						print "</text>";


						//title
						print "<b>Title of your animation</b><br>";
						print "<input type="text" name="title" size="45" value="$title"><br /><br />";
						print "</text>";

						// Hardware
						print "<b>What Hardware or Tools did you use?</b><br>";
			       		print "<textarea name="hardware" cols="45" rows="5" wrap="VIRTUAL">";
						print "$hardware";
						print "</textarea>";


						// Software
						print "<br /><br /><b>What Software did you use?</b><br>";
	       				print "<textarea name="software" cols="45" rows="5" wrap="VIRTUAL">";
						print "$software";			
						print "</textarea>";

						//Animation time
						print "<br /><br /><b>How long did it take to create your animation?</b><br>";
						print "<input type="text" name="creationtime" size="45" value="$creationtime"><br /><br />";
						print "</text>";


						//Rendertime
						print "<b>How long did it take to render your animation?</b><br>";
						print "<input type="text" name="rendertime" size="45" value="$rendertime"> <br /> <br />";
						print "</text>";


						//Viewing Recommendations
						print "<b>What are your viewing recommendations? (be creative </b><br>";
						print "<input type="text" name="viewrecommend" size="45" value="$viewrecommend">";
						print "</text>";
	
						//Description of creation
						print "<br /> <br /><b>Description of the creation process</b><br>";
				       	print "<textarea name="descriptionofcreation" cols="45" rows="10" wrap="VIRTUAL">";
						print "$descriptionofcreation";
						print "</textarea>";
	
						//Description of animation
						print "<br /> <br /><b>Description of Animation (Story/Motivation/etc)</b><br>";
				       	print "<textarea name="animdescription" cols="45" rows="10" wrap="VIRTUAL">";
						print "$animdescription";			
						print "</textarea>";
			
						//extra's
						print "<input type="hidden" name="forum_uid" value="$member_id" />";
						print "<input type="hidden" name="new_id" value="$new_number" />";		
						print "<input type="hidden" name="sound_id" value="$sound_id" />";		
						print "<input type="hidden" name="sound_year" value="$sound_year" />";		
						print "<input type="hidden" name="sound_month" value="$sound_month" />";								
						print "<input type="hidden" name="entrynumber" value="$entrynumber" />";														

						//buttons
						print "<br /><input type="submit" name="Submit" value="Apply your changes"><br />";
						print "(If you don't want to change anything here, simply press BACK in your browser to cancel any changes)";
		

						
						
						
					}
					else
					{
						echo "<p>Your animation details</p>";						
						echo "<b>Animation title</b> = $title<br />";
						echo "<b>Your real name</b> = $animator<br />";
						echo "<b>Hardware used</b> = $hardware<br />";
						echo "<b>Software used</b> = $software<br />";
						echo "<b>Creation time</b> = $creationtime<br />";
						echo "<b>Render time</b> = $rendertime<br />";
						echo "<b>Viewing recommendations</b> = $viewrecommend<br />";
						echo "<b>Animation description</b> = $animdescription<br />";
						echo "<b>Description of creation</b> = $descriptionofcreation<br />";

	
						// the values to be sent on again
						print "<input type="hidden" name="animator" value="$animator" />";
						print "<input type="hidden" name="title" value="$title" />";
						print "<input type="hidden" name="hardware" value="$hardware" />";
						print "<input type="hidden" name="software" value="$software" />";
						print "<input type="hidden" name="creationtime" value="$creationtime" />";
						print "<input type="hidden" name="rendertime" value="$rendertime" />";
						print "<input type="hidden" name="viewrecommend" value="$viewrecommend" />";
						print "<input type="hidden" name="descriptionofcreation" value="$descriptionofcreation" />";
						print "<input type="hidden" name="animdescription" value="$animdescription" />";
						print "<input type="hidden" name="forum_uid" value="$forum_uid" />";
						print "<input type="hidden" name="new_id" value="$new_id" />";
						print "<input type="hidden" name="sound_id" value="$sound_id" />";		
						print "<input type="hidden" name="sound_year" value="$sound_year" />";		
						print "<input type="hidden" name="sound_month" value="$sound_month" />";
						print "<input type="hidden" name="entrynumber" value="$entrynumber" />";							
	

						
						
						

						print "<br /><input type="submit" name="Submit" value="Edit your text info"><br />";
						echo "</form>";
				

					//end button if else bit
					}		
					
					
				
	
		}  //end of , has user logged in?
	
		else //user is NOT logged in
		{
			echo '<p>Error: you are not logged in, to edit your entry you must be logged in.</p>';
		}
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Whats the outcome of the following?

Code: Select all

echo '<pre>';
echo 'PHP Version: '.phpversion()."\n";
echo 'Display Errors: '.(ini_get('display_errors') == '1' ? 'On' : 'Off')."\n";
echo 'Error Level: '.(ini_get('error_reporting') == '2047' ? 'E_ALL' : 'Not E_ALL')."\n";
echo 'Register Globals: '.(ini_get('register_globals') == '' ? 'Off' : 'On')."\n";
echo '</pre>';
If Register Globals sais Off, you need to change $Submit to it's $_POST equilant. Goes for everything sendt through a form. I'm leaving you with that hint for you to play with. ;)

Edited; Take in consideration that:

Code: Select all

// the following
                  $Update = mysql_db_query ($dbname, "UPDATE current SET animator=trim('$animator') WHERE id = $forum_uid");
                  $Update = mysql_db_query ($dbname, "UPDATE current SET title=trim('$title') WHERE id = $forum_uid"); 
// can be rewritten as
                  $Update = mysql_db_query ($dbname, "UPDATE current SET animator=trim('$animator'), title=trim('$title') WHERE id = $forum_uid");
This would generate much less calls to the database thatn doing multible updates...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Using mysql_error() should tell you what's wrong, e.g
$Update = mysql_db_query ($dbname, "UPDATE current SET animator=trim('$animator') WHERE id = $forum_uid") or die(mysql_error());

I also tend to do :
$sql = "some query here";
mysql_query($sql) or die(mysql_error());
That way you can echo $sql; to help debugging.

Btw, mysql_db_query() has been deprecated since PHP 4.0.6 ;)
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Actually you helped me with a recent thread about that (thanks again :)) but this time (on my linux server) the results show:

Code: Select all

PHP Version: 4.3.3
Display Errors: On
Error Level: Not E_ALL
Register Globals: On
Any other ideas? I'm so stumped! :|
(thanks for the response)

Rob
Fataqui
Forum Newbie
Posts: 10
Joined: Fri Jan 09, 2004 7:47 pm

Post by Fataqui »

Hi

A few things I see....

1. You run this query *$sql = "SELECT * FROM current WHERE id = $entrynumber";*, even when you do the update, which you should not do!
2. You use multi 'UPDATE' querys to update the same row over and over again, which should be done in one 'UPDATE' query!
3. don't use mysql_fetch_array if you will not use the array numeric 'keys', use mysql_fetch_assoc instead!
4. you can greatly shorten this by using tools php gives you to optimize your code.
5. Just changing around your if() blocks to do things only when they should be done will fix why your 'UPDATE' query does not work now!


F!
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

Hi again :)

I haven't replied to these as yet becuase I've been trying to work through it before crying for help again. I've now spent days and days doing this page, failing, doing some other pages, coming back to this, failing, other pages, etc etc...

I've decided I'm going to have to bite the bullet and ask someone...

What order would by if statements have to be in for the update query to work? I just can't figure it out. I feel like I've tried every possible combination but obviously haven't :\

Thanks a ton, I hope someone knows the answer....


:)


Rob
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i'm sorry man, i've tried looking at your code, too many lines, too repetative, too hard to understand
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

Why don't you trim the variables when you assign them into variables from the post? IE,

Code: Select all

$foo = trim(stripslashes($_POST['bar']));
Also, why don't you update the database with a single query, instead of all of those single ones? You can get away with this because they are all going to the same table...Here is an example from my script that updates things in the database.

Code: Select all

update clanprofile2 SET
firstname = '$firstname',
lastname = '$lastname',
middleinit = '$midinit',
birthyear = '$yearofbirth',
socommap = '$socommap',
sealswep = '$socomsealsweapon',
terrwep = '$socomterroristsweapon',
website = '$website',
hometown = '$hometown',
picture = '$picture',
gender = '$gender',
birthday = '$birthday',
occupation = '$occupation',
bands = '$bands',
tvshows = '$tvshows',
movies = '$movies',
bgcolor = '$bgcolor',
textcolor = '$textcolor',
picorcolor = '$picorpicture',
music = '$music' 
WHERE socomname = '$username'");
This should cut down on both the complexity of the script and the speed of the script.
Post Reply