This code excerpt from one of my projects demonstrates how to save an image file like JPEG(*.jpg;*.jpeg;*.jpe;*.jfif); GIF, PNG, BMP or even All Files(*.*) into a Mysql Database server. As of the time of this writing I was using Mysql version 5.1.32
You can visit the following sites for references:
Their is a 'Browse Image' button that calls an OpenFileDialog that filters the images, once the user selected the desired image it will be displayed to the PictureBox.
After it is displayed in the PictureBox you can click on the 'Save' button to save the image to the database.
This is my the code:
------------------------------
Private mImageFile As Image
Private mImageFilePath As String
'From Browse Image button
Private Sub Buttonbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonbrowse.Click
Try
OpenFileDialogpic.Filter =
"JPEG(*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|GIF(*.gif)|*.gif|PNG(*.png)|*.png|BMP(*.bmp)|*.bmp|All
Files(*.*)|*.*"
OpenFileDialogpic.ShowHelp = True
OpenFileDialogpic.FilterIndex = 1
OpenFileDialogpic.FileName = ""
If OpenFileDialogpic.ShowDialog = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
If System.IO.File.Exists(OpenFileDialogpic.FileName) = False Then
Exit Sub
Else
TextBoxpicture.Text = OpenFileDialogpic.FileName
mImageFilePath = TextBoxpicture.Text
'Display image
mImageFile = Image.FromFile(mImageFilePath)
PictureBoxenrollment.Image = mImageFile
PictureBoxenrollment.Invalidate()
'TextBoxpicture.Invalidate()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
'
'From Save button
'Blob image
'
Dim FileSize As UInt32
'Write an image in a stream
Dim fspic As FileStream = New FileStream(mImageFilePath, FileMode.Open, FileAccess.Read)
FileSize = fspic.Length
Dim rawdata() As Byte = New Byte(FileSize) {}
fspic.Read(rawdata, 0, FileSize)
fspic.Close()
mysqlcon.Open()
mImageFile = Image.FromFile(mImageFilePath.ToString())
Dim save As String = "INSERT INTO enrollment VALUES" & _ "('" & fname & "', '" & mname & "', '" & lname & "', " & age & "," & _ "'" & address & "', '" & gender & "', '" & date_of_birth & "', '" & place_of_birth & "'," & _ "'" & year_level & "', '" & id_no & "', '" & date_enrolled & "', '" & sy_a & "', '" & sy_b & "'," & _ "'" & contact_no & "', '" & parent_ & "', '" & guardian & "', @mImageFile , '" & transfeeree & "'," & _ "'" & date_of_transfer & "','" & transfer_in & "', '" & transfer_out & "')"
Dim cmd As New MySqlCommand(save, mysqlcon)
cmd.Parameters.AddWithValue("@mImageFile", rawdata)
'
'Blob image
cmd.ExecuteNonQuery()
mysqlcon.Close()
MessageBox.Show("Records and Image Saved!", "Save to Database", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button3, MessageBoxOptions.RightAlign, True)
Thursday, October 8, 2009
Friday, May 15, 2009
Updating database in mysql using php
$Userid = $_POST['User_id'];
$Username = $_POST['User_name'];
$Userpassword = $_POST['User_password'];
mysql_connect("localhost","root","") or die ('Error: ' . mysql_error());
mysql_select_db("it");
$query = "UPDATE users SET User_id='".$Userid."', User_name='".$Username."', User_password='".$Userpassword."' WHERE User_id='".$Userid."'";
mysql_query($query) or die ('Error updating user');
echo "User updated with: " .$Userid. " " .$Username. " " .$Userpassword;
?>
$Username = $_POST['User_name'];
$Userpassword = $_POST['User_password'];
mysql_connect("localhost","root","") or die ('Error: ' . mysql_error());
mysql_select_db("it");
$query = "UPDATE users SET User_id='".$Userid."', User_name='".$Username."', User_password='".$Userpassword."' WHERE User_id='".$Userid."'";
mysql_query($query) or die ('Error updating user');
echo "User updated with: " .$Userid. " " .$Username. " " .$Userpassword;
?>
Wednesday, May 13, 2009
Retrieving data from the database and printing it on the screen
mysql_connect("localhost","root","") or die ('Error: ' . mysql_error());
mysql_select_db("it");
$query = "SELECT * FROM users";
$data = mysql_query($query) or die ('Error displaying data');
while($info = mysql_fetch_array($data))
{
print "Id:" .$info['User_id'] . " ";
print "Name:" .$info['User_name']. " ";
print "Password:" .$info['User_password']. "
";
}
mysql_select_db("it");
$query = "SELECT * FROM users";
$data = mysql_query($query) or die ('Error displaying data');
while($info = mysql_fetch_array($data))
{
print "Id:" .$info['User_id'] . " ";
print "Name:" .$info['User_name']. " ";
print "Password:" .$info['User_password']. "
";
}
Inserting data into mysql database using php
$Userid = $_POST['User_id']; //this is the column of table users
$Username = $_POST['User_name']; //
$Userpassword = $_POST['User_password']; //
mysql_connect("localhost","root","") or die ('Error: ' . mysql_error());
mysql_select_db("it");
$query = "INSERT INTO users (User_id,User_name,User_password) VALUES ('".$Userid."','".$Username."','".$Userpassword."')";
mysql_query($query) or die ('Error creating users');
echo "User Created With: " .$Userid. " " .$Username. " ".$Userpassword;
?>
Subscribe to:
Posts (Atom)