When model details get updated the image name in DB get dropped can some one tell me why?

I’ve written this below scripts one is the form for updating the model details and the other is the script that does the action.

The problem I’m having is when I update the details my image names get dropped I do have a hidden filed to get the old image name if the new image name doesn’t get set. If I upload a new image it works but if edit the details and don’t upload a new image the old image name get’s dropped.

So can some one please look at it and help me to find the issue.

form.php

<?php
/**
 * Created by PhpStorm.
 * User: SiNUX
 * Date: 9/24/2014
 * Time: 12:17 AM
 */
session_start();

$iD = $_GET["id"];
include_once("hostcon.php");
include_once("sessiocheck.php");

$selData = "SELECT * FROM modeldetails WHERE mId =".$iD;
$fireQuery = mysql_query($selData);
$row = mysql_fetch_array($fireQuery);
?>
<html>
<head>
    <title>Create a new model profile</title>
    <link rel="stylesheet" type="text/css" href="Scirpts/adminindex.css" />
</head>
<body style="background-image: none;">
<div id="index" style="margin:0 auto 0 auto; width:220px">
    <form enctype="multipart/form-data" action="editmodel_action.php" method="post" id="createuser">
        <fieldset>
            <legend>New Model Profile</legend>
            <?php if(isset($_GET["error"])) { ?>
                <div style="color: #ff0000; font-weight: bold;">
                    <?php echo $_GET["error"]; ?>
                </div>
            <?php } ?>
            <div style="background-color: chartreuse; color: darkcyan; font-weight: bold;" align="center">
                <?php if (isset($_REQUEST["msg"])){ echo $_REQUEST["msg"]; }?>
            </div>
            <div style="background-color: chartreuse; color: #990000; font-weight: bold;" align="center">
                <?php if(isset($_REQUEST["not"])) { echo $_REQUEST["msg"]; }?>
            </div>
            <div class="margin">
                <label>Name</label><br />
                <input type="text" name="name" id="name" maxlength="60" value="<?php echo $row["name"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Nick Name</label><br />
                <input type="text" name="nicname" id="nicname" maxlength="60" value="<?php echo $row["nicName"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Age</label><br />
                <input type="text" name="age" id="age" maxlength="60" value="<?php echo $row["age"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Bust</label><br />
                <input type="text" name="bust" id="bust" maxlength="60" value="<?php echo $row["bust"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Size</label><br />
                <input type="text" name="size" id="size" maxlength="60" value="<?php echo $row["size"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Height</label><br />
                <input type="text" name="height" id="height" maxlength="60" value="<?php echo $row["height"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Hair</label><br />
                <input type="text" name="hair" id="hair" maxlength="60" value="<?php echo $row["hair"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Eyes</label><br />
                <input type="text" name="eyes" id="eyes" maxlength="60" value="<?php echo $row["eyes"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Nationality</label><br />
                <input type="text" name="natio" id="natio" maxlength="60" value="<?php echo $row["natio"]; ?>" /><br />
            </div>
            <div class="margin">
                <label>Description</label><br />
                <textarea name="desc" id="desc" rows="8" cols="50"><?php echo $row["descrip"]; ?></textarea> <br />
            </div>
            <div class="margin">                
                    <legend style="font-weight: normal; font-size: medium; text-align: left">Standard Services</legend>
                    <textarea name="stdService" id="stdService" rows="8" cols="50"><?php echo $row["stdService"] ?></textarea>                
            </div>
            <div class="margin">                
                    <legend style="font-weight: normal; font-size: medium; text-align: left">Other Services</legend>
                    <textarea name="othService" id="othService" rows="8" cols="50"><?php echo $row["otService"] ?></textarea>
            </div>
            <div class="margin">
                <fieldset>
                    <legend style="font-weight: normal; font-size: medium; text-align: left">Upload a New Image</legend>
                    <input type="file" name="upload[]" />
                    <input type="hidden" name="himage[]" value="<?php echo $row["img1"]; ?>" /><?php echo $row["img1"]; ?>
                    <input type="file" name="upload[]" />
                    <input type="hidden" name="himage[]" value="<?php echo $row["Img2"]; ?>" /><?php echo $row["Img2"]; ?>
                    <input type="file" name="upload[]" />
                    <input type="hidden" name="himage[]" value="<?php echo $row["Img3"]; ?>" /><?php echo $row["Img3"]; ?>
                    <input type="file" name="upload[]" />
                    <input type="hidden" name="himage[]" value="<?php echo $row["Img4"]; ?>" /><br /><br />

                    <?php if(!empty($row["img1"])) {?><b>Current Images</b><br /><br />1.&nbsp;
                    <?php echo "<img src= 'uploads/".$row["img1"]."' width='70' height='100' />" ?>&nbsp;&nbsp;
                    <?php } ?>
                    <?php if(!empty($row["Img2"])) {?>2.&nbsp;
                     <?php echo "<img src= 'uploads/".$row["Img2"]."' width='70' height='100' />" ?><br />
                     <?php } ?>
                     <?php if(!empty($row["Img3"])) {?>3.&nbsp;
                      <?php echo "<img src= 'uploads/".$row["Img3"]."' width='70' height='100' />" ?>&nbsp;&nbsp;
                      <?php } ?>
                     <?php if(!empty($row["Img4"])) {?>4.&nbsp;
                   <?php echo "<img src= 'uploads/".$row["Img4"]."' width='70' height='100' />" ?><br />
                  <?php } ?>
                </fieldset>
                <input type="text" name="moId" id="moId" value="<?php echo $row["mId"]; ?>" style="visibility: hidden;" />
            </div>
            <div class="margin" style="align-content: center;" align="center">
                <input type="submit" name="submit" id="submit" value="Submit" />
                <input type="reset" name="reset" id="reset" value="Reset" />
            </div>
        </fieldset>
    </form>
</div>
</body>
</html>

action.php

<?php
/**
 * Created by PhpStorm.
 * User: SiNUX
 * Date: 9/24/2014
 * Time: 12:17 AM
 */
include_once("hostcon.php");
include_once("Scirpts/multiupload.php");

// All the variables.
$modId = $_POST["moId"];
$rName = validateInput(mysql_real_escape_string($_POST["name"]));
$nicName = validateInput(mysql_real_escape_string($_POST["nicname"]));
$age = validateInput(mysql_real_escape_string($_POST["age"]));
$bust = validateInput(mysql_real_escape_string($_POST["bust"]));
$size = validateInput(mysql_real_escape_string($_POST["size"]));
$height = mysql_real_escape_string($_POST["height"]);
$hair = validateInput(mysql_real_escape_string($_POST["hair"]));
$eyes = mysql_real_escape_string($_POST["eyes"]);
$nAtio = validateInput(mysql_real_escape_string($_POST["natio"]));
$dEscrip = validateInput(mysql_real_escape_string($_POST["desc"]));
$stdService = $_POST["stdService"];
$othService = $_POST["othService"];

//Image File Name
$img1 = $_FILES['upload']['name'][0];
$img2 = $_FILES['upload']['name'][1];
$img3 = $_FILES['upload']['name'][2];
$img4 = $_FILES['upload']['name'][3];


//Hidden image names
$hImg1 = $_FILES['himage']['name'][0];
$hImg2 = $_FILES['himage']['name'][1];
$hImg3 = $_FILES['himage']['name'][2];
$hImg4 = $_FILES['himage']['name'][3];


function validateInput($frmData){
    $frmData = trim($frmData);
    $frmData = stripslashes($frmData);
    $frmData = htmlspecialchars($frmData);

    return $frmData;

}

//----------------------------------------- Start of data verification ------------------------------------------------
//Initiate error variable as empty
$errorCount = "";

//Start comparing and binding of the error variable with the message
//If error passed on with an error message it will stop the adding of the data to table and then send a message
//To be displayed to why the data was not entered.
if (empty($rName)) {
    $errorCount = $errorCount. "Name cannot be empty";
} else {
    if (!preg_match("/^[a-zA-Z]+$/", $rName)) {
        $errorCount = $errorCount . "Only Letters and White Space Allowed";
    }
}

if(empty($nicName)){
    $errorCount = $errorCount . ", Nick name cannot be empty";
}else{
    if(!preg_match("/^[a-zA-Z0-9]+$/",$nicName)){
        $errorCount = $errorCount .", Only Letters, White Space and numbers Allowed";
    }
}

if(empty($age)){
    $errorCount = ", Age cannot be empty";
}else{
    if(!preg_match("/^[0-9]+$/", $age)){
        $errorCount = $errorCount .", age Only numbers are allowed";
    }
}

if(empty($bust)){
    $errorCount = $errorCount.", Bust cannot be empty";
}else{
    if(!preg_match("/^[0-9]+$/", $bust)){
        $errorCount = $errorCount .", Only numbers are allowed";
    }
}

if(empty($size)){
    $errorCount = $errorCount.", Size cannot be empty";
}else{
    if(!preg_match("/^[0-9]+$/", $size)){
        $errorCount = $errorCount .", Size Only numbers are allowed";
    }
}

if(empty($height)){
    $errorCount = $errorCount.", Height cannot be empty";
}

if(empty($hair)){
    $errorCount = $errorCount.", Hair cannot be empty";
}else{
    if(!preg_match("/^[a-zA-Z]+$/", $hair)){
        $errorCount = $errorCount .", Only Letters and White Space Allowed";
    }
}

if(empty($nAtio)){
    $errorCount = $errorCount.", Nationality cannot be empty";
}else{
    if(!preg_match("/^[a-zA-Z]+$/", $nAtio)){
        $errorCount = $errorCount .", Only Letters and White Space Allowed";
    }
}

if(empty($dEscrip)){
    $errorCount = $errorCount.", Description cannot be empty";
}

if(empty($stdService)){
    $errorCount = $errorCount.", Please select a standard service";
}

if(empty($othService)){
    $errorCount = $errorCount.", Please select a other service";
}
//------------------------------------------- end of data verifying ----------------------------------------------------


//------------------------------------------- File upload Area Start ---------------------------------------------------
if($_FILES['upload']['name'] != ""){

    $max_size = 1000*1000; // the max. size for uploading
    $multi_upload = new muli_files;
    $multi_upload->upload_dir = "uploads/"; // "files" is the folder for the uploaded files (you have to create this folder)
    $multi_upload->extensions = array(".png",".jpeg",".jpg"); // specify the allowed extensions here
    // $multi_upload->extensions = "de"; // use this to switch the messages into an other language (translate first!!!)
    $multi_upload->max_length_filename = 1000; // change this value to fit your field length in your database (standard 100)
    //$multi_upload->rename_file = false;
    $multi_upload->tmp_names_array = $_FILES['upload']['tmp_name'];
    $multi_upload->names_array = $_FILES['upload']['name'];
    $multi_upload->error_array = $_FILES['upload']['error'];
    //$multi_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n"; // because only a checked checkboxes is true
    //$multi_upload->do_filename_check = (isset($_POST['check'])) ? $_POST['check'] : "n"; // use this boolean to check for a valid filename
    //$new_name = (isset($_POST['name'])) ? $_POST['name'] : "";
    $multi_upload->upload_multi_files();// new name is an additional filename information, use this to rename the uploaded file


}
//---------------------------------------------- File upload Area End --------------------------------------------------

if($errorCount == "") {

//inserting data in to the data base
//$updModel = "UPDATE modeldetails SET name='".$rName."', nicName='".$nicName."', age='".$age."', bust='".$bust."', size='".$size."', height='".$height."', hair='".$hair."', eyes ='".$eyes."', natio='".$nAtio."', descrip ='".$dEscrip."', stdService ='".$stdService."', otService ='".$othService."', img1 ='".$img1."', Img2 ='".$img2."', Img3 ='".$img3."', Img4 ='".$img4."' WHERE mId=".$modId;

    $updModel = <<<modelQuery
    UPDATE modeldetails
SET name = '{$rName}'
  , nicName = '{$nicName}'
  , age = '{$age}'
  , bust = '{$bust}'
  , size = '{$size}'
  , height = '{$height}'
  , hair = '{$hair}'
  , eyes = '{$eyes}'
  , natio = '{$nAtio}'
  , descrip = '{$dEscrip}'
  , stdService = '{$stdService}'
  , otService = '{$othService}'
  , img1 = '{$img1}'
  , Img2 = '{$img2}'
  , Img3 = '{$img3}'
  , Img4 = '{$img4}'
WHERE mId = {$modId}
modelQuery;

    $fireQuery = mysql_query($updModel);
echo $hImg1;
    echo $hImg2;
    echo $hImg3;
    echo $hImg4;

    echo $img1;
    echo $img2;
   /* // If the query is successful this will render the massage
    if ($fireQuery) {
        header("location:editmodel.php?msg=added && id=".$modId);
        exit();
    } else {
        header("location:editmodel.php?msg=notadded && id=".$modId);
        exit();
    }*/
}else{
    header("location:editmodel.php?error=".$errorCount." && id=".$modId);
}

It’s not the prettiest programing but please help me to find the error.

I see that you retrieve the hidden variables into $hImg1, $hImg2 and so on, but other than echoing them after the query has been run you don’t seem to do anything with those variables. One way might be to define a new set of image names based on whether you have new ones or not:

$uImg1 = ($img1 == "" ? $hImg1 : $img1);
$uImg2 = ($img2 == "" ? $hImg2 : $img2);

then in your query, use $uImg1, $uImg2 and so on to update your row instead of $img1, $img2. Or you could probably just use the same varnames and just overwrite the values if $imgx arrives empty.

Or you could edit the query so that if you don’t have any new images to store, you don’t update those fields. That’s probably more efficient, slightly.

$query = "update modeldetails set name = whatever, otService = whatever";
if ($img1)<>"" $query .= ", img1 = $img1";
if ($img2)<>"" $query .= ", img2 = $img2";
... and so on...
$query .= "where mId = $modId";
1 Like

Thank you very much for enlightening me bro will look in to this again. I was really stuck it’s a bit hard when I’m the only one doing every thing.

Your form.php script is wide open to SQL Injection attack, as your letting user-submitted data near the database without any sort of escaping or sanitizing.

Also the old mysql_* extension is deprecated and will very likely be removed from the next version of php

Yeah I know that it’s too late for me to go back I will convert this back to PDO I’m learning it but I have struck a brick wall which actually making me angry I’m trying to create sessions but I can’t it’s not happening using PDO.