 |
|
|
 |
 |
|
คำสั่ง sql เวลาอัพเดตมันจะดึงมาจากตารางเดียวกันไม่ได้น่ะครับ ไม่งั้นผมคงจะหาคำสั่งที่หาเลขก่อนหน้านี้ทีมีค่ามากที่สุด แล้วเอามาแก้และอัพเดต บรรทัดเดียวเลย
ฉะนั้น เรามาแยกทีละส่วนดีกว่าครับ
1. หาค่าที่มากที่สุด ก็คือไอดีล่าสุด จากอันก่อน
2. เอามาแปลงเป็นโค้ด
Code (PHP)
<?php
$sql = "SELECT id FROM test ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
sscanf($row[0] ,"C%d" , $id);
?>
จากชุดคำสั่งข้างบนนี้เป็นการหา ไอดีล่าสุด โดยต้องการแค่ตัวเลขเท่านั้น
Code (PHP)
<?php
$sql = "INSERT INTO test(id) VALUES(CONCAT('C',RIGHT(CONCAT('0000' , $id) , 4)))";
mysql_query($sql);
?>
ส่วนอันนี้หลักการง่ายๆครับ มาดูฟังชั่นชั้นในสุดก็คือ right ก็จะเป็นการตัดตัวอักษรตามจำนวน โดยจะเริ่มจากทางขวา สมมุตตัวแปร $id = 999 มันก็จะรวมกับ 0000 ด้วยคำสั่ง CONCAT กลายเป็น 0000999 และมันคลุมด้วยคำสั่ง Right ระบุขนาด 4 ฉะนั้น มันจะตัดเหลือ 0999 แล้วมันถูกคลุมด้วย CONCAT ชั้นนอกอีกที ที่เอาตัวอักษร 'C' มาบวกกับตัวอักษรเมื่อกี้ มันจึงกลายเป็น 'C0999' และเก็บลงดาต้าเบสครับผม
|
 |
 |
 |
 |
Date :
2010-04-02 16:39:00 |
By :
kenessar |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่หนุ่มมา แชร์ประสบการกับผมอีกแล้ว ดีดีดีดี อิอิอิ ขอบคุณอีกรอบครับ พี่ เดี๋ยวผมลองเอาไปทำดู ออ id ยังเก็บเป้น varchar อยุ่ใช่ไหมครับ
|
 |
 |
 |
 |
Date :
2010-04-02 17:09:20 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้เป็นในกรณีว่า Primary Key ของตารางนั้นๆ เป็นชนิด varchar น่ะครับ
แต่คลาสที่ได้ไปอันก่อน มันเป็นกรณีที่มี Primary Key เป็น auto_increment และมีอีกคอลัมน์เป็น varchar
แต่ถ้าจะให้ Primary Key เป็น varchar อันนี้ได้แน่นอนครับ
|
 |
 |
 |
 |
Date :
2010-04-02 18:03:24 |
By :
kenessar |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังงี่ผมต้องเขียน code ทำ auto number เองใช่ไหมครับ
|
 |
 |
 |
 |
Date :
2010-04-02 20:19:03 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ kenessar ยังงี่ผมต้องทำ auto id ขึ้นมาเองใช่ไหมครับ พี่ครับ
|
 |
 |
 |
 |
Date :
2010-04-03 09:56:39 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ต้องหรอกครับ เพราะ ถ้าเอาโค้ดในคอมเม้น No.1 ไป ก็ไม่ต้องมี auto_id แต่อย่างใด
แต่มันจะมีจุดอ่อนตรงที่ว่า เมื่อเรายังไม่มีสักเรคคอร์ดในตาราง มันจะดึงรหัสล่าสุดมาไม่ได้ ฉะนั้น ก่อนจะเริ่มโค้ดที่ให้มานั้น ต้องตรวจก่อนนะครับ ว่า มีเรคคอร์ดรึยัง โดยใช้ฟังชั่น count ใน mysql นับเรคคอร์ดทั้งหมดออกมา หรือจะใช้ mysql_num_rows ของ php ก็ได้
|
 |
 |
 |
 |
Date :
2010-04-03 13:42:34 |
By :
kenessar |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?php
$sql = "SELECT id FROM test ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
$row = mysql_fetch_array($result);
sscanf($row[0] ,"C%d" , $id);
$sql = "INSERT INTO test(id) VALUES(CONCAT('C',RIGHT(CONCAT('0000' , $id) , 4)))";
}
else
{
$sql = "INSERT INTO test(id) VALUES('C0001')";
}
mysql_query($sql);
?>
|
 |
 |
 |
 |
Date :
2010-04-03 13:57:10 |
By :
kenessar |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ kenessar มันขึ้น err0r mysql_num_row :: supplie argement
script ที่เขียนบันทึก
Code (PHP)
<?
session_start();
$sql = "select t_id from tb_type order by t_id desc limit 1";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
$row = mysql_fetch_array($result);
sscanf($row[0] ,"C%d" , $id);
$sql = "INSERT INTO test(id) VALUES(CONCAT('C',RIGHT(CONCAT('0000' , $id) , 4)))";
}
else
{
$sql = "INSERT INTO t_type(t_id) VALUES('C0001')";
}
mysql_query($sql);
if ($result) {
echo"<meta http-equiv=\"refresh\" content=\"0;URL=type_show.php\" />";
} else {
echo "<script>alert(' ไม่สามารถเพิ่มประเภทสินค้าได้น่ะครับ ');history.back();</script>";
echo"<meta http-equiv=\"refresh\" content=\"1;URL=type_show.php\" />";
}
mysql_close();
?>
|
 |
 |
 |
 |
Date :
2010-04-04 10:19:25 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังไม่ connect database แล้วจะคิวรี่ได้อย่างไรครับผม
|
 |
 |
 |
 |
Date :
2010-04-04 13:52:29 |
By :
plakrim |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อออ ไม่ error แล้วครับ พี่ plakrim แต่มันบันทึกลง db ไม่ได้ครับ ช่วยดูให้ทีน่ะครับ ขอบคุณครับ
เส้นผมแท้ๆ กำ
|
 |
 |
 |
 |
Date :
2010-04-04 14:50:25 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ยังบันทึกไม่ได้เลยครับ
|
 |
 |
 |
 |
Date :
2010-04-05 09:06:02 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP) ยังบันทึกไม่ได้เลยครับ ตรง $id คือตัวแปรตัวไหนอะครับ
<?
session_start();
$sql = "select t_id from tb_type order by t_id desc limit 1";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
$row = mysql_fetch_array($result);
sscanf($row[0] ,"C%d" , $id);
$sql = "INSERT INTO test(id) VALUES(CONCAT('C',RIGHT(CONCAT('0000' , $id) , 4)))";
}
else
{
$sql = "INSERT INTO t_type(t_id) VALUES('C0001')";
}
mysql_query($sql);
if ($result) {
echo"<meta http-equiv=\"refresh\" content=\"0;URL=type_show.php\" />";
} else {
echo "<script>alert(' ไม่สามารถเพิ่มประเภทสินค้าได้น่ะครับ ');history.back();</script>";
echo"<meta http-equiv=\"refresh\" content=\"1;URL=type_show.php\" />";
}
mysql_close();
?>
|
 |
 |
 |
 |
Date :
2010-04-05 09:08:16 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
error?
database connected?
Debug
Code (PHP)
mysql_query($sql) or die("Error : " . mysql_error(). "SQL : " . $sql);
|
 |
 |
 |
 |
Date :
2010-04-05 12:32:53 |
By :
plakrim |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
code นี้ connect แล้ว เหมือนเดิมครับ
Code (PHP)
<?
session_start();
include "connect.php";
$sql = "SELECT t_id FROM tb_type ORDER BY t_id DESC LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
$row = mysql_fetch_array($result);
sscanf($row[0] ,"C%d" , $id);
$sql = "INSERT INTO tb_type(t_id) VALUES(CONCAT('C',RIGHT(CONCAT('0000' , $id) , 4)))";
}
else
{
$sql = "INSERT INTO tb_typet(t_id) VALUES('C0001')";
}
mysql_query($sql);
if ($result) {
echo"<meta http-equiv=\"refresh\" content=\"0;URL=type_show.php\" />";
} else {
echo "<script>alert(' ไม่สามารถเพิ่มประเภทสินค้าได้น่ะครับ ');history.back();</script>";
echo"<meta http-equiv=\"refresh\" content=\"1;URL=type_show.php\" />";
}
mysql_close();
?>
|
 |
 |
 |
 |
Date :
2010-04-05 14:39:12 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณพระเจ้า ได้แล้ว 555 ขอบคุณ คุณ kenessar และ พี่ plakrim ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2010-04-05 21:02:34 |
By :
chonburi f.c |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|