Web Analytics Made Easy -
StatCounter need help saving saving uploaded image name in database - CodingForum

Announcement

Collapse
No announcement yet.

need help saving saving uploaded image name in database

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • need help saving saving uploaded image name in database

    hi below is my code that saves an image that a user uploads in a folder and this works just fine but the second half of the code is my problem: how do i make it so that besides saving the image in the specified folder, i save its name in my database so that it can be stored by what user uploads it?

    PHP Code:
    <?php require_once('Connections/SH.php'); ?>
    <?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;
    }
    }

    $colname_Recordset1 "-1";
    if (isset(
    $_SESSION['MM_Username'])) {
      
    $colname_Recordset1 $_SESSION['MM_Username'];
    }
    mysql_select_db($database_SH$SH);
    $query_Recordset1 sprintf("SELECT * FROM Pictures WHERE Username = %s"GetSQLValueString($colname_Recordset1"text"));
    $Recordset1 mysql_query($query_Recordset1$SH) or die(mysql_error());
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 mysql_num_rows($Recordset1);

    if (((
    $_FILES["file"]["type"] == "image/gif")
    || (
    $_FILES["file"]["type"] == "image/jpeg")
    || (
    $_FILES["file"]["type"] == "image/pjpeg"))
    && (
    $_FILES["file"]["size"] < 60000))
      {
      if (
    $_FILES["file"]["error"] > 0)
        {
        echo 
    "Return Code: " $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo 
    "Upload: " $_FILES["file"]["name"] . "<br />";
        echo 
    "Type: " $_FILES["file"]["type"] . "<br />";
        echo 
    "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo 
    "Temp file: " $_FILES["file"]["tmp_name"] . "<br />";

        if (
    file_exists("upload/" $_FILES["file"]["name"]))
          {
          echo 
    $_FILES["file"]["name"] . " already exists. ";
          }
        else
        
    $set true;
          {
          
    move_uploaded_file($_FILES["file"]["tmp_name"],
          
    "upload/" $_FILES["file"]["name"]);
          echo 
    "Stored in: " "upload/" $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo 
    "Invalid file";
      }

    mysql_free_result($Recordset1);
    ?>
    <?php
     
    if $set true;
    $con mysql_select_db($database_SH$SH);

    $sql "INSERT INTO Pictures (Username, Pic2)
          VALUES (
    $row_Recordset1['Username'], $_FILES["file"]["name"])";

    if (!
    mysql_query($sql,$con))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "1 record added";
    else
     echo 
    "Error";
    ?>

  • #2
    Move $set = true; inside your else start bracket.

    Also, change if $set = true; to if ($set) {. This will check to see if $set is true or not. If so, it'll enter into the below code. You'll also need to add a closing bracket after your last statement.

    And last, you have echo "1 record added"; after the last bracket of the if statement and before the else. You should remove that last error echo and instead add the "1 record added" echo to after the else.

    -Shane

    Comment

    Working...
    X