 |
มีปัญหาการใช้ if-else ครับ ข้อมูลออกมาไม่ถูกต้องตามที่ควรจะเป็น |
|
 |
|
|
 |
 |
|
มีปัญหาการใช้ if-else ครับ ข้อมูลออกมาไม่ถูกต้องตามที่ควรจะเป็น
* ตรงที่ * ไว้ใน code ตรง if ผมใส่ or $select_another=honda ข้อมูบอื่นๆ ก็กลับเป็น F หมดทั้งแผง array เลยครับ ทำไมไม่เป็นเฉพาะที่เป็นค่าว่าง และข้อมูลที่เป็น honda
Code (PHP)
$strSQL = "SELECT * FROM another";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
if(is_array($_POST['select_another']) and count($_POST['select_another'])>0)
{
$strSQL ="INSERT INTO another ";
$strSQL .="(another_name,busi_no,bg_no,ser_no,Created,CreatedBy,CreatedTime,Enable)";
$strSQL .="VALUES ";
$values = array();
foreach($_POST['select_another'] as $select_another)
{
if ((trim($select_another) == null ) or ($select_another="honda")) //<---ดูตรงนี้ครับ ผมใส่ or $select_another=honda *
{
$sum = "F";
}
else
{
$sum = "T";
}
array_push($values, "('".$select_another."','".$_POST["select_busi"]."','".$_POST["select_bg"]."','".$_POST["select_service"]."',
'".$_POST["text_user_create"]."','".$_POST["text_date_create"]."','".$_POST["text_time_create"]."','".$sum."')");
}
$strSQL .= implode(", ", $values);
}
$objQuery = mssql_query($strSQL);
Tag : PHP
|
|
 |
 |
 |
 |
Date :
2011-09-04 23:26:15 |
By :
Nos1001 |
View :
1201 |
Reply :
18 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ and (&&) แทน or ครับ
|
 |
 |
 |
 |
Date :
2011-09-05 01:38:50 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าใช้อันนี้ && ข้อมูลมันนอกมาเป็น
honda F
honda F
honda T
toyota T
ซึึ่งความจิงมันต้องออกเป็น
ค่าว่าง F
ค่าว่าง F
honda F
toyota T เพราะ toyota ไม่เคยมีข้อมูลใน db หรือถ้ามีข้อมูลซ้ำไม่ให้มัน add เข้าไปได้ไหมครับ เก็บเฉพาะที่ไม่ซ้ำฟ
|
 |
 |
 |
 |
Date :
2011-09-05 09:11:29 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมใส่ if ถ้าค่าว่าง หรือ ถ้าข้อมูลเท่ากับชื่อในฐานข้อมูลจะให้ออกเป็น F แต่ทำไม ออกเป็น T หมด ยกเว้นค่าว่างครับ
Code (PHP)
$strSQL = "SELECT * FROM another";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult = mssql_fetch_array($objQuery);
$count1=count($_POST[select_another]);
if(is_array($_POST[select_another]) and ($count1)>0)
{
$strSQL ="INSERT INTO another ";
$strSQL .="(another_name,busi_no,bg_no,ser_no,Created,CreatedBy,CreatedTime,Enable)";
$strSQL .="VALUES ";
$values = array();
foreach($_POST['select_another'] as $select_another)
{
if ((trim($select_another) == null ) or ($select_another==$objResult[another_name])) // ตรงนี้ครับ
{
$sum = "F";
}
else
{
$sum = "T";
}
echo $select_another." ".$sum."<br>";
array_push($values, "('".$select_another."','".$_POST["select_busi"]."','".$_POST["select_bg"]."','".$_POST["select_service"]."','".$_POST["text_user_create"]."','".$_POST["text_date_create"]."','".$_POST["text_time_create"]."','".$sum."')");
}
$strSQL .= implode(", ", $values);
}
$objQuery = mssql_query($strSQL);
|
 |
 |
 |
 |
Date :
2011-09-05 11:07:07 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขุดๆหน่อยครับ ช่วยที
|
 |
 |
 |
 |
Date :
2011-09-05 14:50:40 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
if (empty(trim($select_another)) or (trim($select_another)==trim($objResult[another_name])))
|
 |
 |
 |
 |
Date :
2011-09-05 15:05:25 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ERROR ครับ
Fatal error: Can't use function return value in write context in C:\AppServ\www\CC\detail_add.php on line 53
|
 |
 |
 |
 |
Date :
2011-09-05 15:28:47 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Code (PHP)
$select_another = trim($select_another);
$another_name = trim($objResult[another_name]);
if (empty($select_another) or $select_another==$another_name)
|
 |
 |
 |
 |
Date :
2011-09-05 15:52:00 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่ครับ ข้อมูลมันเข้ายังไม่ถูกอะครับ ไม่ติด error ละครับ
เดิมข้อมูลในฐานมี
dpu
spu
แล้วก็เพิ่ม bu เข้าไป ฉะนั้นผลลัพธ์ที่เป็น T จะต้อง bu ส่วนข้อมูลที่เคยมีอยู่แล้ว dpu และ spu ที่เพิ่มเข้ามาจะเป็น F ครับ
แต่ผลของพี่มันเข้า
dpu F
spu T
bu T
ซึ่งควรเป็น
dpu F
spu F
bu T
|
 |
 |
 |
 |
Date :
2011-09-05 16:04:28 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือหมายความถ้าเว้นว่าง หรือ มีข้อมูลซ้ำใน DB ก็ให้เป็น F ครับ
|
 |
 |
 |
 |
Date :
2011-09-05 18:56:46 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลอง echo $objResult['another_name'] ออกมาแล้วนะครับ มันออกแต่ dpu อย่างเดียวเลยครับ ค่าซ้ำมันก็เลย dpu เป็น F อย่างเดียวครับ ซึ่งที่จริงมันไม่ได้มีค่า dpu อย่างเดียวครับ หาทางแก้ไม่ได้เลยครับ!!!
|
 |
 |
 |
 |
Date :
2011-09-05 20:45:20 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยหน่อยครับ
|
 |
 |
 |
 |
Date :
2011-09-05 21:35:45 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอผมใส่
while($objResult = mssql_fetch_array($objQuery))
{
.
..
...
}
มันก็ออกแต่ dpu เหมือนเดิมครับ
ผลรัน echo $select_another." ".$sum." ".$objResult['another_name']."<br>";
dpu F dpu
spu T dpu
bu T dpu
|
 |
 |
 |
 |
Date :
2011-09-06 07:33:14 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near 'spu'. (severity 15) in C:\AppServ\www\CC\detail_add.php on line ล่างสุด
Warning: mssql_query() [function.mssql-query]: Query failed in C:\AppServ\www\CC\detail_add.php on line ล่างสุด
Code (PHP)
$strSQL = "SELECT * FROM another";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult = mssql_fetch_array($objQuery);
$count1=count($_POST[select_another]);
if(is_array($_POST[select_another]) and ($count1)>0)
{
$strSQL ="INSERT INTO another ";
$strSQL .="(another_name,busi_no,bg_no,ser_no,Created,CreatedBy,CreatedTime,Enable)";
$strSQL .="VALUES ";
$values = array();
foreach($_POST['select_another'] as $select_another)
{
$strSQL = "SELECT * FROM another";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($objResult = mssql_fetch_array($objQuery))
{
$select_another = trim($select_another);
$another_name = trim($objResult[another_name]);
if (empty($select_another) or $select_another==$another_name)
{
$sum = "F";
}
else
{
$sum = "T";
}
echo $select_another." ".$sum." ".$objResult['another_name']."<br>";
}
array_push($values, "('".$select_another."','".$_POST["select_busi"]."','".$_POST["select_bg"]."','".$_POST["select_service"]."','".$_POST["text_user_create"]."','".$_POST["text_date_create"]."','".$_POST["text_time_create"]."','".$sum."')");
}
$strSQL .= implode(", ", $values);
}
$objQuery = mssql_query($strSQL);
|
 |
 |
 |
 |
Date :
2011-09-06 09:40:18 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
echo $strSQL ออกมาดูแล้วจะเห็นเองครับ แล้วเอาไปคิวรี่ดูใน phpmyadmin ก็ได้
|
 |
 |
 |
 |
Date :
2011-09-06 11:02:11 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พี่กริมครับตอนนี้ไม่ติด Error แล้วครับ ผลออกถูกต้อง แต่เก็บไม่ถูกต้อง เหมือนมันเก็บตัวสุดท้ายเลยอะครับ
Code (PHP)
$strSQL = "SELECT * FROM another";
$objQuery = mssql_query($strSQL) or die ("Error Query [".$strSQL."]");
$objResult = mssql_fetch_array($objQuery);
echo $strSQL."<br>";
$count1=count($_POST[select_another]);
if(is_array($_POST[select_another]) and ($count1)>0)
{
$strSQL ="INSERT INTO another ";
$strSQL .="(another_name,busi_no,bg_no,ser_no,Created,CreatedBy,CreatedTime,Enable)";
$strSQL .="VALUES ";
echo $strSQL."<br>";
$values = array();
foreach($_POST['select_another'] as $select_another)
{
$strSQL1 = "SELECT * FROM another";
$objQuery1 = mssql_query($strSQL1) or die ("Error Query [".$strSQL1."]");
echo $strSQL1."<br>";
while($objResult1 = mssql_fetch_array($objQuery1))
{
$select_another = trim($select_another);
$another_name = trim($objResult1[another_name]);
if (empty($select_another) or $select_another==$another_name)
{
$sum = "F";
}
else
{
$sum = "T";
}
echo $select_another." ".$sum." ".$objResult1['another_name']."<br>";
}
array_push($values, "('".$select_another."','".$_POST["select_busi"]."','".$_POST["select_bg"]."','".$_POST["select_service"]."',
'".$_POST["text_user_create"]."','".$_POST["text_date_create"]."','".$_POST["text_time_create"]."','".$sum."')");
}
$strSQL .= implode(", ", $values);
}
$objQuery = mssql_query($strSQL);
ลอง echo stetment มาแล้วครับ ผลที่ได้คือ
SELECT * FROM another
INSERT INTO another (another_name,busi_no,bg_no,ser_no,Created,CreatedBy,CreatedTime,Enable)VALUES
SELECT * FROM another
dpu F dpu // ชื่อซ้ำกับข้อมูลใน db เป็น F ถูกแล้วครับ แต่เก็บ T เข้าไป เหมือนมันเก็บตัวหลังสุด
dpu T spu
dpu T sss
dpu T ppp
dpu T ooo // มันเอา T ตัวนี้ไปเก็บครับ
SELECT * FROM another
spu T dpu
spu F spu // ชื่อซ้ำกับข้อมูลใน db เป็น F ถูกแล้วครับ แต่เก็บ T เข้าไป เหมือนมันเก็บตัวหลังสุด
spu T sss
spu T ppp
spu T ooo // มันเอา T ตัวนี้ไปเก็บครับ
SELECT * FROM another
bu T dpu
bu T spu
bu T sss
bu T ppp
bu T ooo // มันเอา T ตัวนี้ไปเก็บครับ
|
 |
 |
 |
 |
Date :
2011-09-06 11:42:57 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยหน่อยครับ เหลือสุดท้ายแล้วฮ๊าฟฟฟ
|
 |
 |
 |
 |
Date :
2011-09-06 13:07:41 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอขุดหน่อยครับ ช่วยที
|
 |
 |
 |
 |
Date :
2011-09-06 15:43:03 |
By :
Nos1001 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|