|
 |
|
สอบถามเรื่อง auto id เลขที่อัตโนมัติก่อนเพิ่มข้อมูลใหม่ |
|
 |
|
|
 |
 |
|
มันมีปัญหาตรงบรรทัดนี้อ่ะครับ ซึ่งผมลองก๊อป select Max(substr(cus_id,-4))+1 as MaxID from customer ไป run บน SQL มันก็ไม่มีปัญหาอะไร
แต่เวลาบันทึกข้อมูลถ้ายังไม่มี รหัส มันก็สามารถบันทึกได้ แต่พอมาเพิ่มข้อมูลอีกรอบ มันเหมือนไม่ เพิ่มเลขรหัสอ่ะครับ เหมือนมันทำให้ รหัสซ้ำ
แล้วพอกด save แล้วมันขึ้น
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\WebProject\customermanage.php on line 34
มาด้วยอ่ะครับ หรือว่าผมเรียงลำดับโค้ดผิด
Code (PHP)
<?
include('include/function.php');
$dblink = connect_db();
$todo = $_GET['todo'];
$cus_id = $_POST['cus_id'];
$cus_name = $_POST['cus_name'];
$cus_sex = $_POST['cus_sex'];
$cus_add = $_POST['cus_add'];
$cus_tel = $_POST['cus_tel'];
$status = $_POST['status'];
if($todo == 'insert'){
$new_id =mysql_result(mysql_query("select Max(substr(cus_id,-4))+1 as MaxID from customer"),0,"MaxID");
if($new_id==''){
$cus_id="CUS0001";
}else{
$cus_id="CUS".sprintf("%04d",$new_id);
}
$strsql = "
INSERT INTO customer(cus_id, cus_name, cus_sex, cus_add, cus_tel, status)
VALUES('$cus_id','$cus_name','$cus_sex','$cus_add','$cus_tel','$status')
";
$result = mysqli_query($dblink,$strsql);
if($result){
echo '<font color="#FFFFFF" size="+2" face="TH Kodchasal">บันทึกข้อมูลเรียบร้อยแล้ว</font>';
}
else{
echo '<font color="#FFFFFF" size="+2" face="TH Kodchasal">เกิดข้อผิดพลาดเนื่องจากกรอกข้อมูลไม่ครบหรือกรอกข้อมูลผิดพลาด <br> <a href="customer.php">ย้อนกลับ</a></font> ';
}
}
?>
ขอโทษที่รบกวนนะครับ มือใหม่หัดทำ
Tag : PHP

|
ประวัติการแก้ไข 2014-05-12 13:30:34
|
 |
 |
 |
 |
Date :
2014-05-12 13:29:59 |
By :
cartooncup |
View :
1719 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แอบเห็น บรรทัดที่ 27
$result = mysqli_query($dblink,$strsql);
**ไม่แน่ใจว่า มันถูกแล้ว หรือหนูไม่เคยเห็น
|
 |
 |
 |
 |
Date :
2014-05-12 13:39:22 |
By :
kullasom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$result = mysqli_query($dblink,$strsql); เหมือนจะไม่มีปัญหาอ่ะครับ พอดีผมไปดูในคลิปมา แต่มันก็รันผ่านปกตินะครับ
แต่ผมติดตรงนี้อ่ะครับ
$new_id =mysql_result(mysql_query("select Max(substr(cus_id,-4))+1 as MaxID from customer"),0,"MaxID");
แล้ัวพอรันขึ้นมามันก็แจ้งเตือนบรรทัด ^ อ่ะครับอ่ะครับ
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in
งง เลย @_@
|
 |
 |
 |
 |
Date :
2014-05-12 13:45:01 |
By :
cartooncup |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แยกมันหน่อยดีกว่ามั้ยครับ
$new_id =mysql_result(mysql_query("select Max(substr(cus_id,-4))+1 as MaxID from customer"),0,"MaxID");
แปลงร่างเป็น
Code (PHP)
$result = mysql_query("select Max(substr(cus_id,-4))+1 as MaxID from customer") or die( mysql_error());
$row = mysql_fetch_assoc($result);
$new_id = $row['MaxID'];
|
 |
 |
 |
 |
Date :
2014-05-12 16:54:31 |
By :
{Cyberman} |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เฮ่อ max id + 1 -_-''
|
 |
 |
 |
 |
Date :
2014-05-14 09:08:28 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|