Hello i am trying to put a value into a field on a table in my database. The value changes accordingly so the type of the column is ENUM ('P','A','T') but everything i do the value tha is been inserted is 'P'.

this is the html
<input type="hidden" id="type" name="type" value="A">

the javascript

      $("body").on('click','#updateButton',function()
            {
                var type=$("#type").val();
                 updateNewsFeed(uid, update,Z,groupID,token,pic,lat,lang,baseUrl,apiBaseUrl,type);
            }

        function updateNewsFeed(uid, update,uploads,group_id,token,pic,lat,lang,baseUrl,apiBaseUrl,type)
        {
            $("#newsFeed").fadeIn();
            var embed='';
            var encodedata=JSON.stringify({"uid": uid,"update": update,"group_id": group_id,"uploads": uploads,"token": token,"lat": lat,"lang": lang,"type": type});
            var url=apiBaseUrl+'api/updateNewsFeed'; 
        }

i dont thing its a php thing because the values that is always inserted is 'P'

What is your PHP INSERT query? Are you by any chance casting the value of type to an integer? Also, where is the javscript code that is actually sending the data to the server? The updateNewsFeed(...) doesn't seem to be doing anything with the encodedata and url variables.

Member Avatar for diafol

updateNewsFeed(uid, update,Z,groupID,token,pic,lat,lang,baseUrl,apiBaseUrl,type);

Where are these parameters defined? Are they global variables?

As mentioned the function doesn't seem to do anything:

var encodedata=JSON.stringify({"uid": uid,"update": update,"group_id": group_id,"uploads": uploads,"token": token,"lat": lat,"lang": lang,"type": type});
 var url=apiBaseUrl+'api/updateNewsFeed'; 

Is this suppose to be passed on somewhere?
You make no mention of PHP code - in order to send this to PHP, you'll need to use Ajax or send the data in a querystring using window.location.href - yuck.

In addition, you make reference to ENUM and options - we have no idea what you're doing. Please repost with all the relevant information required to give you an answer or suitable advice.

Ok i want my users to be able to upload audio, picture and i want the uploads to separared byt their type. P for picture, A as audio and so on.

This is the html for P

    <div id="preview" class="marginbottom10">
                </div>
                **<input type="hidden" id="type" value="P">**
                <div id="imageloadstatus" class="displaynone marginbottom10">
                <img src="<?php echo BASE_URL; ?>wall_icons/ajaxloader.gif" class="icon"> Uploading Picture please wait ....
                </div>
                <div id="imageloadbutton">
                <span id="addphoto">Add Photos:</span> <input type="file" name="photos[]" id="photoimg" multiple="true">
                </div>

This is the html for A

  <div id="previewaudio" class="marginbottom10">
            </div>
           **  <input type="hidden" id="type" name="type" value="A">**
            <div id="audioloadstatus" class="displaynone marginbottom10">
                <img src="<?php echo BASE_URL; ?>wall_icons/ajaxloader.gif" class="icon"> Uploading Audio please wait ....
            </div>
            <div id="audioloadbutton">
                <span id="addphoto">Add Audio File:</span> <input type="file" name="audioaud[]" id="audioaud" multiple="multiple">
            </div>

This is the php code

                    function updateNewsFeed()
                    {
                    $request = \Slim\Slim::getInstance()->request();
                    $data = json_decode($request->getBody());
                    $uid=$data->uid;
                    $update=$data->update;
                    $uploads=$data->uploads;
                    $group_id=$data->group_id;
                    $lat=$data->lat;
                    $lang=$data->lang;
                    $type=$data->type;
                        $s1="INSERT INTO `messages` (message, uid_fk, ip,created,uploads,lat,lang,type) VALUES (:feedUpdate, :uid, :ip,:time,:uploads,:lat,:lang,:type)";
                                    $stmt1 = $db->prepare($s1);
                                    $stmt1->bindParam("feedUpdate", $update, PDO::PARAM_STR);
                                    $stmt1->bindParam("uid", $uid, PDO::PARAM_INT);
                                    $stmt1->bindParam("ip", $ip);
                                    $stmt1->bindParam("time", $time);
                                    $stmt1->bindParam("uploads", $uploads, PDO::PARAM_STR);
                                    $stmt1->bindParam("lat", $lat, PDO::PARAM_STR);
                                    $stmt1->bindParam("lang", $lang, PDO::PARAM_STR);
                                    $stmt1->bindParam("type", $type, PDO::PARAM_STR);
                                    $stmt1->execute();
                                  }

This is the javascript

 $("body").on('click','#updateButton',function()
    {
        var update = $("#statusText").val();
        var lat=$("#latitude").val();
        var lang=$("#longitude").val();
        var type=$("#type").val();

         updateNewsFeed(uid, update,Z,groupID,token,pic,lat,lang,baseUrl,apiBaseUrl,type);
            $("#statusText").val('').focus().css("height", "20px");
            $("#latitude").val('');
            $("#longitude").val('');
            $("#type").val('');
            $("#geoContainer").slideUp();
    }
    function updateNewsFeed(uid, update,uploads,group_id,token,pic,lat,lang,baseUrl,apiBaseUrl,type)
{
    $("#newsFeed").fadeIn();
    var embed='';
    var encodedata=JSON.stringify({"uid": uid,"update": update,"group_id": group_id,"uploads": uploads,"token": token,"lat": lat,"lang": lang,"type": type});
    var url=apiBaseUrl+'api/updateNewsFeed'; 
    }

there is nothing wrong with the php code, i think its a javascript thing. It stores always 'P' as type, the rest its ok

Member Avatar for diafol

Just a question, why are you separating file upload? Can't you just have the one upload and let PHP decide on the media type?
You could do a quick file extension check, or a MIME (better) check and then apply the conditional logic prior to binding to prepared statement.

Simplicity of UX

Thats what i am trying to do using type. Can you elaborate? You mean the HTML?

Member Avatar for diafol

I mean just have the one upload form (HTML) - no need for hidden fields or any of that. Something like:

<input type="file" name="file" id="file" />

You can check the mine type like this:

$filename = $_FILES['file']['tmp_name'];
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filename);
finfo_close($finfo);

For this, ensure fileinfo is enabled in php.ini

If this returns "image/jpeg", "image/png", "image/gif" - these will be images (obviously).
If they return "audio/mpeg", "audio/x-wav", then it's an audio file...

See here: http://www.freeformatter.com/mime-types-list.html

There is no 100% foolproof method of determining a filetype, but extensions are easy to change. Changing the header etc, is more difficult.

Anyhow, once file uploaded and mime type extracted you can do a switch:

switch($mime)
{
    case "image/png":
    case "image/jpeg":
    case "image/gif":
        $type = 'P';
        break;
    case "audio/mpeg":
    case "audio/x-wav":
        $type = 'A';
        break;
}        

If you'r accepting text files too (is that what 'T' stands for??), then just add a test case to the control structure.

The reason I mention this is to make it easier for the user. One upload control (no need to select type) for ALL types of media / text files. The type determination takes place automatically behind the scenes :)

//EDIT - almost forgot, but you probably already know - change the form's enctype property to "multipart/form-data" if you're using a regular POST method form.

Member Avatar for diafol

Sorry, just thought I'd mention. Some filetypes can display different headers. E.g. mp3 files can have audio/mpeg, audio/mp3 or even, annoyingly, application/octet-stream (when I looked at downloaded mp3s and uploaded them in localhost Chrome).

The thing is that i want to pass a value that the system doesnt know which one is because when i am submitting all four values are submissible. Let me elaborate.

You have 4 different inputs
<div>
<input type="hidden" id="PTYPE">
</div>

<div>
<input type="hidden" id="ATYPE">
</div>
<div>
<input type="hidden" id="VTYPE">
</div>
<div>
<input type="hidden" id="TTYPE">
</div>

<input type="submit">

and i make an if statement to pass the value i want when the submit button is pressed

if($("#TTYPE"))
        {
            var type = "T";
        }
        else if($("#ATYPE"))
        {
            var type = "A";
        }
        else if($("#VTYPE"))
        {
            var type = "V";
        }
        else if($("#PTYPE"))
        {
            var type = "P";
        }

        alert(type);

and i keep getting the first var. T that is. I know what i do is wrong. Its like i am telling it to pass all four values and it stores the first one is getting. Its like i am asking for A.I. How may i distinquish the id and retund the one i want as var type?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.