Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > (มือใหม่) อยากให้รันรหัสสินค้าแบบอัตโนมัตเข้าDB แต่ไม่ไช้auto_incrementต้องทำยังไงครับ



 

(มือใหม่) อยากให้รันรหัสสินค้าแบบอัตโนมัตเข้าDB แต่ไม่ไช้auto_incrementต้องทำยังไงครับ

 



Topic : 100200



โพสกระทู้ ( 22 )
บทความ ( 0 )



สถานะออฟไลน์




คือใช้ไปกับproductID ไปแล้ว แต่อยากให้ ProductCode เพิ่มตัวมันเองแบบออโต้
pdb
ในที่เพิ่มสินค้าครับ
proe
Code (PHP)
<?
ob_start();
session_start();
if($_SESSION["adminlogin"]=="")
{
header('location:index.php');
exit();
}
 
require('../connect/connect.php');
						
                       if($_GET["Action"]=="Save")
	                   {
$sql = "SELECT *FROM product WHERE ProductCode='$_POST[ProductCode]'";
$re = mysql_query($sql,$conn);
$resultChk = mysql_fetch_array($re);
$strProductCode="".substr("000$strResultProductCode[ProductCode]", -3);	
$sql_up = "update ProductCode set ProductCode=ProductCode+1 ";						
						if($resultChk)
						{
									echo"<script language='JavaScript'>";
									echo"alert('รหัสสินค้านี้มีอยู่แล้ว');";
									echo"</script>";						
						}
						else
						{

					  
								  $sql_up = "insert into product (CatID,ProductCode,ProductName,Price) Values ('$_POST[CatID]','$_POST[ProductCode]','$_POST[ProductName]','$_POST[Price]')";
								  $dbquery_up = mysql_query($sql_up);
											 
					  
									echo"<script language='JavaScript'>";
									echo"alert('เพิ่มข้อมูลเรียบร้อยแล้ว');";
									echo"window.location='product_edit.php?ProductID=".mysql_insert_id()."';";
									echo"</script>";
						}														  
			 		  }
					  
?>
<html>
<title>woodthaishop.com</title>
           <?php include_once "script.php";?>	
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="../css/styles.css" type="text/css">
<div align="center"><br>
  <table cellspacing=1 cellpadding=4 width="68%" bgcolor=#CCCCCC border=0 align="center" height="10">
    <tbody>
      <tr bgcolor=#e5e5e5> 
        <td width="100%" bgcolor="#FFFFFF"> <div align="center"></div>
          <table cellspacing=1 cellpadding=4 width="100%" border=0 align="center" height="10">
            <tbody>
              <tr bgcolor=#e5e5e5> 
                <td width="35%" bgcolor="#FFFFFF"> <div align="left"><b><img src="../image/allrowto.gif" width="11" height="11" align="absbottom"> 
                    เพิ่มสินค้าใหม่</b></div></td>
                <td width="22%" align=middle bgcolor="#FFFFFF"> <div align="left"> 
                  </div></td>
                <td width="43%" bgcolor="#FFFFFF"> <div align="right"><a href="menu.php?rs=pd">กลับไป</a></div></td>
              </tr>
            </tbody>
          </table></td>
      </tr>
    </tbody>
  </table>
</div>

<script language="JavaScript">
function checkvalue()
{
	
		if(document.all.CatID.value =="")
		{
		alert('กรุณาเลือกหมวดหมู่');
		document.all.CatID.focus();
		return false;
		}
		
	/*	if(document.all.ProductCode.value =="")
		{
		alert('กรุณากรอกรหัสสินค้า');
		document.all.ProductCode.focus();
		return false;
		}
	*/	
		if(document.all.ProductName.value =="")
		{
		alert('กรุณากรอกชื่อสินค้า');
		document.all.ProductName.focus();
		return false;
		}
		
		if(document.all.Price.value =="")
		{
		alert('กรุณากรอกราคา');
		document.all.Price.focus();
		return false;
		}						
}
</script>
<form name="frmNew" method="post" action="<?=$_SERVER['PHP_SELF'];?>?ProductID=<?=$_GET["ProductID"];?>&Action=Save" enctype="multipart/form-data" onSubmit="return checkvalue();">
  <table cellspacing=1 cellpadding=3 width="68%" bgcolor=#CCCCCC border=0 align="center" height="10">
    <tbody>
      <tr bgcolor=#e5e5e5> 
        <td width="31" bgcolor="#FFFFFF"> 
          <table width="100%" border="0" cellspacing="2" cellpadding="2">
            <tr>
              <td width="18%" valign="top">หมวดหมู่ <font color="#FF0000">** </font></td>
              <td width="82%"><select name="CatID" class="txtbox">
                <option value="">---- กรุณาเลือก ----</option>
                <?
//===========  แสดงหมวดหมู่
$sql_group = "select * from category order by CatID asc";
$dbquery_group = mysql_query($sql_group);
$num_rows_group = mysql_num_rows($dbquery_group);
while ($result = mysql_fetch_array($dbquery_group))
{
if($_POST["CatID"] == $result["CatID"])
{
$sel="selected";
}
else
{
$sel="";
}
?>
                <option value="<?=$result["CatID"];?>" <?=$sel;?>>
                  <?=$result["CategoryName"];?>
                  </option>
                <?
}
?>
              </select>                &nbsp;</td>
            </tr>
            <tr> 
              <td valign="top">รหัสสินค้า <font color="#FF0000">** </font></td>
              <td><input name="ProductCode" type="text" class="box" id="ProductCode" value="<?=$_POST["ProductCode"];?>" size="21" maxlength="50">              </td>
            </tr>
            <tr> 
              <td valign="top">ชื่อสินค้า <font color="#FF0000">** </font></td>
              <td><input name="ProductName" type="text" class="box" id="ProductName" style="width:300" value="<?=$_POST["ProductName"];?>" size="35" maxlength="100"></td>
            </tr>
            <tr> 
              <td valign="top">ราคา <font color="#FF0000">** </font></td>
              <td><input name="Price" type="text" class="box" id="Price" value="<?=$_POST["Price"];?>" size="21" maxlength="20"></td>
            </tr>
            <tr> 
              <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
              <td><input name="Submit" type="submit" class="button" value="บันทึก"> 
                <input name="Cancel" type="button" class="button" id="Cancel" value="ยกเลิก" onClick="window.location='product.php?CatID=<?=$result["CatID"];?>';"></td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr> 
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
        </table></td>
      </tr>
 
    </tbody>
  </table>
</form>






Tag : PHP, MySQL







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-09-09 08:47:14 By : skulds View : 875 Reply : 4
 

 

No. 1



โพสกระทู้ ( 71 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook

สร้างตัวแปร ProductCode แล้วใช้ Function Random ในการสุ่มตัวเลขครับ

Code (PHP)
$ProductCode = rand(100000, 999999);

$sql_up = "insert into product (CatID,ProductCode,ProductName,Price) Values ('$_POST[CatID]','$ProductCode','$_POST[ProductName]','$_POST[Price]')";


ลองดูครับ







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-09 09:48:19 By : imagineverend
 


 

No. 2



โพสกระทู้ ( 22 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : imagineverend เมื่อวันที่ 2013-09-09 09:48:19
รายละเอียดของการตอบ ::
ทำแล้วครับพอดูในDBที่ส่งค่าเข้าไปมันไม่แรนด้อมครับ ต้องจัดการตัวโค้ดไม่ไห้ส่งค่าเหมือนเดิมไช่ไหมครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-09 10:07:53 By : skulds
 

 

No. 3



โพสกระทู้ ( 310 )
บทความ ( 0 )



สถานะออฟไลน์


Code (PHP)
insert into tb1(productCode) values((SELECT last_insert_id(productID) as lastid FROM `category`  order by lastid desc limit 1  )+1)

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-09 10:20:26 By : survivor
 


 

No. 4



โพสกระทู้ ( 22 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 3 เขียนโดย : survivor เมื่อวันที่ 2013-09-09 10:20:26
รายละเอียดของการตอบ ::
เอาใส่ไว้ตรงนี้ใช่ไหมครับ
Code (PHP)
<?
ob_start();
session_start();
if($_SESSION["adminlogin"]=="")
{
header('location:index.php');
exit();
}
 
require('../connect/connect.php');
						
                       if($_GET["Action"]=="Save")
	                   {
$sql = "SELECT *FROM product WHERE ProductCode='$_POST[ProductCode]'";
$re = mysql_query($sql,$conn);
$resultChk = mysql_fetch_array($re);
$strProductCode="".substr("000$strResultProductCode[ProductCode]", -3);	
$sql_up = "update ProductCode set ProductCode=ProductCode+1 ";						
						if($resultChk)
						{
									echo"<script language='JavaScript'>";
									echo"alert('รหัสสินค้านี้มีอยู่แล้ว');";
									echo"</script>";						
						}
						else
						{

					  $ProductCode = rand(100000, 999999);
					  $sql_up = "insert into tb1(productCode) values((SELECT last_insert_id(productID) as lastid FROM `category`  order by lastid desc limit 1  )+1)";

								  $sql_up = "insert into product (CatID,ProductCode,ProductName,Price) Values ('$_POST[CatID]','$_POST[ProductCode]','$_POST[ProductName]','$_POST[Price]')";
								  $dbquery_up = mysql_query($sql_up);
											 
					  
									echo"<script language='JavaScript'>";
									echo"alert('เพิ่มข้อมูลเรียบร้อยแล้ว');";
									echo"window.location='product_edit.php?ProductID=".mysql_insert_id()."';";
									echo"</script>";
						}														  
			 		  }


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-09 10:36:52 By : skulds
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : (มือใหม่) อยากให้รันรหัสสินค้าแบบอัตโนมัตเข้าDB แต่ไม่ไช้auto_incrementต้องทำยังไงครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่