|
|
|
ช่วยหน่อยครับบบ แก้มาตั้งแต่วันเสารืยังไม่ได้เลย คิดไม่ออกเลย ช่วยผมหน่อยครับ |
|
|
|
|
|
|
|
จะเขียน function ยังไงครับ ให้มัน insert ตามจำนวนข้อมูลที่ คิวรี่ออกมา ผมดัดแปลงไป งง ไป ตอนนี้เวลามัน insert มันก้ยัง เข้าแค่ แถวเดียว
เอามาจากตัวอย่างนี่น่ะครับ https://www.thaicreate.com/php/php-mysql-add-insert-multiple-record.html
ใครก้ได้ช่วยผมหน่อยย ปวดหัวมาหลายวันล่ะ ไม่รุจะแก้ไขยังไง
Code (PHP)
<?
include('config.inc.php');
?>
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
<script language="JavaScript" type="text/JavaScript"></script>
</head>
<body>
<form action="phpMySQLAddSave.php" name="frmAdd" method="post">
<?
$SQL = "SELECT * FROM material";
$Query = mysql_query($SQL) or die ("Error Query [".$SQL."]");
$Num = mysql_num_rows($Query);
?>
</select>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="160"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="70"> <div align="center">Budget </div></th>
<th width="70"> <div align="center">Used </div></th>
</tr>
<?
for($i=1;$i<=$Num;$i++)
{
while($Re_SQL = mysql_fetch_array($Query))
{
?>
<tr>
<td><div align="center"><input type="text" name="txtCustomerID<?=$i;?>" value="<?=$Re_SQL["Name_Material"];?>" size="5"></div></td>
<td><input type="text" name="txtName<?=$i;?>" value="<?=$Re_SQL["Code_Material"];?>" size="20"></td>
<td><input type="text" name="txtEmail<?=$i;?>" value="<?=$Re_SQL["Quantity_Material"];?>" size="20"></td>
<td><div align="center"><input type="text" name="txtCountryCode<?=$i;?>" value="" size="2"></div></td>
<td align="right"><input type="text" name="txtBudget<?=$i;?>" value="" size="5"></td>
<td align="right"><input type="text" name="txtUsed<?=$i;?>" value="" size="5"></td>
</tr>
<?
}
}
?>
</table>
<input type="submit" name="submit" value="submit">
<input type="text" name="hdnLine" value="<?=$i;?>">
</form>
</body>
</html>
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
for($i=1;$i<=$_POST["hdnLine"];$i++)
{
if($_POST["txtCustomerID$i"] != "")
{
$strSQL = "INSERT INTO customer ";
$strSQL .="(CustomerID,Name,Email,CountryCode,Budget,Used) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtCustomerID$i"]."','".$_POST["txtName$i"]."','".$_POST["txtEmail$i"]."' ";
$strSQL .=",'".$_POST["txtCountryCode$i"]."','".$_POST["txtBudget$i"]."','".$_POST["txtUsed$i"]."') ";
$objQuery = mysql_query($strSQL);
}
}
echo "Save Done. Click <a href='phpMySQLListRecord.php'>here</a> to view.";
mysql_close($objConnect);
?>
</body>
</html>
Tag : PHP, MySQL, JavaScript
|
|
|
|
|
|
Date :
2013-05-13 11:48:24 |
By :
cappuczino |
View :
837 |
Reply :
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้า register_globals = On จะสามารถเขียน tag php แบบ short ได้ ครับ
ถ้า register_globals = Off จะต้องเขียน tag php แบบ เต็มรูปแบบเช่น
Code (PHP)
<?
include('config.inc.php');
?>
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
<script language="JavaScript" type="text/JavaScript"></script>
</head>
<body>
<form action="phpMySQLAddSave.php" name="frmAdd" method="post">
<?
$SQL = "SELECT * FROM material";
$Query = mysql_query($SQL) or die ("Error Query [".$SQL."]");
$Num = mysql_num_rows($Query);
?>
</select>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="160"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="70"> <div align="center">Budget </div></th>
<th width="70"> <div align="center">Used </div></th>
</tr>
<?
for($i=1;$i<=$Num;$i++)
{
while($Re_SQL = mysql_fetch_array($Query))
{
?>
<tr>
<td><div align="center"><input type="text" name="txtCustomerID<?php echo $i;?>" value="<?php echo $Re_SQL["Name_Material"];?>" size="5"></div></td>
<td><input type="text" name="txtName<?php echo $i;?>" value="<?php echo $Re_SQL["Code_Material"];?>" size="20"></td>
<td><input type="text" name="txtEmail<?php echo $i;?>" value="<?php echo $Re_SQL["Quantity_Material"];?>" size="20"></td>
<td><div align="center"><input type="text" name="txtCountryCode<?php echo $i;?>" value="" size="2"></div></td>
<td align="right"><input type="text" name="txtBudget<?php echo $i;?>" value="" size="5"></td>
<td align="right"><input type="text" name="txtUsed<?php echo $i;?>" value="" size="5"></td>
</tr>
<?
}
}
?>
</table>
<input type="submit" name="submit" value="submit">
<input type="text" name="hdnLine" value="<?php echo $i;?>">
</form>
</body>
</html>
|
ประวัติการแก้ไข 2013-05-13 11:56:44
|
|
|
|
Date :
2013-05-13 11:55:47 |
By :
popnakub |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมแนะนำ register_globals = Off นะครับเพราะจะทำให้เราเขียนได้ถูกต้องครับ
|
|
|
|
|
Date :
2013-05-13 11:57:56 |
By :
popnakub |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<?
include('config.inc.php');
?>
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
<script language="JavaScript" type="text/JavaScript"></script>
</head>
<body>
<form action="phpMySQLAddSave.php" name="frmAdd" method="post">
<?
$SQL = "SELECT * FROM material";
$Query = mysql_query($SQL) or die ("Error Query [".$SQL."]");
//$Num = mysql_num_rows($Query);
?>
</select>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="160"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="70"> <div align="center">Budget </div></th>
<th width="70"> <div align="center">Used </div></th>
</tr>
<?
//for($i=1;$i<=$Num;$i++)
//{
$i=0;
while($Re_SQL = mysql_fetch_array($Query))
{
$i++;
?>
<tr>
<td><div align="center"><input type="text" name="txtCustomerID<?php echo $i;?>" value="<?php echo $Re_SQL["Name_Material"];?>" size="5"></div></td>
<td><input type="text" name="txtName<?php echo $i;?>" value="<?php echo $Re_SQL["Code_Material"];?>" size="20"></td>
<td><input type="text" name="txtEmail<?php echo $i;?>" value="<?php echo $Re_SQL["Quantity_Material"];?>" size="20"></td>
<td><div align="center"><input type="text" name="txtCountryCode<?php echo $i;?>" value="" size="2"></div></td>
<td align="right"><input type="text" name="txtBudget<?php echo $i;?>" value="" size="5"></td>
<td align="right"><input type="text" name="txtUsed<?php echo $i;?>" value="" size="5"></td>
</tr>
<?
}
// }
?>
</table>
<input type="submit" name="submit" value="submit">
<input type="text" name="hdnLine" value="<?php echo $i;?>">
</form>
</body>
</html>
|
|
|
|
|
Date :
2013-05-13 13:04:34 |
By :
thep |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีปัญหาอีกแล้วครับ ถ้าผมเพิ่มข้อมูล แต่ไม่ได้อยู่ใน While คือ อยู่นอก while เวลา insert ต้องเขียนยังไงให้มันเข้าไปทุก เรคอดที่ insert เข้าไปครับ คือมันเข้าไปแถวเดียวอีกแล้ว
<input type="text" name="txttest<?php echo $i;?>" value" " />
Code (PHP)
<?
include('config.inc.php');
?>
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
<script language="JavaScript" type="text/JavaScript"></script>
</head>
<body>
<form action="phpMySQLAddSave.php" name="frmAdd" method="post">
<?
$SQL = "SELECT * FROM material";
$Query = mysql_query($SQL) or die ("Error Query [".$SQL."]");
//$Num = mysql_num_rows($Query);
?>
</select>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="160"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="70"> <div align="center">Budget </div></th>
<th width="70"> <div align="center">Used </div></th>
</tr>
<?
//for($i=1;$i<=$Num;$i++)
//{
$i=0;
while($Re_SQL = mysql_fetch_array($Query))
{
$i++;
?>
<tr>
<td><div align="center"><input type="text" name="txtCustomerID<?php echo $i;?>" value="<?php echo $Re_SQL["Name_Material"];?>" size="5"></div></td>
<td><input type="text" name="txtName<?php echo $i;?>" value="<?php echo $Re_SQL["Code_Material"];?>" size="20"></td>
<td><input type="text" name="txtEmail<?php echo $i;?>" value="<?php echo $Re_SQL["Quantity_Material"];?>" size="20"></td>
<td><div align="center"><input type="text" name="txtCountryCode<?php echo $i;?>" value="" size="2"></div></td>
<td align="right"><input type="text" name="txtBudget<?php echo $i;?>" value="" size="5"></td>
<td align="right"><input type="text" name="txtUsed<?php echo $i;?>" value="" size="5"></td>
</tr>
<?
}
// }
?>
<input type="text" name="txttest<?php echo $i;?>" value" " />
</table>
<input type="submit" name="submit" value="submit">
<input type="text" name="hdnLine" value="<?php echo $i;?>">
</form>
</body>
</html>
|
ประวัติการแก้ไข 2013-05-13 15:46:57
|
|
|
|
Date :
2013-05-13 15:45:58 |
By :
cappuczino |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
สมมติเราเพิ่ม <input type="text" name="txttest" value="test" />
พอเรากด submit
$txttest = $_POST["txttest"];
if($txttest != ""){
INSERT .....
}
อย่างนี้ใช้ได้ไหม? ตรงตามที่ต้องการหรือเปล่า?
|
|
|
|
|
Date :
2013-05-13 17:18:43 |
By :
thep |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<?
include('config.inc.php');
?>
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
<script language="JavaScript" type="text/JavaScript"></script>
</head>
<body>
<form action="phpMySQLAddSave.php" name="frmAdd" method="post">
<?php
$SQL = "SELECT * FROM material";
$Query = mysql_query($SQL) or die ("Error Query [".$SQL."]");
//$Num = mysql_num_rows($Query);
?>
</select>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="160"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="70"> <div align="center">Budget </div></th>
<th width="70"> <div align="center">Used </div></th>
</tr>
<?php
$i=0;
while($Re_SQL = mysql_fetch_array($Query)){
$i++;
?>
<tr>
<td><div align="center"><input type="text" name="txtCustomerID<?php echo $i;?>" value="<?php echo $Re_SQL["Name_Material"];?>" size="5"></div></td>
<td><input type="text" name="txtName<?php echo $i;?>" value="<?php echo $Re_SQL["Code_Material"];?>" size="20"></td>
<td><input type="text" name="txtEmail<?php echo $i;?>" value="<?php echo $Re_SQL["Quantity_Material"];?>" size="20"></td>
<td><div align="center"><input type="text" name="txtCountryCode<?php echo $i;?>" value="" size="2"></div></td>
<td align="right"><input type="text" name="txtBudget<?php echo $i;?>" value="" size="5"></td>
<td align="right"><input type="text" name="txtUsed<?php echo $i;?>" value="" size="5"></td>
</tr>
<?
}
$i++;
$valMax = ($i + 10);
for($n=$i;$n<=$valMax;$n++){
?>
<tr>
<td><div align="center"><input type="text" name="txtCustomerID<?=$n;?>" value="" size="5"></div></td>
<td><input type="text" name="txtName<?=$n;?>" value="" size="20"></td>
<td><input type="text" name="txtEmail<?=$n;?>" value="" size="20"></td>
<td><div align="center"><input type="text" name="txtCountryCode<?=$n;?>" value="" size="2"></div></td>
<td align="right"><input type="text" name="txtBudget<?=$n;?>" value="" size="5"></td>
<td align="right"><input type="text" name="txtUsed<?=$n;?>" value="" size="5"></td>
</tr>
<?php
}
?>
</table>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="hdnLine" value="<?=$valMax;?>">
</form>
</body>
</html>
|
|
|
|
|
Date :
2013-05-14 08:51:10 |
By :
thep |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่ต้อง
|
|
|
|
|
Date :
2013-05-14 10:40:40 |
By :
thep |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|