Problem uploading / renaming images to server and mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nwclark
    New Member
    • May 2009
    • 9

    Problem uploading / renaming images to server and mysql

    Ok... I have a noobie issue.

    I am trying to upload a photo to specific directory on my server. (this is working correctly with the code below). However, I am also trying to rename the file and upload that filename to a mysql database (users.photo).

    This is my code.. it shows no errors but it does not add it to my server.

    HELP WOULD BE SO APPRECIATED!!\

    Code:
    <?php require_once('../Connections/JMI10.php'); 
    
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    ?>
    <?php
    $colname_Recordset1 = "-1";
    if (isset($_SESSION['MM_Username'])) {
      $colname_Recordset1 = $_SESSION['MM_Username'];
    }
    mysql_select_db($database_JMI10, $JMI10);
    $query_Recordset1 = sprintf("SELECT * FROM `user` WHERE username = %s", GetSQLValueString($colname_Recordset1, "text"));
    $Recordset1 = mysql_query($query_Recordset1, $JMI10) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 = mysql_num_rows($Recordset1);
    ?>
    <?
    //This is the directory where images will be saved 
    $target = "/home/content/n/w/c/nwclark/html/jmi2/secure/idphotos/"; 
    $target = $target . basename( $_FILES['photo']['name']); 
    
    //This gets all the other information from the form 
    $name=$_POST['username']; 
    $pic=($_FILES['photo']); 
    $width = 640; 
    $height = 480; 
    //Writes the information to the database 
    mysql_query("INSERT INTO `user` VALUES ('$name', '$pic')") ; 
    
    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
    { 
    
    //Tells you if its all ok 
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
    } 
    else { 
    
    //Gives and error if its not 
    echo "Sorry, there was a problem uploading your file."; 
    } 
    
    mysql_free_result($Recordset1);
    ?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You can use mysql_error() to retrieve the latest error, otherwise, mysql will not tell you any errors. Add or die(mysql_error ()) to your mysql function calls.

    Code:
    mysql_query("INSERT INTO `user` VALUES ('$name', '$pic')") or die("Error: " . mysql_error());

    Comment

    • nwclark
      New Member
      • May 2009
      • 9

      #3
      Oh.. ok.. cool .. i should've known that.

      i get this error..

      Error: Column count doesn't match value count at row 1

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        If you omit the column names from the INSERT statement, MySQL assumes you're going to supply data for every column. After all, how would it know which columns to put data in?

        You can either add the column names to your query, or provide values for every column in the VALUES section.

        Comment

        • nwclark
          New Member
          • May 2009
          • 9

          #5
          ok.. sorry to sound so ignorant.. (which i am) .. how would you do that?

          BTW: I owe you big time.. working hard to learn PHP/MYSQL

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Not being sure how your table is set up, I'll use an example.

            Table set up:

            Code:
            Database: User
            Table: Photos
            Columsn: id | photo_name | photo_upload_time | photo_tags
            Inserting only 2 fields (no fields defined in Insert)
            Code:
            INSERT INTO
                `User.Photos`
                /** No fields defined here **/
                VALUES
                (
                    'photo.jpg',
                    '00:00:00'
                );
            That would cause the same error you received, for the previously mentioned reason.

            The code revised:

            Code:
            INSERT INTO
                `User.Photos`
                (
                    'photo_name',
                    'photo_upload_time'
                )
                VALUES
                (
                    'photo.jpg',
                    '00:00:00'
                );
            See that we have now specified which fields should have the data inserted?

            If you do want to omit giving the fields, you will have to provide all fields in the VALUES.

            Code:
            INSERT INTO
                `User.Photos`
                /** No fields defined here **/
                VALUES
                (
                    'id',
                    'photo.jpg',
                    'my photo'
                    'a, cool, photo'
                );
            Understand?

            Mark.

            Comment

            • nwclark
              New Member
              • May 2009
              • 9

              #7
              Starting to i think .. but none of these option work.. I guess the issue is that i am trying to update a single column on the user table. I did that rather than creating a whole new table for the photo name.

              so the basic db design is:

              table=user
              column=photo

              I want to place the photo name there and nothing else.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Hi.

                Line #53 of your code:
                [code=php]$pic=($_FILES['photo']);[/code]
                And then you go on to use $pic as a string in your INSERT query on line #57.
                This would put "Array" into the query, which I assume you don't want.

                You probably mean to do this:
                [code=php]$pic = $_FILES['photo']['name'];[/code]

                As to the other thing...

                Like Markus says, your INSERT statements needs to provide values for EVERY column in the table that needs one. But you can leave out columns that have default values.

                You just need to specify which columns you want to provide values for.
                (Not specifying the columns, like you do in your INSERT query, is never good. You should always specify the columns you plan to use, even if you are going to use all of them. Allows you to add to the table later on without messing up your old queries.)

                For example:
                [code=mysql]
                INSERT INTO `user`(`name`, `pic`)
                VALUES ('$name', '$pic')
                [/code]
                Assuming the names of the columns are `name` and `pic`, and that there are no other columns in the table that do not have a default value.

                Comment

                Working...