Can't get Update to work on this script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidPr
    New Member
    • Mar 2007
    • 155

    Can't get Update to work on this script

    I've been working on this script all day with no luck at all. I have to successfully test both parts of this script in order to finish this project. I haven't tested the delete part yet, because I can't get the update part to work. I just can't figure out why this will not update the database.

    [PHP]<?php
    include("includ es/dbconnect.php") ;

    if(!isset($cmd) )
    {
    $result = mysql_query("se lect * from $table order by title");
    while($r = mysql_fetch_arr ay($result))
    {

    $id = $r["id"];
    $title = stripslashes($r["title"]);

    echo "

    <tr>
    <td width='20%' align='right'>I D#: </td>
    <td>$id</td>
    </tr>
    <tr>
    <td width='20%' align='right'>P icture Title: </td>
    <td>$title</td>
    </tr>
    <tr>
    <td width='20%' align='right'>P icture File Name: </td>
    <td>$image_name </td>
    </tr>
    <tr>
    <td width='20%' align='right'>[ <a href='edit_pict ure.php?cmd=edi t&id=$id'>Edi t</a> ]</td>
    <td>[ <a href='edit_pict ure.php?cmd=del ete&id=$id'>Del ete</a> ]</td>
    </tr>
    <tr><td colspan='4' width='100%' style='border-top: 1px solid #666'>&nbsp;</td></tr>

    ";

    }
    }
    ?>

    </table>

    <?php

    echo "<h3>Edit a Picture</h3>";
    if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
    {
    if (!isset($_POST["submit"]))
    {
    $id = $_GET["id"];
    $sql = "SELECT * FROM $table WHERE id=$id";
    $result = mysql_query($sq l);
    $myrow = mysql_fetch_arr ay($result);
    ?>

    <center>
    <table width="95%" cellspacing="0" cellpadding="3" border="0">
    <tr><td>
    <form action="edit_pi cture.php" method="post">

    <input type="hidden" name="id" value="<?php echo $myrow['id'] ?>">


    Picture Title:<br>
    <input type="text" name="title" VALUE="<?php echo stripslashes($m yrow['title']) ?>" size="55"><br>< br>

    Picture Description:<br >
    <TEXTAREA name="desc" ROWS=2 COLS=70 wrap=virtual><? php echo stripslashes($m yrow['desc']) ?></TEXTAREA><br><b r>

    <input type="hidden" name="cmd" value="edit">
    <input type="submit" name="submit" value="submit">
    </form>
    </td>
    </tr>
    </table>
    </center>

    <?php
    }
    ?>

    <?php

    if ($_POST["$submit"])
    {

    $title = addslashes($_PO ST['title']);
    $desc = addslashes($_PO ST['desc']);

    $sql = "UPDATE $table SET title='$title', desc='$desc' WHERE id=$id";

    $result = mysql_query($sq l);
    echo "<br><br><p align='center'> <span style='color:re d'>Information updated.</span><br><br><a href='edit_pict ure.php'>Edit another Picture</a></p>";

    }
    }

    ?>

    <?php
    if($_GET["cmd"]=="delete")
    {

    $image_name = $_GET["image_name "];

    $photo_dir = ('/mysite.com/gallery/image_uploads/');
    $thumb_dir = ('/mysite.com/gallery/thumb_uploads/');

    unlink($photo_d ir.$image_name) ;
    unlink($thumb_d ir.$image_name) ;

    $sql = "DELETE FROM $table WHERE image_name=$ima ge_name";
    $result = mysql_query($sq l);

    echo "<br><br><p align='center'> <span style='color:re d'><b>The Picture has been Deleted!<br><br >";
    }

    ?>[/PHP]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, David.

    Try echoing the output of mysql_error() after the UPDATE query to see if your query generated an error.

    Comment

    • DavidPr
      New Member
      • Mar 2007
      • 155

      #3
      Error, query failed mysql said You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='Red Necklace.' WHERE id=''' at line 1

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, David.

        'desc' is a MySQL keyword. To use desc as a field name, you have to enclose it in backticks, like this:

        [code=mysql]
        WHERE `desc` = 'Red Necklace'
        [/code]

        Comment

        • DavidPr
          New Member
          • Mar 2007
          • 155

          #5
          OK, it's working. But the delete part isn't.

          I'm getting this error message:
          [PHP]Warning: unlink(/mysite.com/gallery/image_uploads/): Is a directory in /home/user/mysite.com/admin/edit_picture.ph p on line 117

          Warning: unlink(/mysite.com/gallery/thumb_uploads/): Is a directory in /home/user/mysite.com/admin/edit_picture.ph p on line 118[/PHP]

          Here's the code:
          [PHP]if($_GET["cmd"]=="delete")
          {

          $image_name = $_GET["image_name "];

          $photo_dir = ('/mysite.com/gallery/image_uploads/');
          $thumb_dir = ('/mysite.com/gallery/thumb_uploads/');

          unlink($photo_d ir.$image_name) ;
          unlink($thumb_d ir.$image_name) ;[/PHP]

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, David.

            You're getting these errors because $image_name is undefined.

            Comment

            • DavidPr
              New Member
              • Mar 2007
              • 155

              #7
              I'm using this further up.
              [PHP]<a href='edit_pict ure.php?cmd=del ete&image_name= $image_name'>De lete</a>[/PHP]

              Comment

              • DavidPr
                New Member
                • Mar 2007
                • 155

                #8
                Well, I've looked through all 3 of my php books and have searched the Web, but I haven't come across anything about deleting an image from the server. Is this beyond PHP's ability?

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, David.

                  Originally posted by DavidPr
                  I haven't come across anything about deleting an image from the server. Is this beyond PHP's ability?
                  Have a look at unlink().

                  Comment

                  • bonski
                    New Member
                    • Jun 2007
                    • 53

                    #10
                    hello davidpr,

                    instead of passing na image_name, you use the image_id.. for more exact querying...

                    this line has an error... it doesn't pass the value of the variable $image_name instead it passes the string '$image_name' because you didn't put it inside a PHP tag. so change this line
                    Code:
                    <a href="edit_picture.php?cmd=delete&image_name=$image_name">

                    to this one... but using the field image_id..

                    Code:
                    <a href="edit_picture.php?cmd=delete&img_id=<?=$image_id?>">
                    then to your delete script...

                    [PHP]
                    <?php
                    if($_GET['cmd']=="delete" && isset($_GET['img_id'])){

                    $image_id = $_GET["img_id"];

                    //there's no need for parenthesis.. so i took it out.. ^__^
                    $photo_dir = '/mysite.com/gallery/image_uploads/';
                    $thumb_dir = '/mysite.com/gallery/thumb_uploads/';

                    //getting the data of the image
                    $image_sql = "SELECT * FROM $table WHERE image_id='".$im age_id."'";
                    $image_qry = mysql_query($im age_sql);
                    $row = @mysql_fetch_ob ject($image_qry );

                    //name of image file
                    $image_name = $row->image_name;

                    if(unlink($phot o_dir.$image_na me) && unlink($thumb_d ir.$image_name) ){

                    $sql = "DELETE FROM $table WHERE image_id=$image _id";
                    $result = mysql_query($sq l);

                    if($result){
                    echo "<br><br><p align='center'> <span style='color:re d'><b>The Picture has been Deleted!<br><br >";
                    }

                    }else{
                    echo 'ERROR: Unable to delete image file!';
                    }

                    } //end of isset

                    ?>[/PHP]


                    so if this works.. just modify its format or variables to where you're comfortable with.. ^___^..

                    bonski
                    Last edited by bonski; Jun 22 '07, 02:36 AM. Reason: doesnt appear on post

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      [nitpick]
                      [code=html]<a href="edit_pict ure.php?cmd=del ete&img_id=<?=$ image_id?>">[/code] only works if you have short tags enabled. Otherwise, you'll have to do this: [code=html]<a href="edit_pict ure.php?cmd=del ete&img_id=<?ph p echo $image_id; ?>">[/code]
                      [/nitpick]

                      Comment

                      • DavidPr
                        New Member
                        • Mar 2007
                        • 155

                        #12
                        I had to change it to this:
                        [PHP]<?php
                        if($_GET['cmd']=="delete" && isset($_GET['img_id'])){

                        $id = $_GET["img_id"];

                        //there's no need for parenthesis.. so i took it out..
                        $photo_dir = ('$_SERVER['DOCUMENT_ROOT'].'/gallery/image_uploads/');
                        $thumb_dir = ('$_SERVER['DOCUMENT_ROOT'].'/gallery/thumb_uploads/');

                        //getting the data of the image
                        $image_sql = "SELECT * FROM $table WHERE id='".$id."'";
                        $image_qry = mysql_query($im age_sql);
                        $row = @mysql_fetch_ob ject($image_qry );

                        //name of image file
                        $image_name = $row->image_name;

                        if(unlink($phot o_dir.$image_na me) && unlink($thumb_d ir.$image_name) ){

                        $sql = "DELETE FROM $table WHERE id=$id";
                        $result = mysql_query($sq l);

                        if($result){
                        echo "<br><br><p align='center'> <span style='color:re d'><b>The Picture has been Deleted!<br><br >";
                        }

                        }else{
                        echo 'ERROR: Unable to delete image file!';
                        }

                        } //end of isset

                        ?>[/PHP]
                        The below, edited with my website was returning = no such directory.
                        Of course, what I changed it to also doesn't work.

                        [PHP]$photo_dir = /mysite.com/gallery/image_uploads/;
                        $thumb_dir = /mysite.com/gallery/thumb_uploads/;[/PHP]

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          Heya, David.

                          Looks pretty good. Is it working the way you expected?

                          Comment

                          • bonski
                            New Member
                            • Jun 2007
                            • 53

                            #14
                            ^_____^... thats gonna work...!

                            Comment

                            • DavidPr
                              New Member
                              • Mar 2007
                              • 155

                              #15
                              Here's the entire code, I think it'll help.

                              The only thing that's missing is my real website here:[PHP]$photo_dir = '/./gallery/image_uploads/';
                              $thumb_dir = '/./gallery/thumb_uploads/';[/PHP]

                              I've tried setting the path several different ways and each time either the directory or the file doesn't exist. Although they do exist. This latest time nothing came up except for my website heading.

                              [PHP]<?php
                              include("includ es/dbconnect.php") ;

                              if(!isset($cmd) )
                              {
                              $result = mysql_query("se lect * from $table order by title");
                              while($r = mysql_fetch_arr ay($result))
                              {

                              $id = $r["id"];
                              $title = stripslashes($r["title"]);

                              echo "

                              <tr>
                              <td width='20%' align='right'>I D#: </td>
                              <td>$id</td>
                              </tr>
                              <tr>
                              <td width='20%' align='right'>P icture Title: </td>
                              <td>$title</td>
                              </tr>
                              <tr>
                              <td width='20%' align='right'>P icture File Name: </td>
                              <td>$image_name </td>
                              </tr>
                              <tr>
                              <td width='20%' align='right'>[ <a href='edit_pict ure.php?cmd=edi t&id=$id'>Edi t</a> ]</td>
                              <td>[ <a href='edit_pict ure.php?cmd=del ete&id=$id'>Del ete</a> ]</td>
                              </tr>
                              <tr><td colspan='4' width='100%' style='border-top: 1px solid #666'>&nbsp;</td></tr>

                              ";

                              }
                              }
                              ?>

                              </table>

                              <?php

                              if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
                              {
                              if (!isset($_POST["submit"]))
                              {
                              $id = $_GET["id"];
                              $sql = "SELECT * FROM $table WHERE id='$id'";
                              $result = mysql_query($sq l);
                              $myrow = mysql_fetch_arr ay($result);
                              ?>


                              <h3>Edit a Picture</h3>

                              <center>
                              <table width="95%" cellspacing="0" cellpadding="3" border="0">
                              <tr><td>
                              <form action="edit_pi cture.php" method="post">

                              <input type="hidden" name="id" value="<? echo $myrow['id']; ?>">

                              Picture Title:<br>
                              <input type="text" name="title" VALUE="<? echo stripslashes($m yrow['title']); ?>" size="55"><br>< br>

                              Picture Description:<br >
                              <TEXTAREA name="image_des c" ROWS=2 COLS=70 wrap=virtual><? echo stripslashes($m yrow['image_desc']); ?></TEXTAREA><br><b r>

                              Picture Name: (This cannot be changed. It's here so you'll know you're editing the correct image.)<br>
                              <input type='text' size='55' name='image-name' value='<? echo stripslashes($m yrow['image_name']); ?><br><br>


                              <input type="hidden" name="cmd" value="edit">
                              <input type="submit" name="submit" value="submit">
                              </form>
                              </td>
                              </tr>
                              </table>
                              </center>

                              <?php
                              }
                              ?>

                              <?php

                              if ($_POST["$submit"])
                              {

                              $id = $_POST['id'];
                              $title = addslashes($_PO ST['title']);
                              $image_desc = addslashes($_PO ST['image_desc']);

                              $query = "UPDATE $table SET title='$title', image_desc='$im age_desc' WHERE id=$id";

                              $result = mysql_query($qu ery) or die('Error, query failed mysql said <b>'.mysql_erro r().'</b>');

                              echo "<br><br><p align='center'> <span style='color:re d'>Information updated.</span><br><br><a href='edit_pict ure.php'>Edit another Picture</a></p>";

                              }
                              }

                              ?>

                              <?php
                              if($_GET['cmd']=="delete" && isset($_GET['img_id'])){

                              $id = $_GET["id"];

                              //there's no need for parenthesis.. so i took it out..
                              $photo_dir = '/./gallery/image_uploads/';
                              $thumb_dir = '/./gallery/thumb_uploads/';


                              //getting the data of the image
                              $image_sql = "SELECT * FROM $table WHERE id='".$id."'";
                              $image_qry = mysql_query($im age_sql);
                              $row = @mysql_fetch_ob ject($image_qry );

                              //name of image file
                              $image_name = $row->image_name;

                              if(unlink($phot o_dir.$image_na me) && unlink($thumb_d ir.$image_name) ){

                              $sql = "DELETE FROM $table WHERE id=$id";
                              $result = mysql_query($sq l);

                              if($result){
                              echo "<br><br><p align='center'> <span style='color:re d'><b>The Picture has been Deleted!<br><br >";
                              }

                              }else{
                              echo 'ERROR: Unable to delete image file!';
                              }

                              } //end of isset

                              ?>[/PHP]

                              ***EDITED***

                              [PHP]if($_GET['cmd']=="delete" && isset($_GET['img_id'])){[/PHP]

                              I forgot to change the img_id to just id. When I did that along with setting the below it worked! Thank you for the help.

                              [PHP]$photo_dir = $_SERVER['DOCUMENT_ROOT'].'/gallery/image_uploads/';
                              $thumb_dir = $_SERVER['DOCUMENT_ROOT'].'/gallery/thumb_uploads/';[/PHP]

                              Comment

                              Working...