|
|
|
ผมต้องการ Update ข้อมูลพร้อมกัน 2 Table ภายใน Form เดียวกัน |
|
|
|
|
|
|
|
ตอนนี้ผมจะทำการอัพเดจข้อมูลครับ
โดยเป็นข้อมูล จำนวน 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)
<?php require_once('Connections/library.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$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",
GetSQLValueString($_POST['hiddenField'], "int"),
GetSQLValueString($_POST['book_name'], "text"),
GetSQLValueString($_POST['book_author'], "text"),
GetSQLValueString($_POST['book_booktype'], "int"),
GetSQLValueString($_POST['book_press'], "text"),
GetSQLValueString($_POST['book_page'], "text"),
GetSQLValueString($_POST['book_timeprint'], "text"),
GetSQLValueString($_POST['txtUsername'], "text"));
mysql_select_db($database_library, $library);
$Result1 = mysql_query($updateSQL, $library) or die(mysql_error());
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE book_number SET book_isbn=%s, book_number_number=%s, book_copy=%s WHERE id=%s",
GetSQLValueString($_POST['txtUsername'], "text"),
GetSQLValueString($_POST['book_number_number'], "text"),
GetSQLValueString($_POST['book_coppy'], "text"),
GetSQLValueString($_POST['hiddenField2'], "int"));
mysql_select_db($database_library, $library);
$Result1 = mysql_query($updateSQL, $library) or die(mysql_error());
}
mysql_select_db($database_library, $library);
$query_Recordset1 = "SELECT * FROM booktype";
$Recordset1 = mysql_query($query_Recordset1, $library) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$colname_Recordset2 = "-1";
if (isset($_GET['book_id'])) {
$colname_Recordset2 = $_GET['book_id'];
}
mysql_select_db($database_library, $library);
$query_Recordset2 = sprintf("SELECT * FROM book WHERE book_id = %s", GetSQLValueString($colname_Recordset2, "int"));
$Recordset2 = mysql_query($query_Recordset2, $library) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
$colname_Recordset3 = "-1";
if (isset($_GET['id'])) {
$colname_Recordset3 = $_GET['id'];
}
mysql_select_db($database_library, $library);
$query_Recordset3 = sprintf("SELECT * FROM book_number WHERE id = %s", GetSQLValueString($colname_Recordset3, "int"));
$Recordset3 = mysql_query($query_Recordset3, $library) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);
mysql_select_db($database_library, $library);
$query_Recordset4 = "SELECT * FROM press";
$Recordset4 = mysql_query($query_Recordset4, $library) or die(mysql_error());
$row_Recordset4 = mysql_fetch_assoc($Recordset4);
$totalRows_Recordset4 = mysql_num_rows($Recordset4);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
</head>
<body>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
<table width="50%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="47" colspan="3"><div align="center" class="style14">แก้ไขข้อมูลหนังสือ</div></td>
</tr>
<tr>
<td width="35%" class="style13">ISBN</td>
<td width="59%"><label>
<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']; ?>" />
</label></td>
<td width="6%"> </td>
</tr>
<tr>
<td class="style13" >ชื่อหนังสือ</td>
<td><label>
<input name="book_name" type="text" id="book_name" value="<?php echo $row_Recordset2['book_name']; ?>" />
</label></td>
<td> </td>
</tr>
<tr>
<td class="style13" >เลขทะเบียน</td>
<td><label>
<input name="book_number_number" type="text" id="book_number_number" onblur="JavaScript:doCallAjax();" value="<?php echo $row_Recordset3['book_number_number']; ?>" />
<span id="mySpan"></span> </label></td>
<td> </td>
</tr>
<tr>
<td class="style13" >ฉบับ</td>
<td><input name="book_coppy" type="text" id="book_coppy" value="<?php echo $row_Recordset3['book_copy']; ?>" /></td>
<td> </td>
</tr>
<tr>
<td class="style13" >ชื่อผู้แต่ง</td>
<td><label>
<input name="book_author" type="text" id="book_author" value="<?php echo $row_Recordset2['book_author']; ?>" />
</label></td>
<td> </td>
</tr>
<tr>
<td class="style13" >จำนวนหน้า</td>
<td><label>
<input name="book_page" type="text" id="book_page" onkeypress="return chkNumber(this)" value="<?php echo $row_Recordset2['book_page']; ?>"/>
</label></td>
<td> </td>
</tr>
<tr>
<td class="style13" >ครั้งที่พิมพ์</td>
<td><label>
<input name="book_timeprint" type="text" id="book_timeprint" onkeypress="return chkNumber(this)" value="<?php echo $row_Recordset2['book_timeprint']; ?>" />
</label></td>
<td> </td>
</tr>
<tr>
<td class="style13" >ประเภทหนังสือ</td>
<td><label>
<select name="book_booktype" id="book_booktype" title="<?php echo $row_Recordset3['book_booktype']; ?>">
<option value="0" <?php if (!(strcmp(0, $row_Recordset2['book_booktype']))) {echo "selected=\"selected\"";} ?>>= กรุณาเลือก =</option>
<?php
do {
?>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?>
<?php
do {
?>
<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>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?>
</select>
</label></td>
<td> </td>
</tr>
<tr>
<td class="style13" >สำนักพิมพ์</td>
<td><label>
<select name="book_press" id="book_press" title="<?php echo $row_Recordset3['book_press']; ?>">
<option value="0" <?php if (!(strcmp(0, $row_Recordset2['book_press']))) {echo "selected=\"selected\"";} ?>>= กรุณาเลือก =</option>
<?php
do {
?>
<?php
} while ($row_Recordset4 = mysql_fetch_assoc($Recordset4));
$rows = mysql_num_rows($Recordset4);
if($rows > 0) {
mysql_data_seek($Recordset4, 0);
$row_Recordset4 = mysql_fetch_assoc($Recordset4);
}
?>
<?php
do {
?>
<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>
<?php
} while ($row_Recordset4 = mysql_fetch_assoc($Recordset4));
$rows = mysql_num_rows($Recordset4);
if($rows > 0) {
mysql_data_seek($Recordset4, 0);
$row_Recordset4 = mysql_fetch_assoc($Recordset4);
}
?>
</select>
</label></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="3"><div align="center">
<label>
<input type="submit" name="acc" id="acc" value="ตกลง" />
</label>
<label>
<input name="btn_can" type="button" id="btn_can" onclick="MM_goToURL('parent','administrator_book.php');return document.MM_returnValue" value="ยกเลิก" />
</label>
</div></td>
</tr>
</table>
<input name="hiddenField" type="hidden" id="hiddenField" value="<?php echo $row_Recordset2['book_id']; ?>" />
<input name="hiddenField2" type="hidden" id="hiddenField2" value="<?php echo $row_Recordset3['id']; ?>" />
<input type="hidden" name="MM_update" value="form1" />
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
mysql_free_result($Recordset2);
mysql_free_result($Recordset3);
mysql_free_result($Recordset4);
?>
ใน Code นี้
จะทำแก้ อัพเดจ แต่ เรคคอf 3
เรคคอด 2 ไม่ยอมอัพเดจ
รบกวนพี่ๆหน่อยครับ
Tag : PHP, MySQL, HTML/CSS, CakePHP
|
ประวัติการแก้ไข 2012-04-10 14:49:45
|
|
|
|
|
Date :
2012-04-10 14:48:29 |
By :
numprick |
View :
1230 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เขียน update 2 query ก็ได้แล้วน่ะครับ
|
|
|
|
|
Date :
2012-04-10 21:08:09 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|