How can I upload an image to a SQL database?
Current mysql query:
CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);
html file
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="file" id="userfile" name="userfiles"> <br>
<input type="submit" id="upload" value="Uploadimage "name="uploads"onclick="upload()">
</form>
I don't know about that SQL query, it doesn't tell much.
What you need to do is base64 encode the image and save this string to your database.
Similarly when you want to pull the image and display it, you will need to decode the string back to an image.
Related
I have an input that's in form. I enter the URL adress into this input, click sumbit, and below it shows if this address is available or not. This is done.
Now I want to make this information show up automatically (without clicking submit), after making a change in the input? I would also like the value in the input to not disappear after checking. How can I do that?
I would like it to look more or less like this, but instead of email correctness, URL availability - https://youtu.be/HzJngc-Se9Q
<form action="" method="GET" name="form1" id="form1">
<div id="custom-search-input">
<div class="adress-div">
<input type="text" id="ok" name="domain" maxlenght="30" class="adress" pattern="(.{1,})?([.]{1})?.+[.]{1}.+" placeholder="np. www.page.com" title="Enter URL adress." autocomplete="off" required/>
<br>
<input type="submit" class="button2" value="Check!">
</div>
</div>
</form>
<?php
error_reporting(0);
if(isset($_GET['domain'])){
$domain = $_GET['domain'];
$godaddycheck = 'https://in.godaddy.com/domains/searchresults.aspx?checkAvail=1&tmskey=&domainToCheck='.$domain.'';
$namecomcheck = 'https://www.name.com/domain/search/'.$domain.'';
$registercomcheck = 'http://www.register.co, m/domain/search/wizard.rcmx?searchDomainName='.$domain.'&searchPath=Default&searchTlds=';
if ( gethostbyname($domain) != $domain ) {
echo "<br><br><h1 style='color: #e30000;'><b>$domain</b> not available.</h1>";
}
else {
echo "<br><br><h1 style='color: #00e339;'><b>$domain</b> available.</h1><h2>
</h2>";
}
}
?>
This is not possible doing only with PHP.
You will need to do that with JavaScript. Therefore you need to
listen on an input change for your <input>field
Send the value of the input field with AJAX (https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started) to a PHP script, which then validates the domain
Fetch the response and display it to the user
But there are a lot of tutorials out there, how to do that. For example: https://www.w3schools.com/php/php_ajax_php.asp
I have created a search query to filter records from the database. Whenever I enter the value I want to filter for eg the email as a criteria and click on search, it retrieves the record from the database, if I click on search again instead of adding the new search result it repopulate the list with the new records and keeping the old record in view.
editted
this is adminList initialization
List<Admin> adminList = new ArrayList<Admin>();
this is the query I am using to search
#SuppressWarnings("unchecked")
public List<Admin> getAllAdmins(String singleAdmin) {
try{
String query = "SELECT DISTINCT e.* FROM admin e WHERE e.email like '%"+ singleAdmin +"%'";
List<Object[]> adminObjects = fetchAll(query);
for(Object[] adminObject: adminObjects) {
Admin adminIndividual = new Admin();
Integer id = ((Integer) adminObject[0]);
String email = (String) adminObject[1];
String name = (String) adminObject[2];
adminIndividual.setId(id);
adminIndividual.setEmail(email);
adminIndividual.setName(name);
adminList.add(adminIndividual);
}
System.out.println("database values for admin>>>>> "+adminList);
return adminList;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
this is the snippet of code in my .jsp file that send the data to the controller to make a search for the query criteria
<form method="post" action="searchAdmin">
<p><label for="name">Admin Email</label>
<input class="form-control" type='text' name='searchName' id='searchName'/>
<input class="btn btn-success" type='submit' value='RETRIEVE'/>
</p>
this is how I am populating the list that gets the values whenever a new search is made
<c:forEach items="${adminList}" var="admin">
<p>
<label for="name">FULLNAME</label>
<input type="text" name="name" placeholder="" value="${admin.name}" readonly/>
</p>
<p>
<label for="email">Email</label>
<input type="text" name="email" placeholder="" value="${admin.email}" readonly/>
</p>
</c:forEach>
you can see the fullname repeating twice and if I enter an email again to click on search, it doubles to three retaining the two previous records. Please how do I tackle this issue whenever I try to search, as the page refreshes let is populates the search query with out repeating with the previous search query. Kindly assist!
I am trying to store the filename of the selected file to be uploaded into a hidden input field on the form. my form looks like this
<form id="uploadattachment" enctype="multipart/form-data"
method="post" action="/governance/attachmentfilestore">
<cfif isDefined("fileUpload")>
<cffile action="upload"
fileField="fileUpload"
accept="application/pdf"
nameconflict="makeunique"
destination="#ExpandPath( '/files/governance/upr/' )#">
<input type="hidden" name="filename" id="filename" value="">
<input type="hidden" readonly id="uprUUID" name="uprUUID"
style="width: 400px" value="<cfoutput>#params.key#</cfoutput>"/>
<input type="hidden" readonly id="status" name="status"
style="width: 400px" value="1"/>
<input name="fileUpload" type="file" style="width: 200px;" />
<button type="submit" name="action"
class="submitBtn primary rightSubmitBtnSpace">Upload</button>
</form>
This is then sent to the controller which writes it to the database how ever I cannot work out a way to get the name of the file to store in the "filename" field.
Does anyone have a solution on how you can populate a field with the name of the file that is selected to be uploaded?
I have added the CFFILE.serverFile in and it worked once, but I'm guessing thats because it grabbed the previously uploaded files name.
Now when loading the page I get Serverfile is undefined in CFFILE and so it does not let me populate the form with the files name.
My code looks like this now to try and work around it how ever this doesn't seem to work either.
<cfif isDefined("CFFILE.serverFile")>
<cfset form.filename = CFFILE.serverFile>
<cfelse>
<cfset form.filename = "null">
</cfif>
<input type="hidden" name="filename" id="filename"
value="<cfoutput>#CFFILE.serverFile#</cfoutput>"/>
The filename does not become available until the file is uploaded. This happens after the form is posted. The only way around this is to try posting the fileupload via AJAX and then returning the filename.
Otherwise, you can assign the value to the field after the file is upload and the form is posted.
<cfset form.filename = CFFILE.serverfile>
You can find the file name before saving.
Railo:
GetPageContext().formScope().getUploadResource("myFormField").getName()
Adobe:
function getClientFileName(fieldName) {
var tmpPartsArray = Form.getPartsArray();
var clientFileName = "";
if (IsDefined("tmpPartsArray")) {
for (local.tmpPart in tmpPartsArray) {
if (local.tmpPart.isFile() AND local.tmpPart.getName() EQ arguments.fieldName) {
return local.tmpPart.getFileName();
}
}
}
return "";
}
Source: http://www.stillnetstudios.com/get-filename-before-calling-cffile/
As lvmisooners said,
GetPageContext().formScope().getUploadResource("myFormField").getName()
works for Railo (and Lucee) but I noticed an interesting wrinkle: if the browser is IE than this returns the full source path including the filename. Firefox and Chrome on the other hand, return only the filename.
For my application I need the full path, but haven't been able to find that if the browser is FireFox or Chrome. If anyone has any ideas I would be most grateful!
(Sorry for not replying to lvmisooners but I don't have the reputation points to reply.)
I want to put some hidden columns in the Webgrid. there a few key columns in my output which are not to be shown to the users but I need them to be present in the webgrid. I require these columns in the grid because when a user selects the row in the grid and clicks on modify I need those keys to get the details of selected columns.Can you please tell me how can I have a hidden column in the webgrid?(razor vb.net syntax please)
See Bianca Huluban's answer posted here: http://forums.asp.net/t/1750407.aspx/1
#{
Dim grid As New WebGrid(Model)
#grid.GetHtml( _
columns:= grid.Columns(grid.Column( _
null, _
null, _
format: #<input type="hidden" name="itemId" value="#item.Id"/>))
}
You can put hidden column inside the S.No. column
grid.Column(header: "S.No.", format: #<text> <div> #(item.WebGrid.Rows.IndexOf(item) + 1) <input type="hidden" value="#item.Id" /> </div> </text>),
enjoy this will not show the column space also
I'm trying to use a Checkboxlist in MonoRail to represent a many to many table relationship. There is a Special table, SpecialTag table, and then a SpecialTagging table which is the many to many mapping table between Special and SpecialTag.
Here is an excerpt from the Special model class:
[HasAndBelongsToMany(typeof(SpecialTag),
Table = "SpecialTagging", ColumnKey = "SpecialId", ColumnRef = "SpecialTagId")]
public IList<SpecialTag> Tags { get; set; }
And then in my add/edit special view:
$Form.LabelFor("special.Tags", "Tags")<br/>
#set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags))
#foreach($specialTag in $items)
$items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name)
#end
The checkboxlist renders correctly, but if I select some and then click Save, it doesn't save the special/tag associations to the SpecialTagging table (the entity passed to the Save controller action has an empty Tags list.) One thing I noticed was that the name and value attributes on the checkboxes are funky:
<label for="special_Tags">Tags</label><br>
<input id="3" name="special.Tags[0]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="3">Buy 1 Get 1 Free</label>
<input id="1" name="special.Tags[1]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="1">Free</label>
<input id="2" name="special.Tags[2]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="2">Half Price</label>
<input id="5" name="special.Tags[3]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="5">Live Music</label>
<input id="4" name="special.Tags[4]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="4">Outdoor Seating</label>
Anyone have any ideas?
Thanks!
Justin
The checkboxlist renders correctly
it seems to me that you could also render something like
<input type="checkbox" name="special.Tags" value="1"/>
<input type="checkbox" name="special.Tags" value="2"/>
which make it simpler (no index to output for the name, it will be properly resolved as an array via controller action parameter binding
also, in your sample, the fact that all checkboxes having the same value UCampus.Core.Models.SpecialTag is probably not right, you may want to output actual primary key identifier from the tags (not sure, could you display the class you are binding back on the form handling action?)
I was able to get it working by specifying the id and text attributes...
$Form.LabelFor("special.Tags", "Tags")<br/>
#set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags, "%{value='Id', text='Name'}"))
#foreach($specialTag in $items)
$items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name)
#end