Jump to content


Problem with UPDATE


3 replies to this topic

#1 lolzaka

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 07 December 2008 - 05:53 PM

Good Evening,

Im getting really frustrated about the UPDATE query...
Ive searched 9 pages of google :D but still didnt got my answer :)

What I am trying to create is a "edit news page".
When I click on submit, it say the data is inserted but in fact it isnt.

I have 2 files.
Edit.php
<?php include "../config.php"; ?>

	
	<?php
		//variabelen
		mysql_query("SELECT * FROM news WHERE id = '".$_GET['id']."'") or die(mysql_error());
		$id = $_POST["id"]; 
		//Toevoegen

		if (isset($_POST['submit'])){
		mysql_query("UPDATE news SET title = '".$_POST['title']."', post = '".$_POST['post']."' 
				 WHERE id = '$id'") or die(mysql_error());

		//Klaar
		echo "Data Inserted!";
			}
	?>
	Click <a href="../index.php">here to return.</a>


editnewsview.inc.php
<?php include "../../config.php"; ?>

	<?php
		// Get all the data from the "news" table
		$result = mysql_query("SELECT * FROM news WHERE id = '".$_GET['id']."'") or die(mysql_error());
		
		echo "<table border='0' width='400'>";
		

		while($row = mysql_fetch_array( $result )) { 

				?>
				<form method="POST" action="edit.php">
				<input type="hidden" name="id" value="<?php $row['id']; ?>">
				<tr>
				<td>
						<span class="kop">News Title:</span>
				</td>
				</tr>
				<tr>
				<td>
						<span class="content"><input type="text" name="title" class="textfield" value=" <?php echo $row['title']; 
						?>" /></span><br /><br />
				</td>
				</tr>
				<tr>
				<td>
						<span class="kop">News Post:</span>
				</td>
				</tr>
				<tr>
				<td>
						<span class="content"><textarea type="text" name="post" class="textarea" cols="40" rows="20" />
						<?php echo $row['post']; ?></textarea></span>
						<br /><br />
				</td>
				</tr>
				<tr><td><input type="submit" class="submit" name="submit" value="Edit News" /></td></tr>
				<?php
		} 
		
		echo "</table>";
	?>


I really dont know whats wrong, hopefully you could help me :)

Regards,

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 December 2008 - 07:19 PM

I'm going to assume the confusion is here:
		//variabelen
		mysql_query("SELECT * FROM news WHERE id = '".$_GET['id']."'") or die(mysql_error());
		$id = $_POST["id"]; 
		//Toevoegen

		if (isset($_POST['submit'])){
		mysql_query("UPDATE news SET title = '".$_POST['title']."', post = '".$_POST['post']."' 
				 WHERE id = '$id'") or die(mysql_error());

You're selecting something from the news (which near as i can tell is a waste of time), then you're setting $id to $_POST['id'] where just before you were using $_GET['id'].

In other words, everything is working fine, but you need to make sure $id is being set to the right variable.

p.s. You should also look into mysql_real_escape_string() for security purposes.

Edited by rc69, 07 December 2008 - 07:20 PM.


#3 lolzaka

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 08 December 2008 - 04:56 AM

Thanks for the reply :)

It works now ^^ I made it from 2 pages to 1 page, here is the new "working :D" code!

<?php include "../../config.php"; ?>

	<?php
		if (isset($_POST['submit'])){
			$id = $_GET["id"]; 
			mysql_query("UPDATE news SET title = '".$_POST['title']."', post = '".$_POST['post']."' WHERE id = '".$id."'") or die(mysql_error());

			//Klaar
			?> <span class="content"><?php echo "Data Inserted!"; ?><br />Click <a href="../index.php">here to return.</a></span> <?php
		} else {

		// Get all the data from the "members" table
		$result = mysql_query("SELECT * FROM news WHERE id = '".$_GET['id']."'") or die(mysql_error());
		
		echo "<table border='0' width='400'>";
		
		// keeps getting the next row until there are no more to get
		while($row = mysql_fetch_array( $result )) { 
			// Print out the contents of each row into a table
				?>
				<form method="POST" action="editnewsview.php?id=<? echo $_GET['id']; ?>">
				<tr>
				<td>
						<span class="kop">News Title:</span>
				</td&Proxy-Connection: keep-alive Cache-Control: max-age=0 ;
				</tr>
				
				<tr>
				<td>
						<span class="content"><input type="text" name="title" class="textfield" value=" <?php echo $row['title']; 
						?>" /></span><br /><br />
				</td>
				</tr>
				
				<tr>
				<td>
						<span class="kop">News Post:</span>
				</td>
				</tr>
				
				<tr>
				<td>
						<span class="content"><textarea type="text" name="post" class="textarea" cols="40" rows="20" />
						<?php echo $row['post']; ?></textarea></span>
						<br /><br />
				</td>
				</tr>
				
				<tr><td><input type="submit" class=Proxy-Connection: keep-alive
Cache-Control: max-age=0

2submit" name="submit" value="Edit News" /></td></tr>
				<?php
		} 
		
		echo "</table>";
		}
	?>

Edited by lolzaka, 08 December 2008 - 04:57 AM.


#4 Steve Marcano

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Tucson, Arizona

Posted 03 February 2009 - 02:05 PM

I have relatively the same issue and hoping either of you can comment on my post as well.

http://www.pixel2lif...showtopic=44306





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users