สอบถามคับ เวลาเราแก้ไขข้อมูลในฟอร์มการแก้ไขมีบางฟิวไม่ยอมแสดงข้อมูจากฐานขึ้นมาเช่น ฟิวของ Tel ,Address และฟิวแบบ Select Option ไม่ทราบว่าเกิดจากอะไร ขอแนวทางจากท่านผู้รู้ในการเขียนหรือปรับปรุงแก้ไขโค้ดหน่อยคับ
<?
include 'includes/connect.';
$myedit = $_GET['edit'];
$sql = "SELECT * FROM user WHERE id = '".$myedit."'";
$result = mysql_query($sql);
$rs = mysql_fetch_array($result);
?>
<form action="edit_st_process." method="post" name="formadd" enctype="multipart/form-data">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
<img src="<? echo $rs['photo'];?>" width="200px" height="150px" alt="อัพโหลดรูปภาพนักเรียน">
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
<div>
<span class="btn btn-default btn-file"><span class="fileinput-new">เลือกรูปภาพ</span><span class="fileinput-exists">เปลี่ยน</span><input type="file" name="fileUpload"></span>
<a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">ลบ</a>
</div>
</div>
<script>
$('.fileinput').fileinput()
</script>
<script>
$(document).ready (function(){
$("#success-alert").hide();
$("#myWish").click(function showAlert() {
$("#success-alert").alert();
window.setTimeout(function () {
$("#success-alert").alert('close'); }, 2000);
});
});
</script>
<div class="form-group">
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" name="e[username]" class="form-control" value="<? echo $rs['username'];?>" style="" >
</div>
</div>
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-key"></i></span>
<input type="password" name="e[password]" class="form-control" value="<? echo $rs['password'];?>" style="">
</div>
</div>
<div class="col-lg-4">
</div>
<br>
<hr>
</div>
<div class="form-group">
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-edit"></i></span>
<input type="text" class="form-control" name="e[firstname]" value="<? echo $rs['firstname'];?>" required="">
</div>
</div>
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-edit"></i></span>
<input type="text" class="form-control" name="e[lastname]" value="<? echo $rs['lastname'];?>" required="">
</div>
</div>
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-edit"></i></span>
<input type="text" class="form-control" name="e[email]" value="<? echo $rs['email'];?>" required="">
</div>
</div>
<br><br>
</div>
<div class="form-group">
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-credit-card"></i></span>
<input type="text" class="form-control" name="e[st_id]" value="<? echo $rs['st_id'];?>" >
</div>
</div>
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-database"></i></span>
<?
$sql = "SELECT * FROM level";
$result = mysql_query($sql);
?>
<select class="form-control" name="e[level]" required="" >
<option>---ระดับชั้นเรียน---</option>
<? while ($rs= mysql_fetch_array($result)){ ?>
<option value="<? echo $rs['id']; ?>"> <? echo $rs['level']; ?></option>
<? } ?>
</select>
</div>
</div>
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-glass"></i></span>
<?
$sql = "SELECT * FROM number";
$result = mysql_query($sql);
?>
<select class="form-control" name="e[number]" required="" >
<option>---เลขที่ของนักเรียน---</option>
<? while ($rs= mysql_fetch_array($result)){ ?>
<option value="<? echo $rs['id']; ?>"> <? echo $rs['number']; ?></option>
<? } ?>
</select>
</div>
</div>
<br><br>
</div>
<div class="form-group">
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-male"></i> <i class="fa fa-female"></i></span>
<?
$sql = "SELECT * FROM sex";
$result = mysql_query($sql);
?>
<select class="form-control" name="e[sex]" required="" >
<option >เพศ</option>
<? while ($rs= mysql_fetch_array($result)){ ?>
<option value="<? echo $rs['id']; ?>"> <? echo $rs['sex']; ?></option>
<? } ?>
</select>
</div>
</div>
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-mobile"></i></span>
<input type="text" class="form-control" name="e[tel]" value="<? echo $rs['tel'];?>">
</div>
</div>
<div class="col-lg-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-eye-slash"></i></span>
<?
$sql = "SELECT * FROM status";
$result = mysql_query($sql);
?>
<select class="form-control" name="e[status]" required="" >
<option>เลือกสถานะการใช้งาน</option>
<? while ($rs= mysql_fetch_array($result)){ ?>
<option value="<? echo $rs['id']; ?>"> <? echo $rs['status']; ?></option>
<? } ?>
</select>
</div>
</div>
<br><br>
</div>
<div class="form-group">
<div class="col-lg-6">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
<textarea class="form-control" name="e[address]" placeholder="ที่อยู่" value="<? echo $rs['address'];?>" required=""></textarea>
</div>
</div>
<div class="col-lg-6"></div>
<br><br><br>
</div>
<div class="form-group">
<div class="input-group">
<div class="col-lg-12">
<button type="submit" name="add" class="btn btn-success btn-flat">แก้ไขข้อมูล</button> | <button type="reset" name="reset" class="btn btn-warning btn-flat">ล้างข้อมูล</button>
</div>
</div>
</div>
</form>
อันนี้คือโค้ดปุ่มการคลิกที่จะไปแก้ไขข้อมูล
<a class="btn btn-warning btn-flat" href="edit_st.?edit=<? echo $rs['id']; ?>"><i class="fa fa-edit"></i></a>
สาเหตุที่มันไม่แสดงเพราะตัวแปร $rs ถูกเปลี่ยนค่า ดังนั้นใน Select ต้องใช้ตัวแปรอื่นที่ไม่ใช้ $rs ในการ fetch_array() ครับ เช่น
<? while ($rsl= mysql_fetch_array($result)){ ?>
<option value="<? echo $rsl['id']; ?>" <?php if($rsl['id']==$rs['level_id']){?> selected="selected"<?php }?>> <? echo $rsl['level']; ?></option>
<? } ?>
แล้วในส่วนของฟิว Tel , Address มันก็ยังไม่ดึงข้อมูลมาครับ ไม่ทราบว่าเกิดผิดพลาดจากโค้ดส่วนตรงไหนครับ
<?php
include 'includes/connect.php';
$myedit = $_GET['edit'];
$sql = "SELECT * FROM user WHERE id = '".$myedit."'";
$result = mysql_query($sql);
$rs = mysql_fetch_array($result);
?>
<input type="text" class="form-control" name="e[tel]" value="<?php echo $rs['tel'];?>" placeholder="เบอร์โทร">
<textarea class="form-control" name="e[address]" placeholder="ที่อยู่" value="<?php echo $rs['address'];?>" required=""></textarea>
เกิดข้อผิดพลาดระหว่างการแก้ไข และข้อมูลก็ไม่เข้าฐานเลยคับ
Warning: unlink(): Invalid argument
เกิดจากอะไรครับ
<?php
include 'includes/connect.php';
$mye = $_POST['e'];
$ext = findexts($_FILES['fileUpload']['name']);
$target = "img/upload/users/";
$target = $target . time() . $ext;
if (trim($_FILES["fileUpload"]["tmp_name"]) != "") {
$sqle = "SELECT * FROM user WHERE id = '" . $mye . ['edit'] . "'";
$resulte = mysql_query($sqle);
$rse = mysql_fetch_array($resulte);
unlink($rse['photo']);
$images = $_FILES["fileUpload"]["tmp_name"];
$new_images = "Thumbnails_" . $_FILES["uploaded"]["name"];
copy($_FILES["fileUpload"]["tmp_name"], $target);
//$width = 200; //*** Fix Width & Heigh (Autu caculate) ***//
//$size = GetimageSize($images);
//$height = round($width * $size[1] / $size[0]);
//$images_orig = ImageCreateFromJPEG($images);
//$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);
//ImageJPEG($images_fin, $target);
//ImageDestroy($images_orig);
//ImageDestroy($images_fin);
$sqle = "UPDATE user SET
username = '" . $mye['username'] . "',
password = '" . $mye['password'] . "',
firstname '" . $mye['firstname'] . "',
lastname = '" . $mye['lastname'] . "',
sex_id = '" . $mye['sex'] . "',
level_id = '" . $mye['level'] . "',
number_id = '" . $mye['number'] . "',
email = '" . $mye['email'] . "',
st_id = '" . $mye['st_id'] . "',
tel = '" . $mye['tel'] . "',
address = '" . $mye['address'] . "',
status_id = '" . $mye['status'] . "',
modified = '" . date("Y-m-d H:i:s") . "',
photo = '" . $target . "'";
} else {
$sqle = "UPDATE user SET
username = '" . $mye['username'] . "',
password = '" . $mye['password'] . "',
firstname '" . $mye['firstname'] . "',
lastname = '" . $mye['lastname'] . "',
sex_id = '" . $mye['sex'] . "',
level_id = '" . $mye['level'] . "',
number_id = '" . $mye['number'] . "',
email = '" . $mye['email'] . "',
st_id = '" . $mye['st_id'] . "',
tel = '" . $mye['tel'] . "',
address = '" . $mye['address'] . "',
status_id = '" . $mye['status'] . "',
modified = '" . date("Y-m-d H:i:s") . "'";
}
if (mysql_query($sql)) {
echo '<div class="alert alert-success" id="success-alert">
<button type="button" class="close" data-dismiss="alert">x</button>
<i class="icon fa fa-check"></i> <strong>ข้อความจากระบบ! </strong>
แก้ไขเรียบร้อยแล้ว.
</div>';
} else {
echo '<div class="alert alert-danger" id="success-alert">
<button type="button" class="close" data-dismiss="alert">x</button>
<i class="icon fa fa-warning"></i> <strong>แจ้งเตือน! </strong>
ไม่สามารถแก้ไขได้.
</div>';
}
?>
ตรวจสอบคำสั่ง sql แล้วคับ แต่ข้อมูลก็ยังไม่อัพเดทครับเกิดผิดพลาดของโค๊ดตรงไหนครับ
<?php
include 'includes/connect.php';
if(isset($_GET['edit'])){
$myedit = $_GET['edit'];
$sql = "SELECT * FROM user WHERE id = '" . $myedit . "'";
$result = mysql_query($sql);
$rs = mysql_fetch_array($result);
}
?>
<?php
if(isset($_POST['e']['edit'])){
$e = $_POST['e'];
$sqle = "UPDATE user SET
username = '" . $e['username'] . "',
password = '" . $e['password'] . "',
firstname '" . $e['firstname'] . "',
lastname = '" . $e['lastname'] . "',
sex_id = '" . $e['sex'] . "',
level_id = '" . $e['level'] . "',
number_id = '" . $e['number'] . "',
email = '" . $e['email'] . "',
st_id = '" . $e['st_id'] . "',
tel = '" . $e['tel'] . "',
address = '" . $e['address'] . "',
status_id = '" . $e['status'] . "',
modified = '" . date("Y-m-d H:i:s") . "'
WHERE id = '" .$e['id']."'";
$rse = mysql_query($sqle);
}
?>
<form action="<?php echo $_SERVER['PHP_SELFT']; ?>" method="post" name="formadd" enctype="multipart/form-data">
<?php if(isset($_GET['edit'])){ ?>
<input type="hidden" name="e[edit]" value="1">
<input type="hidden" name="e[id]" value="<?php echo $rs['id'];?>">
<?php }else{ ?>
<input type="hidden" name="e[insert]" value="1">
<?php } ?>
ใน php myadmin ฟ้องว่า
Error
SQL query:
UPDATE user SET username = 'user', password = 'user', firstname 'ทดสอบอีกแล้ว', lastname = 'ทดสอบนามสกุล', sex_id = '1', level_id = '13', number_id = '14', email = 'demo@gmail.com', st_id = '4564', tel = '3453534', address = ' Test ', status_id = '1', modified = '2016-06-27 15:19:29' WHERE id = '6'
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ทดสอบอีกแล้ว', lastname = 'ทดสอบนามส' at line 1
ตอบ/อธิบาย