|
 |
|
ช่วยดูหน่อยครับ จะเขียนโค้ด บันทึก ไอพี กับ วันที่ ลง ดาต้าเบส ยังงัยครับ.... |
|
 |
|
|
 |
 |
|
อันนี้ผมเอาของไทยครีเอท มาแก้อ่ะครับ
แล้วถ้าผมจะให้บันทึก วัน เดือน ปี แล้วก็ ไอพี ด้วย จะต้องเพิ่มส่วนไหนบ้างครับ ผมลองแล้วยังไม่ได้ รบกวนคนเก่งๆช่วยหน่อยครับ.
<html>
<head>
<title>แบบประเมิน</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("db_assess");
$strSQL = "INSERT INTO tb_assess ";
$strSQL .="(fname,lname,ward,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtfname"]."','".$_POST["txtlname"]."','".$_POST["txtward"]."','".$_POST["txtq1"]."',
'".$_POST["txtq2"]."','".$_POST["txtq3"]."','".$_POST["txtq4"]."','".$_POST["txtq5"]."','".$_POST["txtq6"]."','".$_POST["txtq7"]."' ";
$strSQL .=",'".$_POST["txtq8"]."','".$_POST["txtq9"]."','".$_POST["txtq10"]."') ";
$objQuery = mysql_query($strSQL);
if($objQuery)
{
echo "Save Done.";
}
else
{
echo "Error Save [".$strSQL."]";
}
mysql_close($objConnect);
?>
</body>
</html>
Tag : PHP, MySQL

|
|
 |
 |
 |
 |
Date :
2012-02-19 12:37:26 |
By :
Beer_7 |
View :
1030 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมเพิ่มโค้ดลงไป save ได้ปกติ แต่ทำไม่ค่า ไอพี กับ วันที่ ไม่ลง database ครับ....
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
$ip = $_SERVER['REMOTE_ADDR']; <------------------- เพิ่ม
$now = date("Y-m-d H:i:s");
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("db_assess");
$strSQL = "INSERT INTO tb_assess ";
$strSQL .="(fname,lname,ward,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtfname"]."','".$_POST["txtlname"]."','".$_POST["txtward"]."','".$_POST["txtq1"]."',
'".$_POST["txtq2"]."','".$_POST["txtq3"]."','".$_POST["txtq4"]."','".$_POST["txtq5"]."','".$_POST["txtq6"]."','".$_POST["txtq7"]."' ";
$strSQL .=",'".$_POST["txtq8"]."','".$_POST["txtq9"]."','".$_POST["txtq10"]."') ";
$sql="insert into tb_assess values('','$ip','$now')"; <---------------------เพิ่ม
[font=Verdana][/font]
$objQuery = mysql_query($strSQL);
if($objQuery)
{
echo "Save Done.";
}
else
{
echo "Error Save [".$strSQL."]";
}
mysql_close($objConnect);
?>
</body>
</html>
|
 |
 |
 |
 |
Date :
2012-02-19 14:16:10 |
By :
Beer_7 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำความเข้าใจใหม่ในเรื่อง ฐานข้อมูลและ การ insert ข้อมูลลง ฐานข้อมูลก่อนนะครับ
https://www.thaicreate.com/tutorial/sql-insert.html
ของเดิม
Code (PHP)
$strSQL = "INSERT INTO tb_assess ";
$strSQL .="(fname,lname,ward,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtfname"]."','".$_POST["txtlname"]."','".$_POST["txtward"]."','".$_POST["txtq1"]."',
'".$_POST["txtq2"]."','".$_POST["txtq3"]."','".$_POST["txtq4"]."','".$_POST["txtq5"]."','".$_POST["txtq6"]."','".$_POST["txtq7"]."' ";
$strSQL .=",'".$_POST["txtq8"]."','".$_POST["txtq9"]."','".$_POST["txtq10"]."') ";
ในเมื่อต้องการจะเพิ่ม field ที่จะ insert ลงไปก็ต้อง เพิ่มเงื่อนไขครับ
ไม่ใช่ไปสร้างเป็นคำสั่งใหม่แบบนั้น ไม่งั้น โปรแกรมจะมองว่าเป็นการ เพิ่มแถวใหม่อีกแถว
Quote:$sql="insert into tb_assess values('','$ip','$now')";
แบบนี้คือผิดครับ
Code (PHP)
$strSQL = "INSERT INTO tb_assess ";
$strSQL .="(fname,lname,ward,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtfname"]."','".$_POST["txtlname"]."','".$_POST["txtward"]."','".$_POST["txtq1"]."',
'".$_POST["txtq2"]."','".$_POST["txtq3"]."','".$_POST["txtq4"]."','".$_POST["txtq5"]."','".$_POST["txtq6"]."','".$_POST["txtq7"]."' ";
$strSQL .=",'".$_POST["txtq8"]."','".$_POST["txtq9"]."','".$_POST["txtq10"]."') ";
$sql="insert into tb_assess values('','$ip','$now')"; //<---------------------เพิ่ม
$objQuery = mysql_query($strSQL);
แล้วอีกอย่าง การที่เขียนคำสั่ง SQL ไว้ที่ตัวแปร $strSQL นั้น
การจะ query คำสั่งที่เขียนนั้น ทำโดยผ่าน function mysql_query();
Quote:$objQuery = mysql_query($strSQL);
แต่ส่วนที่ เพิ่ม มานั้นเขียนคำสั่งไว้ที่ตัวแปร $sql แต่ไม่มีการสั่ง query ต่อให้เขียน sql ถูกยังไงไม่มีการสั่ง query ก็ไม่มีอะไรเกิดขึ้นครับ
อันนี้คืออันที่ถูกครับ
Quote:$strSQL = "INSERT INTO tb_assess ";
$strSQL .="(fname,lname,ward,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,ip,date) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtfname"]."','".$_POST["txtlname"]."','".$_POST["txtward"]."','".$_POST["txtq1"]."',
'".$_POST["txtq2"]."','".$_POST["txtq3"]."','".$_POST["txtq4"]."','".$_POST["txtq5"]."','".$_POST["txtq6"]."','".$_POST["txtq7"]."' ";
$strSQL .=",'".$_POST["txtq8"]."','".$_POST["txtq9"]."','".$_POST["txtq10"]."','".$ip."','".$now."') ";
$objQuery = mysql_query($strSQL);
|
 |
 |
 |
 |
Date :
2012-02-19 14:30:47 |
By :
Likito |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากเลยนะครับ....เดี๋ยวผมจะไปลองศึกษาดูครับ...พอดีเพิ่งหัดเขียน php ครับ 
|
 |
 |
 |
 |
Date :
2012-02-20 10:14:34 |
By :
Beer_7 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|