Default Re: Mga Tanong tungkol sa PHP, CSS, jquery, HTML at MySQL pasok D2
Pa help sa update code ko po, na-uupdate niya kasi lahat ng nasa tables ko.
ano po problem sa query?



Here's my code for my update, the problem is when i submit, it updates all the data in my table.. what is wrong????

Code:

	<?php 
						include("connect.php");
						if(isset($_POST['submit']))
						{
						$upd="update news set title='$_POST[title]',date='$_POST[date]',post='$_POST[post]'";
						$up=mysql_query($upd);
						$msg="you have updated";
						}
						?>



Reply With Quote

Recommended Answers

All 2 Replies

You need to add a 'WHERE' clause in your statement. For example:

$upd="update news set title='$_POST[title]',date='$_POST[date]',post='$_POST[post]' WHERE id = '$_POST[id]'";

This assumes that there is a hidden field in your form that has the unique value of the entry you are editing.

Edit:
I should also add that it is bad practice to execute sql statements without sanitising the inputs.

You also need your post data in '' and either use {} or '".."' around them:

$upd="update news set title='".$_POST['title']."',date='".$_POST['date']."',post='".$_POST['post']."' WHERE id = '".$_POST['id']."'";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.