Updating a field in a database not working

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 758 Views - Last Post: 12 November 2012 - 03:29 AM Rate Topic: -----

#1 phpnovice2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 12

Updating a field in a database not working

Posted 09 November 2012 - 08:44 AM

I have a form that should allow users of my site to change a field in my database

html:
           <form action="edit.php" method="POST"> 
			
                        Lyrics: <br>
			<textarea class="textArea" rows="25" cols="70" name="thelyrics"> <?php echo $lyrics;?></textarea><br>
			<input type="submit" class="signIn3" value="Submit Corrections!" name="edit"/>
			
		
			
	   </form>


As you can see, by default, this text area displays the lyrics from the database. All the users have to do is scroll through the
textarea and find a lyric they want to change and change it. Then they can hit submit. Next I want to Update the database with
their "corrections".


PHP:

<?php

$theLyrics = $_POST['thelyrics'];
	$edit = $_POST['edit']; // This is the submit button
	

//if the user submits the form
	if ($edit){
		
		
                   
                if (!empty($theLyrics)){
		//add to database using update query!
		

                //The part I'm unsure of. (I'm new to PHP & MySQL)
                $query = ("UPDATE lyricdata SET `lyrics` = '$theLyrics' WHERE `page_name` = '$page'");


                //The $page variable is a value I passed from another page. It stored the URL of that page. I used $_GET to
                pass it to this page so I could use that value to find where to update the lyrics field in my database.

		mysql_query($query) or die ("NOT UPDATED!");
		
		
		
		
		
		//this works, if the user has submitted the textarea with nothing in it it displays the error.
		} else if (empty($theLyrics)) {
		//display error saying "You didn't enter any lyrics"
		
		echo "<div id='fail'><center>You didn't enter any lyrics</center></div>";
		
		
		}
		
		}

//prevents people directly accessing the page in the URL bar.
	else{
	header("Location: http://localhost/home");
	}


?>



This doesn't work, it doesn't update the records.

As always, any guidance is appreciated.

This post has been edited by Dormilich: 09 November 2012 - 02:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Updating a field in a database not working

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Updating a field in a database not working

Posted 09 November 2012 - 08:49 AM

Hello,

Firstly, please use code tags. It makes reading your code so much easier for us.

Next, the following line...
$query = ("UPDATE lyricdata SET `lyrics` = '$theLyrics' WHERE `page_name` = '$page'");

Need only be...
$query = "UPDATE lyricdata SET `lyrics` = '$theLyrics' WHERE `page_name` = '$page'";

.. as it's not a function, you're declaring a string.

Do you have any errors being kicked out in your PHP logs?

This post has been edited by Kruithne: 09 November 2012 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 phpnovice2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 12

Re: Updating a field in a database not working

Posted 09 November 2012 - 08:55 AM

That's odd, I used code tags but they didn't display my code correctly.
I forgot a line of code I had in after this part:

$query = ("UPDATE lyricdata SET `lyrics` = '$theLyrics' WHERE `page_name` = '$page'");


The next line is:

mysql_query($query) or die ("NOT UPDATED!");

This post has been edited by Dormilich: 09 November 2012 - 02:09 PM
Reason for edit:: please use [CODE] [/CODE] tags when posting code

Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Updating a field in a database not working

Posted 09 November 2012 - 08:57 AM

Hello, you're trying to use HTML code tags, you need to use BB code tags. Look at the background of the 'reply' box, it shows you how to do it.

This post has been edited by Kruithne: 09 November 2012 - 08:58 AM

Was This Post Helpful? 0
  • +
  • -

#5 phpnovice2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 12

Re: Updating a field in a database not working

Posted 09 November 2012 - 09:13 AM

Oh right I see. I'll repost my topic below:
___________________________________________

I have a form that should allow users of my site to change a field in my database

html:

<form action="edit.php" method="POST"> 

Lyrics: <br>
<textarea class="textArea" rows="25" cols="70" name="thelyrics"> <?php echo $lyrics;?></textarea><br>
<input type="submit" class="signIn3" value="Submit Corrections!" name="edit"/>



</form>



As you can see, by default, this text area displays the lyrics from the database. All the users have to do is scroll through the
textarea and find a lyric they want to change and change it. Then they can hit submit. Next I want to Update the database with
their "corrections".


PHP:

<?php

$theLyrics = $_POST['thelyrics'];
$edit = $_POST['edit']; // This is the submit button


//if the user submits the form
if ($edit){



if (!empty($theLyrics)){
//add to database using update query!


//The part I'm unsure of. (I'm new to PHP & MySQL)
$query = ("UPDATE lyricdata SET `lyrics` = '$theLyrics' WHERE `page_name` = '$page'");


//The $page variable is a value I passed from another page. It stored the URL of that page. I used $_GET to
pass it to this page so I could use that value to find where to update the lyrics field in my database.

mysql_query($query) or die ("NOT UPDATED!");





//this works, if the user has submitted the textarea with nothing in it it displays the error.
} else if (empty($theLyrics)) {
//display error saying "You didn't enter any lyrics"

echo "<div id='fail'><center>You didn't enter any lyrics</center></div>";


}

}

//prevents people directly accessing the page in the URL bar.
else{
header("Location: http://localhost/home");
}


?>




This doesn't work, it doesn't update the records.

As always, any guidance is appreciated.
Was This Post Helpful? 0
  • +
  • -

#6 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Updating a field in a database not working

Posted 09 November 2012 - 09:18 AM

Hello,

I can only assume that you have elsewhere connected to your MySQL database. As far as I can see there are no 'errors' above, if I create a fake database within my environment and try to run your code, it functions as expected.

Could you perhaps throw a small dump of your database here so we might take a look at that?
Was This Post Helpful? 0
  • +
  • -

#7 phpnovice2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 12

Re: Updating a field in a database not working

Posted 09 November 2012 - 09:20 AM

Yes I have included a "connect.php" page in all pages on my site that establishes a connection with the database.

I don't understand what you mean by throwing a "small dump" of my database. Do you want me to upload an SQL file?

This post has been edited by Dormilich: 09 November 2012 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Updating a field in a database not working

Posted 09 November 2012 - 09:23 AM

If that's easier for you yes, anything that describes your database/tables in question so that I can see what your script is having to interact with.
Was This Post Helpful? 0
  • +
  • -

#9 phpnovice2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 12

Re: Updating a field in a database not working

Posted 09 November 2012 - 09:31 AM

I have attached an image below:

This post has been edited by Dormilich: 09 November 2012 - 02:10 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#10 phpnovice2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 12

Re: Updating a field in a database not working

Posted 09 November 2012 - 09:39 AM

http://imageshack.us/f/196/dbpicf.png

This post has been edited by Dormilich: 09 November 2012 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#11 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Updating a field in a database not working

Posted 09 November 2012 - 09:41 AM

Not sure if I am just being dense on this one but I cannot see any issue, could you perhaps post all of your application (only voiding things that are 100% unrelated). Censor out any important information such as database connection details.
Was This Post Helpful? 0
  • +
  • -

#12 phpnovice2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 12

Re: Updating a field in a database not working

Posted 09 November 2012 - 09:55 AM

View PostKruithne, on 09 November 2012 - 09:41 AM, said:

Not sure if I am just being dense on this one but I cannot see any issue, could you perhaps post all of your application (only voiding things that are 100% unrelated). Censor out any important information such as database connection details.


This is the entire "edit.php" page.

<?php 



include "connect.php";
include "header.html";

$page = $_GET['getpage']; //This value is posted from another page, which I will post the code from below this one.

$getLyrics = mysql_query("SELECT * FROM lyricdata WHERE page_name = '$page'");

WHILE($rows = mysql_fetch_array($getLyrics)):

             

												$artist = $rows['artist'];
												$title = $rows['title'];
												$lyrics = $rows['lyrics'];
												$youtube_id = $rows['youtube'];
												
											
											endwhile;
											
	
	
											



?>


	<html>
	
		<head>
			<title><?php echo $siteName;?> </title> 
	
		</head>
		
		<body>
			<div id="editContainer">
			<h1 class="submitCorrections">Submit Corrections Below: </h1>
			
			<form action="edit.php" method="POST"> 
			Lyrics: <br>
			<textarea class="textArea" rows="25" cols="70" name="thelyrics"> <?php echo $lyrics;?></textarea><br>
			<input type="submit" class="signIn3" value="Submit Corrections!" name="edit"/>
			
			
			</form>
			
			
			</div>
		
		<?php include "footer.php";?>
		</body>
	
	
	
	
	
	
	
	</html>
	
	<?php 
	
	$theLyrics = $_POST['thelyrics'];
	$edit = $_POST['edit'];
	
	if ($edit){
		
		
		//add to database using update query!
		$query = mysql_query("UPDATE lyricdata SET `lyrics` = '$theLyrics' WHERE `page_name` = '$page'");
		
		if ($query){
		echo "success";
		}
		else {
		echo "failed";
		}
		
	
		
		
		
	
		
		
		
		
		if (empty($theLyrics)) {
		//display error saying "You didn't enter any lyrics"
		
		echo "<div id='fail'><center>You didn't enter any lyrics</center></div>";
		
		
		}
		
		}
	else{
	header("Location: http://localhost/home");
	}
	
	
	
	
	?>
[code]


Template.php 

[code]


<!DOCTYPE html>
<?php
	


	include "connect.php";
	include "header.html";

	

	
		$currentFile = $_SERVER["PHP_SELF"];
		$parts = Explode('/', $currentFile);
		$current = $parts[count($parts) - 1];
	
	
	
	
				
				$query = mysql_query("SELECT * FROM lyricdata WHERE page_name = '$current' "); 	
		
			WHILE($rows = mysql_fetch_array($query)):
			
				$artist = $rows['artist'];
				$title = $rows['title'];
				$lyrics = $rows['lyrics'];
		
			
			

			
			endwhile;
			
	

?>

		<html>


		<head>
	
		

		<link rel=stylesheet type="text/css" href="CSS/style.css"/>
		<title> <?php 

		$query = mysql_query("SELECT * FROM lyricdata WHERE page_name = '$current' ");
		
			WHILE($rows = mysql_fetch_array($query)):
			
				$artist = $rows['artist'];
				$title = $rows['title'];
				$lyrics = $rows['lyrics'];
			
			echo $artist." - ".$title;
			
			endwhile;
		
		?>
		
		</title>
</head>
<body>			
					
										<div id="lyricContainer">
										<pre class="pre">
										<table class="table">
										
											<tr> 
												<td class='artist1'>Artist&nbsp; &nbsp;</td><td class='artist'> <?php echo $artist?> </td>
											</tr>
											<tr>
												<td class='title1'>&nbsp;Title &nbsp; &nbsp;&nbsp; </td><td class='title'> <?php echo "'$title'"?></td> 
											</tr>
											
											</table>
										
											
											<br> <?php echo $lyrics?><br><form action="searchResults.php" method="GET"><br><center><a href='search-results.php?searchterm=<?php echo "$artist";?>'>More <?php echo $artist ?> lyrics</a></center></form>
											<div id='disclaimer'> <center><?php echo "'$title' lyrics are provided for personal and/or educational use ONLY. <br>$artist lyrics are property and copywright of their owners."?> </center>
											</div>
											
										</pre>
										</div>
										
										
										<?php 
										$query = mysql_query("SELECT * FROM lyricdata WHERE page_name = '$current' ");
										
									//	$amount_of_rows = mysql_num_rows($query);
										
										WHILE($rows = mysql_fetch_array($query)):

             

												$artist = $rows['artist'];
												$title = $rows['title'];
												$lyrics = $rows['lyrics'];
												$youtube_id = $rows['youtube'];
								
											 
											
								
											 
											endwhile;
											
											
											
											
											if (!empty($youtube_id)) {
											
											echo "<div id='youtube'>
											<object type='application/x-shockwave-flash' style='width:434px; height:300px;' data='http://www.youtube.com/v/$youtube_id'>
											<param name='movie' value='http://www.youtube.com/v/$youtube_id'/>
											<param name='allowFullScreen' value='true' />
											<param name='allowscriptaccess' value='always' />
											</object>
											
											</div>";
											
											
											}
										?>
										
										
										<div id="footer">
										<?php include "footer.php";?>
										</div>	
			
			
			
				
				
				<a href='edit.php?getpage=<?php echo "$current";?>'>Click here to edit these lyrics</a>
</body>



</html>






Sorry I posted two pages at once there.
About half way through it says "template.php".

Everything after that is the template.php page.

This post has been edited by Dormilich: 09 November 2012 - 02:13 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#13 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Updating a field in a database not working

Posted 09 November 2012 - 10:01 AM

Okay, looking over it your $page value does not contain what I think you want it to, in fact, I don't think it contains anything.

When you submit it, you need to send the getpage value with the form, an example below.
<form action="edit.php?getpage=example-artist-example-title-lyrics.php" method="POST">

Then you want to grab that inside your edit.php page before running the query.
$page = $_GET['getpage'];
		$query = mysql_query("UPDATE lyricdata SET `lyrics` = '$theLyrics' WHERE `page_name` = '$page'");


This might be wrong, it's hard to tell with your code being all mushed into one code section on the post.
Was This Post Helpful? 0
  • +
  • -

#14 phpnovice2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 12

Re: Updating a field in a database not working

Posted 09 November 2012 - 10:08 AM

I don't think this would work as it isn't dynamic? :
<form action="edit.php?getpage=example-artist-example-title-lyrics.php" method="POST">

This post has been edited by Dormilich: 09 November 2012 - 02:13 PM
Reason for edit:: removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#15 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Updating a field in a database not working

Posted 09 November 2012 - 10:10 AM

It was an example. It will because it isn't dynamic, but it's not what you want. My point was to emphasis where you need to be putting something, not directly giving you the code.

You'll want to put whichever page you are editing in there, although preferably I would send an ID rather than a page_name, and edit it by that.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2