function DetectEnterPressed(e) {
var characterCode
if(e && e.which){ // NN4 specific code
e = e
characterCode = e.which
}
else {
e = event
characterCode = e.keyCode // IE specific code
}
if (characterCode == 13) return true // Enter key is 13
else return false
}
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
include("connectDB.php");
//**** New class database ****//
$strHost = "localhost";
$strDB = "smartek";
$strUser = "root";
$strPassword = "laekarat";
$clsMyDB = new MyDatabase($strHost,$strDB,$strUser,$strPassword);
//**** Call to class function insert record ****//
$clsMyDB->strTable = "serials_cabinet";
$clsMyDB->strField = "sequence,serials";
foreach($_POST["barcode"] as $serial){
$clsMyDB->strValue = " 'XXX','$serial'";
$objInsert = $clsMyDB->fncInsertRecord();
if(!$objInsert)
{
echo "$serial :: Record already exist.<br>";
}
else
{
echo "$serial :: Record inserted.<br>";
}
}
?>
</body>
</html>
ส่วนไฟล์ connectDB.php นั้นคือ
Code (PHP)
<?
/**** Class Database ****/
Class MyDatabase
{
/**** function connect to database ****/
function MyDatabase($strHost,$strDB,$strUser,$strPassword)
{
$this->objConnect = mysql_connect($strHost,$strUser,$strPassword);
$this->DB = mysql_select_db($strDB);
}
/**** function insert record ****/
function fncInsertRecord()
{
$strSQL = "INSERT INTO $this->strTable ($this->strField) VALUES ($this->strValue) ";
return mysql_query($strSQL);
}
/**** function select record ****/
function fncSelectRecord()
{
$strSQL = "SELECT * FROM $this->strTable WHERE $this->strCondition ";
$objQuery = @mysql_query($strSQL);
return @mysql_fetch_array($objQuery);
}
/**** function update record (argument) ****/
function fncUpdateRecord($strTable,$strCommand,$strCondition)
{
$strSQL = "UPDATE $strTable SET $strCommand WHERE $strCondition ";
return @mysql_query($strSQL);
}
/**** function delete record ****/
function fncDeleteRecord()
{
$strSQL = "DELETE FROM $this->strTable WHERE $this->strCondition ";
return @mysql_query($strSQL);
}
/*** end class auto disconnect ***/
function __destruct() {
return @mysql_close($this->objConnect);
}
}
?>
alter table ชื่อตาราง drop primary key,modify sequence int unsigned not null auto_increment primary key
และต้องแก้ไขโค้ด DB ใหม่
<?
/**** Class Database ****/
Class MyDatabase
{
/**** function connect to database ****/
function MyDatabase($strHost,$strDB,$strUser,$strPassword)
{
$this->objConnect = mysql_connect($strHost,$strUser,$strPassword);
$this->DB = mysql_select_db($strDB);
}
/**** function insert record ****/
function fncInsertRecord()
{
$strSQL = "INSERT INTO $this->strTable ($this->strField) VALUES ($this->strValue) ";
return mysql_query($strSQL) or die(mysql_error());
}
/**** function select record ****/
function fncSelectRecord()
{
$strSQL = "SELECT * FROM $this->strTable WHERE $this->strCondition ";
$objQuery = mysql_query($strSQL) or die(mysql_error());
return @mysql_fetch_array($objQuery);
}
/**** function update record (argument) ****/
function fncUpdateRecord($strTable,$strCommand,$strCondition)
{
$strSQL = "UPDATE $strTable SET $strCommand WHERE $strCondition ";
return mysql_query($strSQL) or die(mysql_error());
}
/**** function delete record ****/
function fncDeleteRecord()
{
$strSQL = "DELETE FROM $this->strTable WHERE $this->strCondition ";
return mysql_query($strSQL) or die(mysql_error());
}
/*** end class auto disconnect ***/
function __destruct() {
return mysql_close($this->objConnect);
}
}
?>
ต้องเพิ่ม or die(mysql_error()) เข้าไปครับ
ถ้าเปลี่ยนได้ กรุณาไปใช้ class DB ตัวอื่นดีกว่า ผิดหลักการเขียน OOP ทั้งหมดเลยครับ
<?
/**** Class Database ****/
Class MyDatabase
{
public $strField;
public $strValue;
public $strTable;
public $strCondition;
/**** function connect to database ****/
function __construct($strHost,$strDB,$strUser,$strPassword)
{
$this->objConnect = mysql_connect($strHost,$strUser,$strPassword);
$this->DB = mysql_select_db($strDB);
$this->strField = "";
$this->strValue = "";
$this->strTable = "";
$this->strCondition = "";
}
/**** function insert record ****/
function fncInsertRecord()
{
$strSQL = "INSERT INTO $this->strTable($this->strField) VALUES($this->strValue) ";
echo $strSQL;
$objQuery = mysql_query($strSQL) or die(mysql_error());
return mysql_affected_rows($objQuery);
}
/**** function select record ****/
function fncSelectRecord()
{
$strSQL = "SELECT * FROM $this->strTable WHERE $this->strCondition ";
$objQuery = mysql_query($strSQL) or die(mysql_error());
return mysql_fetch_array($objQuery);
}
/**** function update record (argument) ****/
function fncUpdateRecord($strTable,$strCommand,$strCondition)
{
$strSQL = "UPDATE $strTable SET $strCommand WHERE $strCondition ";
$objQuery = mysql_query($strSQL) or die(mysql_error());
return mysql_affected_rows($objQuery);
}
/**** function delete record ****/
function fncDeleteRecord()
{
$strSQL = "DELETE FROM $this->strTable WHERE $this->strCondition ";
$objQuery = mysql_query($strSQL) or die(mysql_error());
return mysql_affected_rows($objQuery);
}
/*** end class auto disconnect ***/
function __destruct() {
return @mysql_close($this->objConnect );
}
}
?>
Array ( [barcode] => Array ( [0] => 38DB9611286AD1ENH2H0064 ) ) INSERT INTO serials_cabinet(serials) VALUES( '38DB9611286AD1ENH2H0064' )
Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in D:\AppServ\www\barcode\test\connectDB.php on line 27
38DB9611286AD1ENH2H0064 :: Record already exist.