Jump to content

Constants


seasexsun

Recommended Posts

Hi:  I have these two constants that I got from a tutorial and that are being called by:  require_once '../library/config.php';

 

define('ALBUM_IMG_DIR', '../images/album/');

// all images inside an album are stored here

define('GALLERY_IMG_DIR', '../images/gallery/');

 

The directories work fine, but what happens is when I from the add-album script "add an album" and upload an image, it only adds it to one table, which is tbl_album but not the other which is tbl_image.  I have tried adding another INSERT INTO in the following script, but I just cannot seem to get it to function properly.  I've gone through every book on my shelf and am pulling my hair out.  Below is the script that is not uploading to both directories but only the tbl_album.  Thanks in advance... and again, both directories and tables are working fine if I query them separately. 

 

<?php

require_once '../library/config.php';

require_once '../library/functions.php';

if(isset($_POST['txtName']))

{

$albumName = $_POST['txtName'];

$albumDesc = $_POST['mtxDesc'];

 

$imgName  = $_FILES['fleImage']['name'];

$tmpName  = $_FILES['fleImage']['tmp_name'];

 

// we need to rename the image name just to avoid

// duplicate file names

// first get the file extension

$ext = strrchr($imgName, ".");

 

// then create a new random name

$newName = md5(rand() * time()) . $ext;

 

    // the album image will be saved here

    $imgPath = ALBUM_IMG_DIR . $newName;

 

// resize all album image

$result = createThumbnail($tmpName, $imgPath, THUMBNAIL_WIDTH);

 

if (!$result) {

echo "Error uploading file";

exit;

}

 

    if (!get_magic_quotes_gpc()) {

        $albumName  = addslashes($albumName);

        $albumDesc  = addslashes($albumDesc);

    } 

 

$query = "INSERT INTO tbl_album (al_name, al_description, al_image, al_date)

  VALUES ('$albumName', '$albumDesc', '$newName', NOW())";

 

    mysql_query($query) or die('Error, add album failed : ' . mysql_error());

   

   

    // the album is saved, go to the album list

echo "<script>window.location.href='index.php?page=list-album';</script>";

exit;

}

?>

 

<form action="" method="post" enctype="multipart/form-data" name="frmAlbum" id="frmAlbum">

    <table width="100%" border="0" cellpadding="2" cellspacing="1" class="table_grey">

        <tr>

            <th width="150">Album Name</th>

            <td width="80" bgcolor="#FFFFFF"> <input name="txtName" type="text" id="txtName"></td>

        </tr>

        <tr>

            <th width="150">Description</th>

            <td bgcolor="#FFFFFF"> <textarea name="mtxDesc" cols="50" rows="4" id="mtxDesc"></textarea>

            </td>

        </tr>

        <tr>

            <th width="150">Image</th>

            <td bgcolor="#FFFFFF"> <input name="fleImage" type="file" class="box" id="fleImage"></td>

        </tr>

        <tr>

            <td width="150" bgcolor="#FFFFFF"> </td>

            <td bgcolor="#FFFFFF"> <input name="btnAdd" type="submit" id="btnAdd" value="Add Album">

                <input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.history.back();"></td>

        </tr>

    </table>

</form>

 

 

 

 

Link to comment
Share on other sites

>it only adds it to one table, which is tbl_album

 

You have only one INSERT query, which does an insert into `tbl_album`, and that's it.. that's all you're asking it to do, and that's all it does. You say you tried another separate INSERT query, but what was that query?

 

Please restate your question, as it's a bit confusing when compared to your code. Also, please post code between BBCode code tags.

 

[ code]your code goes here[ /code]
* remove spaces after the opening bracket of the code tags

 

Or, in the advanced editor, just press the # icon

 

PhREEEk

Link to comment
Share on other sites

Thanks.  I did notice that too so I tried to add another query, but it didn't seem to work.  That made sense to me too, but wonder what it is that I'm doing wrong.  So please, let me ask you this.  If I add the query like this, why won't it work???

 

	$query = "INSERT INTO tbl_album (al_name, al_description, al_image, al_date) 
		  VALUES ('$albumName', '$albumDesc', '$newName', NOW())";


$sql = "INSERT INTO tbl_image (im_album_id, im_title, im_description, im_image, im_thumbnail, im_date) 
VALUES ($albumId, '$imgTitle', '$imgDesc', '$image', '$thumbnail', NOW())";

    mysql_query($query) or die('Error, add album failed : ' . mysql_error());                    
[ /code]

Link to comment
Share on other sites

You've built 2 querys now, but you're still only sending one of them. Try the following code, and note that although you have proper single quotes around values, you should add back ticks ( ` ) to field names and tables names.

 

<?php

$sql = "INSERT INTO `tbl_album`
       (`al_name`, `al_description`, `al_image`, `al_date`) 
        VALUES
       ('$albumName', '$albumDesc', '$newName', NOW())
       ";

if ( !$result = mysql_query($sql) ) {
    die('Error, add album failed for table tbl_album: ' . mysql_error());
}

$sql = "INSERT INTO `tbl_image`
       (`im_album_id`, `im_title`, `im_description`, `im_image`, `im_thumbnail`, `im_date`) 
        VALUES
       ($albumId, '$imgTitle', '$imgDesc', '$image', '$thumbnail', NOW())
       ";

if ( !$result = mysql_query($sql) ) {
    die('Error, add album failed for table tbl_image: ' . mysql_error());
}
?>

 

PhREEEk

Link to comment
Share on other sites

if ( !$result = mysql_query($sql) ) { <<<<<<<< i dont think this line is correct!!!! i believe that will always return false

 

So why don't you try it before proclaiming that you believe it doesn't work?

 

Let me ask you this... is this valid?

 

<?php
if ( $result = mysql_query($sql) ) {
    // carry on wayward son;
} else {
    // generate an error;
}

 

Of course it is, as long as $result receives a resource and becomes 'live' with a value. If it does not, it is false and the branch is traversed.

 

So if we can test for true, we can certainly test for false with

<?php
if ( !$result = mysql_query($sql) ) {
    // generate an error;
} else {
    // carry on wayward son;
}

 

PhREEEk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.