ไม่รู้ว่าต้องแก้ไขตรงไหน อย่างไร ค่ะ ช่วยด้วยนะค่ะ
Code (PHP)
<form id="form1" name="form1" method="post" action="insert_db_pcu.php">
<table width="674" border="0" align="center">
<tr>
<td height="29"><span class="style65">1. หมายเลขทะเบียนใน PCU</span></td>
<td colspan="3"><span class="style18">
<input name="nopcu" type="text" id="nopcu" maxlength="50" onChange="JavaScript:doCallAjax('nopcu','name_sur','depart','phone','email');" />
</span></td>
</tr>
<tr>
<td width="189" height="34"><span class="style65">2. ชื่อ - นามสกุล </span></td>
<td width="187"><span class="style18">
<input name="name_sur" type="text" id="name_sur" maxlength="100" />
</span></td>
<td width="105"> <span class="style65">สังกัดภาควิชา</span></td>
<td width="175"><span class="style18">
<label></label>
</span> <label><span class="style18">
<input name="depart" type="text" id="depart" maxlength="50" />
<span class="style62"><span class="style63"> </span></span></span></label></td>
</tr>
<tr>
<td height="35"><span class="style65"> เบอร์โทรศัพท์ติดต่อ</span></td>
<td><span class="style18">
<input name="phone" type="text" id="phone" maxlength="100" />
</span></td>
<td> <span class="style65">E-mail </span></td>
<td><label><span class="style18">
<input name="email" type="text" id="email" maxlength="50" />
<span class="style66"> </span></span></label></td>
</tr>
<tr>
<td height="33"><span class="style65">3. สถานภาพ
<label> </label>
</span></td>
<td colspan="3"><span class="style65">
<label>
<input type="radio" name="status" id="radio" value="นักวิจัย" />
</label>
</span>
<label></label>
<label><span class="style53"><span class="style65">นักวิจัย </span></span>
<input type="radio" name="status" id="radio2" value="อาจารย์" />
</label>
<span class="style65">
<label> อาจารย์
<input type="radio" name="status" id="radio3" value="ผู้ช่วยศาสตราจารย์" />
</label>
</span>
<label></label>
<label><span class="style53"><strong><span class="style68">ผศ. </span></strong></span>
<input type="radio" name="status" id="radio4" value="รองศาสตราจารย์" />
<span class="style53"><strong><span class="style68"><span class="style65"><strong> </strong></span><strong>รศ. </strong></span></strong></span>
<input type="radio" name="status" id="radio5" value="ศาตราจารย์" />
<span class="style53"><strong><span class="style68"><strong><strong>ศ.</strong></strong></span></strong></span></label>
<label></label></td>
</tr>
<tr>
<td height="33"><span class="style65">4. ประกอบการขอรับบริการ </span></td>
<td colspan="3"><label><span class="style65"><br />
<input name="chk[]" type="checkbox" id="checkbox3" value="แนะนำวารสาร เพื่อส่งบทความตีพิมพ์" />
แนะนำวารสาร เพื่อส่งบทความตีพิมพ์<br />
<input name="chk[]" type="checkbox" id="checkbox4" value="แก้ไขปรับปรุงต้นฉบับบทความ" />
แก้ไขปรับปรุงต้นฉบับบทความ<br />
</span></label></td>
</tr>
<tr>
<td valign="top"height="14"><span class="style65"><br />
บทความชื่อ</span></td>
<td colspan="3"><br />
<textarea name="journal" id="journal" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<td valign="top"height="36"><p class="style65"> คณะผู้เขียนบทความ</td>
<td colspan="3"><p>
<label></label>
<label> </label>
<label></label>
<label>
<textarea name="group" id="group" cols="45" rows="5"></textarea>
</label>
<br />
</p> <label></label></td>
</tr>
<tr>
<td valign="top" height="31"><span class="style65">
<label></label>
อื่น ๆ </span></td>
<td height="31" colspan="3"><textarea name="other" id="other" cols="45" rows="5"></textarea></td>
<tr>
<td height="30"><span class="style65">5. ประกอบการขอรับบริการ </span></td>
<td colspan="3"><label><span class="style65">ได้ส่ง </span>
<input name="sent[]" type="checkbox" id="send_journal" value="ส่งต้นฉบับบทความ" />
<span class="style65">ต้นฉบับบทความจำนวน 2 ชุด (จำนวน </span>
<input name="no_journal" type="text" id="no_journal" value="0" size="5" maxlength="100" />
<span class="style65">หน้า/บทความ</span> <span class="style65">)</span>
<input name="sent[]" type="checkbox" id="cd" value="ส่ง CD" />
<span class="style65">CD</span> </label></td>
</tr>
<tr>
<td height="30"> </td>
<td colspan="3"> </td>
</tr>
<tr>
<td height="30"> </td>
<td colspan="3"><label>
<input name="submit" type="submit" id="submit" value="ส่งข้อมูล" />
</label></td>
</tr>
</table>
</form>
Code (PHP)
<?php
$host="127.0.0.1";
$username="root";
$password="admin";
$db_name="project";
mysql_connect($host, $username, $password)or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("set character set utf8");
// Get values from form
$nopcu=$_POST['nopcu'];
$name_sur=$_POST['name_sur'];
$depart=$_POST['depart'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$status = $_POST['status'];
if ($chk)
{
$chks = implode($chk, ",");
mysql_query ("INSERT INTO pcu_servive(recieve_service)VALUES('$chks')");
}
//$chk = implode( ',',$_POST['chk[]']);
$journal=$_POST['journal'];
$group=$_POST['group'];
$other=$_POST['other'];
if ($sent)
{
$sents = implode($sent, ",");
mysql_query ("INSERT INTO pcu_servive(sent)VALUES('$sents')");
}
//$sent = implode( ',',$_POST['sent[]']);
$no_jourmal=$_POST['no_journal'];
// Insert data into mysql
$sql="INSERT INTO pcu_service(nopcu, name_sur, depart, phone,email, status,journal,group,other,no_journal)VALUES('$_POST[nopcu]','$_POST[name_sur]','$_POST[depart]', '$_POST[phone]', '$_POST[email]', '$_POST[status]','$_POST[journal]','$_POST[group]','$_POST[other]','$_POST[no_journal]')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful"
if($result){
echo "<h2>ใส่ข้อมูลลงฐานข้อมูลเรียบร้อยแล้ว</h2>";
echo "<BR>";
echo "<h1><a href='home.php'>กลับสู่หน้าหลัก</a></h1>";
}
else {
echo "<h2>ข้อมูลไม่ได้ถูกใส่ลงในฐานข้อมูล</h2>";
}
// close connection
mysql_close();
?>
checkbox และ radio button เขียนอย่างในโค้ดนี้ถูกหรือเปล่าค่ะ ที่จะต้องมี [] ด้วย
แต่ยังทำให้มัน insert เข้าฐานข้อมูลไม่ได้เลยค่ะ ลองอ่านตัวอย่างที่ทุกคนโพสตอบแล้วที่เป็นเลือกสี แต่ยังไม่เข้าใจเลยค่ะ ช่วยอธิบายด้วยนะค่ะ
ขอบคุณมากค่ะTag : - - - -
Date :
2009-12-03 23:13:42
By :
elfying
View :
1149
Reply :
7
ค่ะ ทั้ง 4,5 สามารถเลือกได้ทั้ง 2 อัน หรือ อันเดียวก็ได้ค่ะ
แล้วเอาข้อมูลลงฐานข้อมูลต้องแก้ยังไงหรอค่ะ
Date :
2009-12-04 00:54:36
By :
elfying
Quote: ค่ะ ทั้ง 4,5 สามารถเลือกได้ทั้ง 2 อัน หรือ อันเดียวก็ได้ค่ะ
งั้นก้ไม่ต้องแก้หน้าฟอร์ม ครับ
Quote: แล้วเอาข้อมูลลงฐานข้อมูลต้องแก้ยังไงหรอค่ะ
ตามนี้ครับ ผมไม่รู้ว่าชื่อฟิลด์ในตาราง ชื่ออะไรบ้างก็เดาไปตามชื่อตัวแปร ปรับแก้เอานะครับ
Code (PHP)
$host="127.0.0.1";
$username="root";
$password="admin";
$db_name="project";
mysql_connect($host, $username, $password)or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
mysql_query("set character set utf8");
// Get values from form
$nopcu = $_POST['nopcu'];
$name_sur = $_POST['name_sur'];
$depart = $_POST['depart'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$status = $_POST['status'];
$chk = $_POST['chk'];
$journal = $_POST['journal'];
$group = $_POST['group'];
$other = $_POST['other'];
$no_journal = $_POST['no_journal'];
// เช็คว่ามีการส่งข้อมูล 4. ประกอบการขอรับบริการ มาหรือไม่
$chks = "";
if(count($chk) > 0 )
{
$chks = implode(",",$chk);
}
// เช็คว่ามีการส่งข้อมูล 5. ประกอบการขอรับบริการ มาหรือไม่
$sents = "";
if(count($sent) > 0 )
{
$sents = implode(",",$sent);
}
$sql ="INSERT INTO pcu_servive (nopcu, name_sur, depart, phone, email, status, chk, journal, group, other, no_journal, sent) VALUES ('$nopcu', '$name_sur', '$depart', '$phone', '$email', '$status', '$chks', '$journal', '$group', '$other', '$no_journal', '$sent' ) ";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful"
if($result){
echo "<h2>ใส่ข้อมูลลงฐานข้อมูลเรียบร้อยแล้ว</h2>";
echo "<BR>";
echo "<h1><a href='home.php'>กลับสู่หน้าหลัก</a></h1>";
}
else {
echo "<h2>ข้อมูลไม่ได้ถูกใส่ลงในฐานข้อมูล</h2>";
}
// close connection
mysql_close();
Date :
2009-12-04 13:59:52
By :
lozomac
อยากถามอะค่ะ ว่าฐานข้อมูลต้องสร้างฟิลด์กี่หรอค่ะ แบบว่าตัวเลือกของ checkbox มี 3 ก็ต้องสร้างไว้ 3 ฟิลด์หรือเปล่าค่ะ หรือว่าฟิลด์เดียวก็พออะค่ะ
แล้วมันขึ้นว่าข้อมูลไม่ได้ถูกใส่ลงฐานข้อมูลตลอดเลยอะค่ะ ไม่รู้ว่าต้องแก้ตรงไหนค่ะ
Database Error: INSERT INTO pcu_service (nopcu, name_sur, depart, phone, email, status, chk, journal, group, other,sent,no_journal) VALUES ('1234', 'หมูหยอง ขนฟู', 'คณิตศาสตร์', '02222222', '[email protected] ', 'นักวิจัย', 'แนะนำวารสาร เพื่อส่งบทความตีพิมพ์,แก้ไขปรับปรุงต้นฉบับบทความ', '', '', '','','0') You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group, other,sent,no_journal) VALUES ('1234', 'หมูหยอง ขนฟ��' at line 1
มันขึ้น error ประมาณนี้อะค่ะ
ขอบคุณสำหรับคำตอบนะค่ะ
Date :
2009-12-04 22:38:53
By :
elfying
group เป็น keyword ของ sql มันเลยด่าเอา
แบบว่ามันมองเป็นคำสั่ง group by มันไม่เห็น by มันเลยด่าเอา
ถ้าจะใช้ให้ใส่ `group` นะพี่น้อง
Date :
2009-12-05 02:19:03
By :
plakrim
ขอบคุณมากค่ะทุกท่าน ได้แล้วค่ะ ดีใจมากๆๆๆ ค่ะ
Date :
2009-12-05 12:09:40
By :
elfying
Load balance : Server 00