insert ข้อมูลไม่ได้ครับช่วยแนะนำแนวทางหน่อยครับ
<?php require_once('Connections/Myconnect.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$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_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO main (Date, Site, Network, Type, Loads, Bank, Backup, EndVoltage, Source, Users, Comment) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['Date'], "date"),
GetSQLValueString($_POST['Site'], "text"),
GetSQLValueString($_POST['Network'], "text"),
GetSQLValueString($_POST['Type'], "text"),
GetSQLValueString($_POST['Loads'], "double"),
GetSQLValueString($_POST['Bank'], "int"),
GetSQLValueString($_POST['Backup'], "int"),
GetSQLValueString($_POST['EndVoltage'], "double"),
GetSQLValueString($_POST['Source'], "text"),
GetSQLValueString($_POST['Users'], "text"),
GetSQLValueString($_POST['Comment'], "text"));
mysql_select_db($database_Myconnect, $Myconnect);
$Result1 = mysql_query($insertSQL, $Myconnect) or die(mysql_error());
$insertGoTo = "Main.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_Myconnect, $Myconnect);
$query_Recordset1 = "SELECT Bat_type, Bat_gen FROM battery";
$Recordset1 = mysql_query($query_Recordset1, $Myconnect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
mysql_select_db($database_Myconnect, $Myconnect);
$query_Recordset2 = "SELECT `Users` FROM main";
$Recordset2 = mysql_query($query_Recordset2, $Myconnect) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
?>
<!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>Insert</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table width="651" align="center">
<tr valign="baseline">
<td width="643"><table width="599" align="center">
<tr valign="baseline">
<td width="88" align="right" nowrap="nowrap"><strong>วันที่</strong> :</td>
<td width="383" bgcolor="#D6D6D6"><!doctype html>
<meta charset="utf-8" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<input type="text" id="datepicker" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Site :</td>
<td><input name="Site" type="text" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Network :</td>
<td><select name="Network">
<option value="" >--Please Select--</option>
<option value="" >2G</option>
<option value="" >3G</option>
<option value="" >4G</option>
</select></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="right">Battery Band :</div></td>
<td><select name="Type" id="Type">
<option value="" <?php if (!(strcmp("", $row_Recordset1['Bat_type']))) {echo "selected=\"selected\"";} ?>>--Please Select--</option>
<?php
do {
?>
<option value="<?php echo $row_Recordset1['Bat_type']?>"<?php if (!(strcmp($row_Recordset1['Bat_type'], $row_Recordset1['Bat_type']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['Bat_type']?></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>
Model :
<select name="Type2">
<option value="" <?php if (!(strcmp("", $row_Recordset1['Bat_type']))) {echo "selected=\"selected\"";} ?>>--Please Select--</option>
<?php
do {
?>
<option value="<?php echo $row_Recordset1['Bat_gen']?>"<?php if (!(strcmp($row_Recordset1['Bat_gen'], $row_Recordset1['Bat_type']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['Bat_gen']?></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></td>
<td width="82" align="right" nowrap="nowrap"><div align="right"><a href="Type.php">Insert Battery</a></div></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="right">Load :</div></td>
<td><input type="text" name="Loads" value="" size="20" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="right">Bank Total : </div></td>
<td><input type="text" name="Bank" value="" size="20" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="right">Backup Time :</div></td>
<td><input type="text" name="Backup" value="" size="20" />
นาที</td>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="right">EndVoltage :</div></td>
<td><input type="text" name="EndVoltage" value="" size="20" />
Volte</td>
<tr valign="baseline">
<td nowrap="nowrap" align="right"><div align="right">Source :</div></td>
<td><select name="Source">
<option value="" selected="selected" >--Please Select--</option>
<option value="" >Preventive</option>
<option value="" >Corrective</option>
<option value="" >Improvement</option>
<option value="" >Alarm</option>
<option value="" >Other</option>
</select></td>
</tr>
<tr valign="baseline">
<td height="47" align="center" valign="middle" nowrap="nowrap"><div align="right">Comment :</div></td>
<td><textarea name="Comment" cols="32" rows="5"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" /></td>
</tr>
</table></td>
</tr>
</table>
<input type="hidden" name="Users" value="<?php echo $row_Recordset2['Users']; ?>" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
mysql_free_result($Recordset2);
?>
โค้ดอาจะยาวไปนิดนึง คือผมลองทำการ insert ข้อมูล ตามโค้ดนี้เลยครับแต่มันฟ้องว่า date เป็นค่า null จะถามว่าค่า datepiker นี่เวลาจะแอดลงฐานข้อมูลจะต้องเขียนโค้ดยังไงครับ ขอแนวทางหน่อยครับTag : PHP, MySQL, HTML/CSS, jQuery
Date :
2014-12-21 18:52:33
By :
Arm8Lamp
View :
1064
Reply :
3
<input type="text" id="datepicker" value="" size="32" />
id ใส่ไม่ตรงกันครับ ดูที่ $_Post สิครับ
Date :
2014-12-22 03:33:12
By :
natt3021
datepicker ไม่มี name
Date :
2014-12-22 09:04:40
By :
armdbz
ก่อน Insert ช่วย echo "===> ".$_POST['Date']; break;
ดูสิครับ ว่ามันได้ค่าอะไร ถ้ามันไม่มีค่า แปลว่าใน form ไม่มีตัวแปร Date
ถ้ามีค่า ก็ดู format ว่า ได้ format yyyy-mm-dd ตรงกับที่สร้าง format ไว้ใน Database หรือเปล่าครับ ถ้าไม่ตรงกันมันก็จะไม่สร้างน่ะครับ
Date :
2014-12-22 14:49:08
By :
nut_ch31
Load balance : Server 01