Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,685 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,258 people online right now. Registration is fast and FREE... Join Now!




Update sql statement is not working

2 Pages V  1 2 >  
Reply to this topicStart new topic

Update sql statement is not working, Update error PHP

we_undertaker
post 28 May, 2008 - 09:59 PM
Post #1


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 34

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)
CODE
<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
CODE

$query = "update categories set Category = '".$_POST['cat']."', Cate_Title = 'Y'  where CateID ='".$_REQUEST['CateID']."'";
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 28 May, 2008 - 10:18 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

we_undertaker
post 28 May, 2008 - 10:21 PM
Post #3


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 34

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

thank you so much
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 28 May, 2008 - 10:23 PM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


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.

CODE

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

Go to the top of the page

we_undertaker
post 29 May, 2008 - 12:00 AM
Post #5


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 34

i read the tutorial , but i still can't solve my problem
can so help me..
thank you so much
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 29 May, 2008 - 12:27 AM
Post #6


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

we_undertaker
post 29 May, 2008 - 12:32 AM
Post #7


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 34

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

thank you so much
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 29 May, 2008 - 10:49 AM
Post #8


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


QUOTE(we_undertaker @ 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


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.
User is offlineProfile CardPM

Go to the top of the page

we_undertaker
post 29 May, 2008 - 06:36 PM
Post #9


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 34

CODE
<?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 - 06:37 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 29 May, 2008 - 06:42 PM
Post #10


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(we_undertaker @ 29 May, 2008 - 10:36 PM) *

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.
CODE

<?php
    $query = "select * from categories";  

This will select EVERYTHING. We don't know what column names you have in this database.
CODE

    $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.

CODE

<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>
User is offlineProfile CardPM

Go to the top of the page

we_undertaker
post 29 May, 2008 - 07:49 PM
Post #11


New D.I.C Head

*
Joined: 6 May, 2008
Posts: 34

i change into this , but still got error
CODE
<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
CODE
<?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
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 29 May, 2008 - 07:58 PM
Post #12


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


Try this for your process
CODE
<?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());
    }
?>

User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:06AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month