|
 |
|
การบันทึกข้อมูลแบบระบุฟิวส์มีปัญหาในการบันทึกครับอยากทราบวิธีการเขียน sql |
|
 |
|
|
 |
 |
|
ผมต้องการบันทึกรูปภาพลง db แต่ติดปัญหาคือต้องใช้ sql แบบไหนข้อมูลรูปภาพจะลง ตาม id ที่กำหนดคือข้อมูลอันเก่ามีอยู่แล้วอยากเพิ่ม รูปภาพลงไปก็เท่านั้นรุปลงแต่ชื่อไม่ลง ไม่ทราบว่าต้องใช้ sql แบบไหน
จากตัวอย่าง ผมมีเขียน code ประมาณนี้ครับ
Code (PHP)
<?
include "connect.php";
$mid=$_SESSION['mid'];
$folderpic = "memberpic" ; //ชื่อโฟลเดอร์ที่ไว้ใช้เก็บไฟล์
$max_size = 500; // จำกัดขนาดไฟล์ ค่าเป็น kb
$width=150; //ขนาดความกว้างที่ต้องการ Resize
$height=150; // ขนาดความสูงที่ต้องการ Resize
$file_size=$_FILES['filUpload']['size']; // ตัวแปรขนาดของไฟล์
$limit_size=1000*$max_size;
if($file_size >= $limit_size){
echo "<script language='javascript'>alert('ไฟล์ของคุณใหญ่เกินไปครับต้องมีขนาดไม่เกิน $max_size kb');</script>";
} else {
if($_FILES["filUpload"]["tmp_name"]){
$fileUpload=$_FILES["filUpload"]["tmp_name"]; // ตัวแปร temp
$fileUpload_name=$_FILES["filUpload"]["name"]; // ตัวแปรชื่อไฟล์
if (isset($_FILES[filUpload][name])) { // ถ้ามีการเซทชื่อไฟล์มาให้ประมวลผลส่วนนี้
//*** ส่วนนี้คือการ Rename รูปให้เป็นวันที่ที่บันทึก ***//
$ext = strtolower(end(explode('.', $fileUpload_name)));
$dateUpFile=date("YmdHis");
$newname=$dateUpFile.".".$ext; //ชื่อไฟล์ที่กำหนดขึ้นมาใหม่ โดยใช้วันและเวลาเป็นตัวกำหนดตั้งชื่อ
//*** สร้างภาพขึ้นมาใหม่ตามนามสกุลของไฟล์เดิม ***//
$images = $fileUpload;
$size = getimagesize($images);
$img_type=strtolower(end(explode('.',$images)));
if($size[2] == 1) {
$images_orig = imagecreatefromgif($images); // GIF
} else if($size[2] == 2) {
$images_orig = imagecreatefromjpeg($images); // JPEG
}
else if($size[2] == 3) {
$images_orig = imagecreatefrompng($images); // png
}
//*** ส่วนนี้คือการ Resize รูป ***//
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = imagecreatetruecolor($width, $height);
imagecopyresampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); // ทำการ Resize ภาพ
imagegif($images_fin,"$folderpic/".$newname);
imagedestroy($images_orig);
imagedestroy($images_fin);
}
$sql = mysql_query("insert into tbl_member(mem_pic) where mem_id='$mid' value('$newname')") or die(mysql_error());
if ($sql) {
echo"<script>alert('เสร็จสินขั้นตอนการเปลี่ยนรูปภาพ');self.opener.location='main.php';window.close();</script>";
}else{
echo"<script>alert('ไมสามารถทำรายการได้');history.back();</script>";
}
}
}
?>
Tag : PHP

|
|
 |
 |
 |
 |
Date :
2012-02-01 17:44:22 |
By :
chonburi f.c |
View :
1133 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้การ insert ข้อมูลแรกก่อนครับ ส่วนรุปภาพให้เอา $id ที่ได้จากการ insert มา update ครับ
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
//*** Insert Record ***//
$strSQL = "INSERT INTO album (AlbumName) VALUES ('".$_POST["txtAlbumName"]."') ";
$objQuery = mysql_query($strSQL);
$AlbumID = mysql_insert_id();
if($_FILES["filAlbumShot"]["name"] != "")
{
$fileName = date("YmdHis")."_".$_FILES["filAlbumShot"]["name"];
if(move_uploaded_file($_FILES["filAlbumShot"]["tmp_name"],"myfile/".$fileName))
{
//*** Delete Old File ***//
@unlink("myfile/".$_POST["hdnOldFile"]);
//*** Update New File ***//
$strSQL = "UPDATE album ";
$strSQL .=" SET AlbumShot = '".$fileName."' WHERE AlbumID = '".$AlbumID."' ";
$objQuery = mysql_query($strSQL);
echo "Copy/Upload Complete<br>";
}
}
mysql_close($objConnect);
?>
|
 |
 |
 |
 |
Date :
2012-02-02 11:12:20 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|