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,038

HOME > PHP > PHP Forum > ผมต้องการ Update ข้อมูลพร้อมกัน 2 Table ภายใน Form เดียวกัน


[PHP] ผมต้องการ Update ข้อมูลพร้อมกัน 2 Table ภายใน Form เดียวกัน

 
Topic : 076899



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



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



ตอนนี้ผมจะทำการอัพเดจข้อมูลครับ

โดยเป็นข้อมูล จำนวน 2 เทบิล

เทเบิลที่ 1 (ตาราง : book)

book_isbn
book_id
book_name
book_author
book_booktype
book_press
book_page
book_timeprint

เทบิลที่ 2 (ตาราง : book_number)

id
book_isbn
book_number_number
book_copy

โดยมี

ิbook.book_isbn = book_number.book_isbn
book.book_isbn PK (Auto)
book_number.id = PK (Auto)


แต่ในตอนนี้เวลาผมอัพเดจ
มันไม่ทำงานครับ
ผมทำการแยกเป็น 2 เรคคอดเซ็ต

โดยส่งค่าเป็น

book_id=xx&id=xx


Code (PHP)
001.<?php require_once('Connections/library.php'); ?>
002.<?php
003.if (!function_exists("GetSQLValueString")) {
004.function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
005.{
006.  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
007. 
008.  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
009. 
010.  switch ($theType) {
011.    case "text":
012.      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
013.      break;   
014.    case "long":
015.    case "int":
016.      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
017.      break;
018.    case "double":
019.      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
020.      break;
021.    case "date":
022.      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
023.      break;
024.    case "defined":
025.      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
026.      break;
027.  }
028.  return $theValue;
029.}
030.}
031. 
032.$editFormAction = $_SERVER['PHP_SELF'];
033.if (isset($_SERVER['QUERY_STRING'])) {
034.  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
035.}
036. 
037.if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
038.  $updateSQL = sprintf("UPDATE book SET book_id=%s, book_name=%s, book_author=%s, book_booktype=%s, book_press=%s, book_page=%s, book_timeprint=%s WHERE book_isbn=%s",
039.                       GetSQLValueString($_POST['hiddenField'], "int"),
040.                       GetSQLValueString($_POST['book_name'], "text"),
041.                       GetSQLValueString($_POST['book_author'], "text"),
042.                       GetSQLValueString($_POST['book_booktype'], "int"),
043.                       GetSQLValueString($_POST['book_press'], "text"),
044.                       GetSQLValueString($_POST['book_page'], "text"),
045.                       GetSQLValueString($_POST['book_timeprint'], "text"),
046.                       GetSQLValueString($_POST['txtUsername'], "text"));
047. 
048.  mysql_select_db($database_library, $library);
049.  $Result1 = mysql_query($updateSQL, $library) or die(mysql_error());
050.}
051. 
052.if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
053.  $updateSQL = sprintf("UPDATE book_number SET book_isbn=%s, book_number_number=%s, book_copy=%s WHERE id=%s",
054.                       GetSQLValueString($_POST['txtUsername'], "text"),
055.                       GetSQLValueString($_POST['book_number_number'], "text"),
056.                       GetSQLValueString($_POST['book_coppy'], "text"),
057.                       GetSQLValueString($_POST['hiddenField2'], "int"));
058. 
059.  mysql_select_db($database_library, $library);
060.  $Result1 = mysql_query($updateSQL, $library) or die(mysql_error());
061.}
062. 
063.mysql_select_db($database_library, $library);
064.$query_Recordset1 = "SELECT * FROM booktype";
065.$Recordset1 = mysql_query($query_Recordset1, $library) or die(mysql_error());
066.$row_Recordset1 = mysql_fetch_assoc($Recordset1);
067.$totalRows_Recordset1 = mysql_num_rows($Recordset1);
068. 
069.$colname_Recordset2 = "-1";
070.if (isset($_GET['book_id'])) {
071.  $colname_Recordset2 = $_GET['book_id'];
072.}
073.mysql_select_db($database_library, $library);
074.$query_Recordset2 = sprintf("SELECT * FROM book WHERE book_id = %s", GetSQLValueString($colname_Recordset2, "int"));
075.$Recordset2 = mysql_query($query_Recordset2, $library) or die(mysql_error());
076.$row_Recordset2 = mysql_fetch_assoc($Recordset2);
077.$totalRows_Recordset2 = mysql_num_rows($Recordset2);
078. 
079.$colname_Recordset3 = "-1";
080.if (isset($_GET['id'])) {
081.  $colname_Recordset3 = $_GET['id'];
082.}
083.mysql_select_db($database_library, $library);
084.$query_Recordset3 = sprintf("SELECT * FROM book_number WHERE id = %s", GetSQLValueString($colname_Recordset3, "int"));
085.$Recordset3 = mysql_query($query_Recordset3, $library) or die(mysql_error());
086.$row_Recordset3 = mysql_fetch_assoc($Recordset3);
087.$totalRows_Recordset3 = mysql_num_rows($Recordset3);
088. 
089.mysql_select_db($database_library, $library);
090.$query_Recordset4 = "SELECT * FROM press";
091.$Recordset4 = mysql_query($query_Recordset4, $library) or die(mysql_error());
092.$row_Recordset4 = mysql_fetch_assoc($Recordset4);
093.$totalRows_Recordset4 = mysql_num_rows($Recordset4);
094.?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
095.<html xmlns="http://www.w3.org/1999/xhtml">
096.<head>
097.<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
098.<title>Untitled Document</title>
099.<script type="text/javascript">
100.<!--
101.function MM_goToURL() { //v3.0
102.  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
103.  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
104.}
105.//-->
106.</script>
107.</head>
108. 
109.<body>
110.<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
111.  <table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
112.    <tr>
113.      <td height="47" colspan="3"><div align="center" class="style14">แก้ไขข้อมูลหนังสือ</div></td>
114.    </tr>
115.    <tr>
116.      <td width="35%" class="style13">ISBN</td>
117.      <td width="59%"><label>
118.        <input name="txtUsername" type="text" id="txtUsername"  onchange="JavaScript:doCallAjax2('txtUsername','book_name','book_number_number','book_coppy','book_author','book_page','book_timeprint','book_type','book_press');" onkeypress="return chkNumber(this)" value="<?php echo $row_Recordset2['book_isbn']; ?>" />
119.      </label></td>
120.      <td width="6%">&nbsp;</td>
121.    </tr>
122.    <tr>
123.      <td class="style13" >ชื่อหนังสือ</td>
124.      <td><label>
125.        <input name="book_name" type="text" id="book_name" value="<?php echo $row_Recordset2['book_name']; ?>" />
126.      </label></td>
127.      <td>&nbsp;</td>
128.    </tr>
129.    <tr>
130.      <td class="style13" >เลขทะเบียน</td>
131.      <td><label>
132.        <input name="book_number_number" type="text" id="book_number_number" onblur="JavaScript:doCallAjax();" value="<?php echo $row_Recordset3['book_number_number']; ?>" />
133.        <span id="mySpan"></span> </label></td>
134.      <td>&nbsp;</td>
135.    </tr>
136.    <tr>
137.      <td class="style13" >ฉบับ</td>
138.      <td><input name="book_coppy" type="text" id="book_coppy" value="<?php echo $row_Recordset3['book_copy']; ?>" /></td>
139.      <td>&nbsp;</td>
140.    </tr>
141.    <tr>
142.      <td class="style13" >ชื่อผู้แต่ง</td>
143.      <td><label>
144.        <input name="book_author" type="text" id="book_author" value="<?php echo $row_Recordset2['book_author']; ?>" />
145.      </label></td>
146.      <td>&nbsp;</td>
147.    </tr>
148.    <tr>
149.      <td class="style13" >จำนวนหน้า</td>
150.      <td><label>
151.        <input name="book_page" type="text" id="book_page" onkeypress="return chkNumber(this)" value="<?php echo $row_Recordset2['book_page']; ?>"/>
152.      </label></td>
153.      <td>&nbsp;</td>
154.    </tr>
155.    <tr>
156.      <td class="style13" >ครั้งที่พิมพ์</td>
157.      <td><label>
158.        <input name="book_timeprint" type="text" id="book_timeprint" onkeypress="return chkNumber(this)" value="<?php echo $row_Recordset2['book_timeprint']; ?>" />
159.      </label></td>
160.      <td>&nbsp;</td>
161.    </tr>
162.    <tr>
163.      <td class="style13" >ประเภทหนังสือ</td>
164.      <td><label>
165.        <select name="book_booktype" id="book_booktype" title="<?php echo $row_Recordset3['book_booktype']; ?>">
166.          <option value="0" <?php if (!(strcmp(0, $row_Recordset2['book_booktype']))) {echo "selected=\"selected\"";} ?>>= กรุณาเลือก =</option>
167.          <?php
168.do
169.?>
170.          <?php
171.} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
172.  $rows = mysql_num_rows($Recordset1);
173.  if($rows > 0) {
174.      mysql_data_seek($Recordset1, 0);
175.      $row_Recordset1 = mysql_fetch_assoc($Recordset1);
176.  }
177.?>
178.          <?php
179.do
180.?>
181.          <option value="<?php echo $row_Recordset1['booktype_id']?>"<?php if (!(strcmp($row_Recordset1['booktype_id'], $row_Recordset2['book_booktype']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['booktype_name']?></option>
182.<?php
183.} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
184.  $rows = mysql_num_rows($Recordset1);
185.  if($rows > 0) {
186.      mysql_data_seek($Recordset1, 0);
187.      $row_Recordset1 = mysql_fetch_assoc($Recordset1);
188.  }
189.?>
190.                        </select>
191.      </label></td>
192.      <td>&nbsp;</td>
193.    </tr>
194.    <tr>
195.      <td class="style13" >สำนักพิมพ์</td>
196.      <td><label>
197.        <select name="book_press" id="book_press" title="<?php echo $row_Recordset3['book_press']; ?>">
198.          <option value="0" <?php if (!(strcmp(0, $row_Recordset2['book_press']))) {echo "selected=\"selected\"";} ?>>= กรุณาเลือก =</option>
199.          <?php
200.do
201.?>
202.          <?php
203.} while ($row_Recordset4 = mysql_fetch_assoc($Recordset4));
204.  $rows = mysql_num_rows($Recordset4);
205.  if($rows > 0) {
206.      mysql_data_seek($Recordset4, 0);
207.      $row_Recordset4 = mysql_fetch_assoc($Recordset4);
208.  }
209.?>
210.          <?php
211.do
212.?>
213.          <option value="<?php echo $row_Recordset4['press_id']?>"<?php if (!(strcmp($row_Recordset4['press_id'], $row_Recordset2['book_press']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset4['press_name']?></option>
214.          <?php
215.} while ($row_Recordset4 = mysql_fetch_assoc($Recordset4));
216.  $rows = mysql_num_rows($Recordset4);
217.  if($rows > 0) {
218.      mysql_data_seek($Recordset4, 0);
219.      $row_Recordset4 = mysql_fetch_assoc($Recordset4);
220.  }
221.?>
222.                        </select>
223.      </label></td>
224.      <td>&nbsp;</td>
225.    </tr>
226.    <tr>
227.      <td>&nbsp;</td>
228.      <td>&nbsp;</td>
229.      <td>&nbsp;</td>
230.    </tr>
231.    <tr>
232.      <td colspan="3"><div align="center">
233.          <label>
234.          <input type="submit" name="acc" id="acc" value="ตกลง" />
235.          </label>
236.          <label>
237.          <input name="btn_can" type="button" id="btn_can" onclick="MM_goToURL('parent','administrator_book.php');return document.MM_returnValue" value="ยกเลิก" />
238.          </label>
239.      </div></td>
240.    </tr>
241.  </table>
242.  <input name="hiddenField" type="hidden" id="hiddenField" value="<?php echo $row_Recordset2['book_id']; ?>" />
243.  <input name="hiddenField2" type="hidden" id="hiddenField2" value="<?php echo $row_Recordset3['id']; ?>" />
244.  <input type="hidden" name="MM_update" value="form1" />
245.</form>
246.</body>
247.</html>
248.<?php
249.mysql_free_result($Recordset1);
250. 
251.mysql_free_result($Recordset2);
252. 
253.mysql_free_result($Recordset3);
254. 
255.mysql_free_result($Recordset4);
256.?>



ใน Code นี้

จะทำแก้ อัพเดจ แต่ เรคคอf 3

เรคคอด 2 ไม่ยอมอัพเดจ

รบกวนพี่ๆหน่อยครับ



Tag : PHP, MySQL, HTML/CSS, CakePHP



ประวัติการแก้ไข
2012-04-10 14:49:45
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-04-10 14:48:29 By : numprick View : 1256 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

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

เขียน update 2 query ก็ได้แล้วน่ะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-04-10 21:08:09 By : webmaster
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ผมต้องการ Update ข้อมูลพร้อมกัน 2 Table ภายใน Form เดียวกัน
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





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