13 Replies - 1678 Views - Last Post: 02 June 2008 - 10:32 PM Rate Topic: -----

#1 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Update sql statement is not working

Posted 28 May 2008 - 10:59 PM

i have table categories ( CateID, Category, Cate_Title
1 aaa N
2 bbb N)

below is a form to display data to make a update (change)
<form name="r_form" action="change_cate.php" method="post" enctype="multipart/form-data" onsubmit="return validateForm()">

<table width="90%" border="0" cellpadding="3" cellspacing="3">
			
<?php
	$query = "select * from categories";
	$result = mysql_query($query);
	$num = mysql_num_rows($result);
?>

<?php
	if($num > 0){
	while($row = mysql_fetch_assoc($result)){
?>


<tr class="mainCONTENT"><td class="label" width="100">Category :</td>
	  <td><input name="cat" class="fieldTXTBOX" value="<? echo $row['Category']?>" /></td>
</tr>
					
<?php } } ?>
			
	<tr><td></td>
		  <td><input type="image" border="0" value="submit" onclick="return validateForm();" src="../images/Register_Submit_27.jpg"  style="cursor:crosshair;"></td>
			</tr>

	</table>
</form>



this is my process, but i think i wrong, please help me
$query = "update categories set Category = '".$_POST['cat']."', Cate_Title = 'Y'  where CateID ='".$_REQUEST['CateID']."'";



Is This A Good Question/Topic? 0
  • +

Replies To: Update sql statement is not working

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Update sql statement is not working

Posted 28 May 2008 - 11:18 PM

You never ever want to feed $_POST data directly into your database. This opens up an entire can of worms for security holes. 1st I would suggest that you check for & strip out any html & mysql data from the $_POST variables, & store that into a local variable.
Was This Post Helpful? 0
  • +
  • -

#3 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Re: Update sql statement is not working

Posted 28 May 2008 - 11:21 PM

sori , i just new in PHP , can you explain more

thank you so much
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Update sql statement is not working

Posted 28 May 2008 - 11:23 PM

Here are two tutorials.

This one is great for security in PHP
This one goes over the use of $_POST & $_GET

Also, you want to check to make sure that your mysql result came back with something, rather than just trying to use it.

	$query = "select * from categories";
	$result = mysql_query($query);
	 if($result) {
		$num = mysql_num_rows($result);
	 }
	 else die(mysql_error());


Was This Post Helpful? 0
  • +
  • -

#5 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Re: Update sql statement is not working

Posted 29 May 2008 - 01:00 AM

i read the tutorial , but i still can't solve my problem
can so help me..
thank you so much
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Update sql statement is not working

Posted 29 May 2008 - 01:27 AM

Without knowing how your database is setup, I would say, before you try to change the data, make sure that you can output it 1st. Make sure that your select statement is working. Again, you are performing no error checking on your sql statements.

Do you even have a database connection by the time you hit this code? You need to check for results before you can jump right into changing data.
Was This Post Helpful? 0
  • +
  • -

#7 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Re: Update sql statement is not working

Posted 29 May 2008 - 01:32 AM

my output is working, but when i come to this state i can't update :-s
help me plz...

thank you so much
Was This Post Helpful? 0
  • +
  • -

#8 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Update sql statement is not working

Posted 29 May 2008 - 11:49 AM

View Postwe_undertaker, on 29 May, 2008 - 01:32 AM, said:

my output is working, but when i come to this state i can't update :-s
help me plz...

thank you so much


Build your query and output it to the browser instead of executing it so you can make sure it's building properly. Check your field names in your query and make sure you're using uppercase and lowercase letters in their proper places. A field name of 'cateID' won't accept something that says 'CateID'. They're two different names.

When you echo your query statement out copy and paste it into this forum. That way we can see how it's displaying. We might be able to catch something that you aren't. Also, make sure you're checking the integrity of your post data and not using your post variables directly in your queries. Those tutorials are really good and are very important to understand when programming in PHP.
Was This Post Helpful? 0
  • +
  • -

#9 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Re: Update sql statement is not working

Posted 29 May 2008 - 07:36 PM

<?php
	session_start();
	include("../Register/dbb.php");

	if(isset($_SESSION['userid']) == false)
	{
		echo "<script>window.location=\"../Register/login.php\"</script>";
	} else {
	
for($i=0; $i < $_POST['cate'].length; $i++)

$query="update categories set Category = '".$_POST['cate'][$i]."'";

	if(!mysql_query($query))
	{
	mysql_query($query) or die (mysql_errno().": ".mysql_error()."<BR>"); 

	}
			
}
		
?>


this is my code for process update.
but i got the error 1065: Query was empty

help me plz. thank you so much

This is my form
<td><input type="text" name="cate[]" class="fieldTXTBOX" value="<? echo $row['Category']?>" /></td>

This post has been edited by we_undertaker: 29 May 2008 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Update sql statement is not working

Posted 29 May 2008 - 07:42 PM

View Postwe_undertaker, on 29 May, 2008 - 10:36 PM, said:

This is my form
<td><input type="text" name="cate[]" class="fieldTXTBOX" value="<? echo $row['Category']?>" /></td>


There is still an information gap here... there is stuff we need to know, in order to help you.
<?php
	$query = "select * from categories";  


This will select EVERYTHING. We don't know what column names you have in this database.
	$result = mysql_query($query);
	$num = mysql_num_rows($result);
?>

<?php
	if($num > 0){
	while($row = mysql_fetch_assoc($result)){
?>


This will fill the row array with number, not the column names.

Assuming that 'Category' is a column name, try this.

<form name="r_form" action="change_cate.php" method="post" enctype="multipart/form-data" onsubmit="return validateForm()">

<table width="90%" border="0" cellpadding="3" cellspacing="3">
			
<?php
	$query = "select Category from categories";
	$result = mysql_query($query);
	$num = mysql_num_rows($result);
?>

<?php
	if($num > 0){
	while($row = mysql_fetch_assoc($result)){
?>


<tr class="mainCONTENT"><td class="label" width="100">Category :</td>
	  <td><input name="cat" class="fieldTXTBOX" value="<? echo $row[0]?>" /></td>
</tr>
					
<?php } } ?>
			
	<tr><td> </td>
		  <td><input type="image" border="0" value="submit" onclick="return validateForm();" src="../images/Register_Submit_27.jpg"  style="cursor:crosshair;"></td>
			</tr>

	</table>
</form>


Was This Post Helpful? 0
  • +
  • -

#11 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Re: Update sql statement is not working

Posted 29 May 2008 - 08:49 PM

i change into this , but still got error
<form name="r_form" action="change_cate.php?userid=<? echo $_REQUEST['userid']?>" method="post" enctype="multipart/form-data" onsubmit="return validateForm()">
<table width="90%" border="0" cellpadding="3" cellspacing="3">
			
<?php
$query = "select Category from categories";
$result = mysql_query($query);
$num = mysql_num_rows($result);
?>

<?php
	if($num > 0){
	while($row = mysql_fetch_assoc($result)){
?>


<tr class="mainCONTENT"><td class="label" width="100">Category :</td>
<td><input name="cate" class="fieldTXTBOX" value="<? echo $row[0]?>" /></td>
		
</tr>
			
<?php } } ?>

			
	<tr><td>&nbsp;</td>
   <td><input type="image" border="0" value="submit" onclick="return validateForm();" src="../images/Register_Submit_27.jpg"  style="cursor:crosshair;"></td>
</tr>

	</table>
</form>


and this is my prococess
<?php
	session_start();
	include("../Register/dbb.php");

	if(isset($_SESSION['userid']) == false)
	{
		echo "<script>window.location=\"../Register/login.php\"</script>";
	} else {
	
		

			for($i=0; $i < $_POST['cate'].length; $i++)

	$query="update categories set Category = '".$_POST['cate'][$i]."'";

	if(!mysql_query($query))
	{
	mysql_query($query) or die (mysql_errno().": ".mysql_error()."<BR>"); 

	}
			
	}
		
?>


is something wrong ?
help me , thank you so much
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Update sql statement is not working

Posted 29 May 2008 - 08:58 PM

Try this for your process
<?php
	session_start();
	include("../Register/dbb.php");

	if(isset($_SESSION['userid']) == false) {
		echo "<script>window.location=\"../Register/login.php\"</script>";
	} else {		

		  if(isset($_POST['cate'])) $replace=strip_tags($_POST['cate'];
		  else die("Post did not bring a value");

	  $query="update categories set Category = '$replace';";
		  $result=mysql_query($query);
	  if(!$result) die (mysql_errno()); 
	}
?>


Was This Post Helpful? 0
  • +
  • -

#13 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Re: Update sql statement is not working

Posted 29 May 2008 - 10:18 PM

i still got the error and while page

when i use the code below , my form is not appear any values
><input name="cate" class="fieldTXTBOX" value="<? echo $row[0]?>" /></td>


and when i use code below , then my form appears all the values, then i just change from there.
<input name="cate[]" class="fieldTXTBOX" value="<? echo $row[Category]?>" /></td>
,

is this cos of this one, then i can run the process

This post has been edited by we_undertaker: 29 May 2008 - 10:19 PM

Was This Post Helpful? 0
  • +
  • -

#14 we_undertaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 06-May 08

Re: Update sql statement is not working

Posted 02 June 2008 - 10:32 PM

anyone help me please
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1