Jump to content

Recommended Posts

Hi, this is what I want to get working: each time I add an entry, it is displayed in a nice html table, and each entry listed in this html table has an UPDATE link that allows the user to update the Price and Description field of the corresponding table in my database. I have also included an upload element which whenever a user chooses an image, it will replace the default fileUploadIcon.jpg with this. Please see the index.php and bellUpdateForm.php below respectively

 

//index.php 
<table border="border">
<?php
    require 'bellConnect.inc.php';

    $curImage;
    $imgIndx=-1;//iniitally
    $result=mysql_query("SELECT ID, Name, Manufacturer, Price, Description, SimSupport FROM bellProducts");

    $indx=0;//***HOW DO I MAKE THIS SHARED SO THAT EACH ENTRY ADDED HAS AN INCREMENTED index appended to end of its name attribute 

    //phpinfo();

   //print "Val of \$counter so far: ".$counter;
    //NB: print table headings
    if(mysql_num_rows($result))//if there is at least one entry in bellProducts, make a table
    {
        //$counter+=1;
        print "<table border='border'>";
        print "<tr>
        <th>ID</th>
        <th>Image</th>
        <th>Name</th>
        <th>Manufacturer</th>
        <th>Price</th>
        <th>Description</th>
        <th>SimSupport</th>
        <th colspan=2 align='center'>Modify</th>
           </tr>";

        //NB: now output each row of records
        while($row=mysql_fetch_assoc($result))
        {
            //extract($row);
      
            //if($indx<sizeof($bellProductsArray))
            //{
                print
                "<tr align='center'>
                    <td>$row[iD]</td>
                    <td>";
                    if($_POST['upload'])
                       print "<img src=".$_FILES['image']['name'].">";
                    else
                        print "<img src='fileUploadIcon.jpg' name='image' />
                    </td>
                    <td> $row[Name] </td>
                    <td> $row[Manufacturer] </td>
                    <td> $$row[Price]</td>
                    <td align='left'>$row[Description]</td>
                    <td>$row[simSupport]</td>
                    <td><a href='bellUpdateForm.php?ID=$row[iD]'>UPDATE</a></td>
                    <td><a href='bellDeleteForm.php?ID=$row[iD]' name='delete'>DELETE</a></td>
                  </tr>";
            //}//END INNER IF
            $indx++;
        }//END WHILE
             
  }//END BIG IF

?>

</table>
    
</body>

</html>

 

//bellUpdateForm.php
<?php 

require 'bellConnect.inc.php';

if($_GET && !$_POST)//this is to make sure that the current entry is selected BUT the updated info NOT submitted yet
{
    if(isset($_GET['ID']))
    {
        $ID=$_GET['ID'];
        $result=mysql_query("SELECT * FROM bellProducts WHERE ID=$ID");
        $row=mysql_fetch_assoc($result); //store the one entry that will be updated
        print "You are currently updating item: <strong>$row[Manufacturer] $row[Name] </strong> <br /><br />";
        print
            "<form action='bellUpdate.php' method='post'>
                <label>
                    Price:<input type='text' value='$row[Price]' name='price' id='price'></input>
                </label>

                <label>
                    Description: <textarea cols='60' rows='3' name='description' id='description'>
                        $row[Description]
                                 </textarea>
                </label>

               <div>
                <input type='submit' value='Update entry' name='update' id='update' />
                <input type='hidden' value=$row[iD] name='id' />
               </div>
                    
                <input type='reset' value='Reset entry' />";
                      
            print "</form>";

    }//END INNER IF   
}//END BIG IF

?>

<form method='post' action='index.php' enctype='multipart/form-data' name='uploadImage' id='uploadImage'>
    <p>
        <label>Upload image</label>
            <input type='file' name='image' id='image' />
    </p>

    <p>
        <input type='submit' value='Upload this' name='upload' id='upload' />
    </p>
</form>


<?php if(array_key_exists('upload', $_POST))
{
    // define constant for upload folder
    define('UPLOAD_DIR', 'C:\wamp\www');
    // move the file to the upload folder and rename it
    move_uploaded_file($_FILES['image']['tmp_name'], !
    UPLOAD_DIR.$_FILES['image']['name']);
}

?>

</body>

</html>

We need a little more information in order to help you. Its difficult for us to just look through your code trying to find a problem. You need to describe what is happening what happens when you run that script, what you expect to happen, and the difference between the two.

 

A little advice. for the best results while debugging, make sure you have error reporting on. This can be achieved by adding the following to the top of your php pages

 

ini_set('display_errors',1);
error_reporting(E_ALL);

Ok, here's the steps leading to my errors:

1) I have a blank index.php (no html table b/c no entries in my database table yet), so I click the Insert new entry

2) I enter all the fields and Add Entry and it redirects to index.php with an html table with the default fileUploadicon.jpg in the Image <tr>, which is good

since I can only upload an image once I have an entry

3) Now I clicke the UPLOAD IMAGE link for that entry, and choose a jpg image and it takes me back to index.php, BUT now it shows nothing in the Image <tr> of the <th> for Image

4)When I click DELETE or UPDATE, it redirects me to index.php BUT now with the default fileUploadIcon.jpg again, why??

 

Please see my code below:

index.php

<table border="border">
<?php
    require 'bellConnect.inc.php';

    $curImage;
    $imgIndx=-1;//iniitally
    $result=mysql_query("SELECT ID, Name, Manufacturer, Price, Description, SimSupport FROM bellProducts");

    $indx=0;//***HOW DO I MAKE THIS SHARED SO THAT EACH ENTRY ADDED HAS AN INCREMENTED index appended to end of its name attribute 

    //phpinfo();

   //print "Val of \$counter so far: ".$counter;
    //NB: print table headings
    if(mysql_num_rows($result))//if there is at least one entry in bellProducts, make a table
    {
        //$counter+=1;
        print "<table border='border'>";
        print "<tr>
        <th>ID</th>
        <th>Image</th>
        <th>Name</th>
        <th>Manufacturer</th>
        <th>Price</th>
        <th>Description</th>
        <th>SimSupport</th>
        <th colspan=3 align='center'>Modify</th>
           </tr>";

        //NB: now output each row of records
        while($row=mysql_fetch_assoc($result))
        {
            //extract($row);
      
            //if($indx<sizeof($bellProductsArray))
            //{
                print
                "<tr align='center'>
                    <td>$row[iD]</td>
                    <td>";
                if($_POST)
                {
                    if($_POST['upload'])
                       print "<img src=".$_FILES['imageFileType']['name']." width='100px' height='80px'>";
                }
                else
                {
                    print "<img src='fileUploadIcon.jpg' name='defaultImage' />";
                }
                    print "</td>
                    <td> $row[Name] </td>
                    <td> $row[Manufacturer] </td>
                    <td> $$row[Price]</td>
                    <td align='left'>$row[Description]</td>
                    <td>$row[simSupport]</td>
                    <td><a href='bellUploadForm.php'>UPLOAD IMAGE</a></td>
                    <td><a href='bellUpdateForm.php?ID=$row[iD]'>UPDATE</a></td>
                    <td><a href='bellDeleteForm.php?ID=$row[iD]' name='delete'>DELETE</a></td>
                  </tr>";
            //}//END INNER IF
            $indx++;
        }//END WHILE
             
  }//END BIG IF

?>

</table>
    
</body>

</html>

 

bellUploadForm.php

<form action="index.php" method="post" enctype="multipart/form-data">
            <p>
                <label>
                    Upload image<input type='file' name='imageFileType' id='imageFileType' />
                </label>
            </p>

            <p>
                <label>
                    <input type='submit' value='Upload this image' name='upload' id='upload' />
                </label>
            </p>
        </form>

    <?php
        if(array_key_exists('upload', $_POST))
        {
            print "Hello, world again!";
            // define constant for upload folder
            define('UPLOAD_DIR', 'C:\wamp\www');
            // move the file to the upload folder and rename it
            move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$_FILES['image']['name']);

//            header("Location: http://localhost/index.php");
//            exit;
        }

    ?>



    </body>
</html>

 

**NB: my SQL delete, alter (update), insert commands work fine, it's just this Image and upload stuff that's frustrating me, please help!! thanks

are you sure the image is being moved correctly. Try moving the move_uploaded_file() function into an if statement to see that it is indeed being uploaded correctly. like so:

 

if (move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$_FILES['image']['name'])){
echo "File uploaded correctly";
else {
echo "file upload failed";
}

You're right, it is NOT uploading at all, I just checked the directory, here are the 3 error messages I'm getting in my bellUploadForm.php:

 

Undefined index: image  (in bellUploadForm.php )

Use of undefined constant UPLOAD_DIR, assumed 'UPLOAD_DIR' in C:\wamp\www\bellUploadForm.php

Undefined index: image in C:\wamp\www\bellUploadForm.php

 

I'm stuck, plz help! Thx

those are notices not errors. However, they indicate a lack of knowledge on certain concepts.  "Undefined Index" notices, For array keys, you should always surround them with single quotes. for example

//instead of 
$array[key];
//it should be
$array['key'];

 

undefined key errors basically mean the array is not defined on that key. I'm not sure why you are using those keys, or what you expect them to be, so im not sure how to help you there

 

your problem is probably with the second notice. You are using  (or trying to) a constant named UPLOAD_DIR. this Constant doesn't exist. You are going to have to define it, or include a script to define it. Otherwise, you are trying to move the image into the directory named "UPLOAD_DIR" which probably doesn't exist

ahh I see. Yeah i don't know how I missed that. You have error reporting turned on correct? Perhaps there is some error not being displayed that is stopping that constant from being defined.

 

you could also used the defined() function that check if it is indeed defined.

 

However, why don't you try putting the string directly into the argument to the move_uploaded_file() function instead of using the constant, and see if that works

Ok, so I figured out why it wasn't going into the directory I want, I forgot to put the the trailing backslash but now my new problem is that for the form action attribute in my bellUploadForm.php, if I leave it empty, it adds the jpg file to my specified directory BUT once I assign the form tag's action attribute the value of index.php, it doesn't add the jpg file. Plz help, thx.

hmm, that doesn't make much sense to me. When you leave the action attribute out, it, by default, submits to itself. By supplying the action (and setting it to itself anyways) this same behavior should occur. Are you positive that you are supplying the action correctly. When you say "its doesn't add the jpg file", what exactly happens? is there an error? blank page?

 

However, setting the action to index.php and leaving out the action attribute all together is pretty much the same thing since index.php is the page where your form is, and the processing for your form is done

thanks for replying again, ok, so when I put in the action="index.php" in my form tag, then after I upload an image and click the Upload this image button, it redirects to index.php and in the table rows for the table heading Image, it is just blank, but the remaining fields are filled in properly (html table). Is it possible you can use the code index.php and bellUpload.php and build a simple webpage, you don't even need the mySQL delete, insert etc stuff, just an html table with a <tr> to hold an img src. I'm trying to get this to work b/c I'm trying to add this to my portfolio of projects b/c I'm trying to apply for a practicum so i'm trying to learn a little more php. (I'm trying to build a simple Bell Mobility online shopping website as a project, sorry for details)

Hi, ppl, I have a related problem. I want to be able to store each upload image into an array and at the same time and so after each upload is done, it will redirect to the index page which shows an html table of the uploaded Image along with each record's other field data. I also made another array to store each ID value so that when a user decides to upload a new image for an entry with an uploaded image already, it will compare the current ID using $_GET with the ID in my $idArray (traverse this array until a matching ID found), so that it will overwrite the index of my images array (stores all the uploaded image), but my problem is when I use a var such as: static $indx=0, I want the value to not get reset to 0 each time, but it seems to be the case. So I have basically an index.php and when user clicks the insert entry, each entry displayed in an html table in index.php will have an upload image link, and I want to store the ID using $GET in an idArray and store the uploaded image in an imageArray, again I tried making idArray static, and image Array static, but I don't think it works. Does anyone know what I should do?

I was thinking of using $_SESSION, but the only problem I think would be that once the session is done and I start a new session (when I'm done all my editing and inserting of entries for that day or whatever), my static $bellProductsArray will be reinitialized to empty again. Does anyone know how I can make sure that each time I add the user's uploaded images to my $bellProductsArray (via of $bellProductsArray[$indx]=$_FILES['imageFileType']['name'] where variable $indx is initialized as static $indx=0) that it will remember its current value of $indx, b/c it keeps resetting each time I upload a new image and then I get redirected to my index.php (with my html table of database records), so after the redirect, I insert a new entry (Insert New Entry button in my index.php script) and when I upload, the value of $indx is 0 again, I thought static meant to preserve the current state? I'm lost, please see the code below, thanks.

 

Here is the bellConnect.inc.php script

<?php
mysql_connect("localhost", "root");
mysql_select_db("bell");
static $bellProductsArray=array();
static $bellIDArray=array();
?>

 

Here is the index.php

<table border="border">
<?php
    require 'bellConnect.inc.php';

    $result=mysql_query("SELECT ID, Name, Manufacturer, Price, Description, SimSupport FROM bellProducts");

    //phpinfo();

    //NB: print table headings
    if(mysql_num_rows($result))//if there is at least one entry in bellProducts, make a table
    {
        print "<table border='border'>";
        print "<tr>
        <th>ID</th>
        <th>Image</th>
        <th>Name</th>
        <th>Manufacturer</th>
        <th>Price</th>
        <th>Description</th>
        <th>SimSupport</th>
        <th colspan=3 align='center'>Modify</th>
           </tr>";

        //NB: now output each row of records
        while($row=mysql_fetch_assoc($result))
        {
            //extract($row);
      
            //if($indx<sizeof($bellProductsArray))
            //{
                print
                "<tr align='center'>
                    <td>$row[iD]</td>
                    <td>";
                if($_POST)
                {
                    if($_POST['upload'])
                       print "<img src=".$_FILES['imageFileType']['name']." width='100px' height='80px'>";
                }
                else
                {
                    print "<img src='fileUploadIcon.jpg' name='defaultImage' />";
                }
                    print "</td>
                    <td> $row[Name] </td>
                    <td> $row[Manufacturer] </td>
                    <td> $$row[Price]</td>
                    <td align='left'>$row[Description]</td>
                    <td>$row[simSupport]</td>
                    <td><a href='bellUploadForm.php?ID=$row[iD]'>UPLOAD IMAGE</a></td>
                    <td><a href='bellUpdateForm.php?ID=$row[iD]'>UPDATE</a></td>
                    <td><a href='bellDeleteForm.php?ID=$row[iD]' name='delete'>DELETE</a></td>
                  </tr>";
            //}//END INNER IF
        }//END WHILE         
  }//END BIG IF

?>

</table>
    
</body>

</html>

 

 

Here is the bellUploadForm.php

<html>
<body>

<form action="" method="post" enctype="multipart/form-data">
            <p>
                <label>
                    Upload image<input type='file' name='imageFileType' id='imageFileType' />
                </label>
            </p>

            <p>
                <input type='submit' value='Upload this image' name='upload' id='upload' />
            </p>
        </form>

    <?php
        require "bellConnect.inc.php";
        static $indx=0;
        if(array_key_exists('upload', $_POST))
        {
            // define constant for upload folder
            define('UPLOAD_DIR', 'C:\wamp\www\\');
            // move the file to the upload folder and rename it
            move_uploaded_file($_FILES['imageFileType']['tmp_name'], UPLOAD_DIR.$_FILES['imageFileType']['name']);//params source, destination
            $bellProductsArray[$indx]=$_FILES['imageFileType']['name'];
            $bellIDArray[$indx]=$_GET['ID'];
            $indx+=1;  
        }

        print "\$bellIDArray ID currently: ".$_GET['ID'];
        
        print_r("current itm in \$bellProductsArray: ".$_FILES["imageFileType"]["name"]);
        print "<br />";

        //NB: output all the current img in $bellProductsArray so far
        for($i=0;$i<sizeof($bellProductsArray);$i++)
        {
            print "cur \$bellProductsArray img: ".$bellProductsArray[$i]."<br />";
        }

        for($j=0;$j<sizeof($bellIDArray);$j++)
        {
            print "cur \$bellIDArray ID: ".$bellIDArray[$j]."<br />";
        }

        print "Size of \$bellProductsArray:".sizeof($bellProductsArray);

        print "<pre>";
        if(array_key_exists('upload', $_POST))
        {
            print_r($_FILES);
        }
        
        print "</pre>";
       
    ?>

    </body>
</html>

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.