|
|
|
ต้องการเก็บข้อมูลลง Mysql โดยใช้ Select เป็นตัวเลือก Table DB ทำยังไงครับ |
|
|
|
|
|
|
|
รับค่าชื่อ table จาก select ก่อนครับสมมุติใช้ POST
$tb_name = $_POST['select_tb'];
insert into {$tb_name} (ฟิลด์) values (ค่า)
|
|
|
|
|
Date :
2014-04-11 12:16:50 |
By :
Manussawin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<head>
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("fprojectscience")
?>
</head>
<body
<select name="tablesel" >
<option><? $tb_name= $post['bachelor'];?>bachelor</option>
<option><? $tb_name = $post['vocation']; ?> vocation</option>
</select>
</body>
แบบนี้หรอครับหรือยังไง ช่วยอธิบายมากกว่านี้ทีครับ
|
|
|
|
|
Date :
2014-04-11 12:26:56 |
By :
็Harizadoz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เอา code ทั้งหมดมาดูหน่อยสิครับ
|
|
|
|
|
Date :
2014-04-11 14:49:11 |
By :
pokultra |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
<script language="JavaScript" type="text/JavaScript">
function CreateSelectOption(ele)
{
var objSelect = document.getElementById(ele);
var Item = new Option("", "");
objSelect.options[objSelect.length] = Item;
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
var Item = new Option("<?=$objResult["subject$i"];?>", "<?=$objResult["txtCustomerID$i"];?>");
objSelect.options[objSelect.length] = Item;
<?
}
?>
}
function CreateNewRow()
{
var intLine = parseInt(document.frmMain.hdnMaxLine.value);
intLine++;
var theTable = document.all.tbExp
var newRow = theTable.insertRow(theTable.rows.length)
newRow.id = newRow.uniqueID
var item1 = 1
var newCell
//*** Column 1 ***//
newCell = newRow.insertCell(0)
newCell.id = newCell.uniqueID
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><td><INPUT TYPE=\"TEXT\" NAME=\"txtCustomerID<?=$i;?>"+intLine+"\" VALUE=\"\"></td></center>"
//*** Column 2 ***//
newCell = newRow.insertCell(1)
newCell.id = newCell.uniqueID
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><td><INPUT TYPE=\"TEXT\" NAME=\"txtName<?=$i;?>"+intLine+"\" VALUE=\"\"></td></center>"
//*** Column 3 ***//
newCell = newRow.insertCell(2)
newCell.id = newCell.uniqueID
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><td><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"txtEmail<?=$i;?>"+intLine+"\" VALUE=\"\"></td></center>"
//*** Column 4 ***//
newCell = newRow.insertCell(3)
newCell.id = newCell.uniqueID
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><td><INPUT TYPE=\"TEXT\" SIZE=\"5\" NAME=\"txtCountryCode<?=$i;?>"+intLine+"\" VALUE=\"\"></td></center>"
newCell = newRow.insertCell(4);
newCell.id = newCell.uniqueID;
newCell.setAttribute("className", "css-name");
newCell.innerHTML = "<center><SELECT NAME=\"Column5_"+intLine+"\" ID=\"Column5_"+intLine+"\"></SELECT></center>";
//*** Create Option ***//
CreateSelectOption("Column5_"+intLine)
document.frmMain.hdnMaxLine.value = intLine;
}
function RemoveRow()
{
intLine = parseInt(document.frmMain.hdnMaxLine.value);
if(parseInt(intLine) > 0)
{
theTable = (document.all) ? document.all.tbExp :
document.getElementById("tbExp")
theTableBody = theTable.tBodies[0];
theTableBody.deleteRow(intLine);
intLine--;
document.frmMain.hdnMaxLine.value = intLine;
}
}
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>
<style>
#tbExp{font-family:"Lucida Sans Unicode", "Lucida Grande", Sans-Serif;font-size:12px;width:480px;
text-align:left;border-collapse:collapse;border:1px solid #69c;margin:20px;}
#tbExp th{ text-align:center; font-weight:bold;font-size:14px;color:#039;padding:15px 10px 10px;background:#d0dafd;}
#tbExp tbody{background:#e8edff;}
#tbExp td{color:#669;border-top:1px dashed #fff;padding:10px;}
#tbExp tbody tr:hover td{color:#339;background:#d0dafd;}
</style>
<?
mysql_connect("localhost","root","12345");
mysql_select_db("fprojectscience");
?>
<body>
<form action="phpMySQLAddSave.php
" name="frmMain" method="post" >
<select hidden="yes" name="menu1" onChange="MM_jumpMenu('parent',this,0)">
<?
for($i=5;$i<=5;$i++)
{
if($_GET["Line"] == $i)
{
$sel = "selected";
}
else
{
$sel = "";
}
?>
<option value="<?=$_SERVER["PHP_SELF"];?>?Line=<?=$i;?>" <?=$sel;?>><?=$i;?></option>
<?
}
?>
</select>
<table width="600" border="1" id="tbExp">
<tr>
<th width="91"> <div align="center">ID </div></th>
<th width="160"> <div align="center">Subject </div></th>
<th width="198"> <div align="center">Credit </div></th>
<th width="97"> <div align="center">Cound </div></th>
<td><div align="center">Column 5 </div></td>
</tr>
<?
$line = $_GET["Line"];
if($line == 0){$line=1;}
for($i=1;$i<=$line;$i++)
{
?>
<tr>
<td><input type="text" name="txtCustomerID<?=$i;?>" values=""></td>
<td><input type="text" name="txtName<?=$i;?>" values=""></td>
<td><div align="center"><input type="text" name="txtEmail<?=$i;?>" values="" size="5"></div></td>
<td><div align="center"><input type="text" name="txtCountryCode<?=$i;?>" values="" size="5"></div></td>
<td>
</tr>
<?
}
?>
</table>
<select name="Liname">
</select>
<input name="btnAdd" type="button" id="btnAdd" value="+" onClick="CreateNewRow();">
<input name="btnDel" type="button" id="btnDel" value="-" onClick="RemoveRow();">
<input type="submit" name="submit" value="submit">
<input type="hidden" name="hdnMaxLine" value="<?=$i;?>">
</form>
</body>
</html>
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","12345") or die("Error Connect to Database");
$objDB = mysql_select_db("fprojectscience");
for($i=1;$i<=$_POST["hdnMaxLine"];$i++)
{
if($_POST["txtCustomerID$i"] != "")
{
$strSQL ="insert into bachelor";
$strSQL .="(id_subject,subject,credit,cound) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtCustomerID$i"]."','".$_POST["txtName$i"]."', ";
$strSQL .="'".$_POST["txtEmail$i"]."' ";
$strSQL .=",'".$_POST["txtCountryCode$i"]."')";
$objQuery = mysql_query($strSQL);
}
}
echo "Save Done. Click <a href='phpMySQLListRecord.php'>here</a> to view.";
echo $objQuery;
mysql_close($objConnect);
?>
</body>
</html>
ืที่ผมอยากทราบคือ ทำยังไง เราจะใช้ Select เลือก Insert ข้อมูลลง ตาราง เช่นผมมี ตาราง bachelor ถ้าเรา Select bachelor มันก้จะไป Insert ข้อมูลลงใน Record ของ Bachelor
หรือถ้าผมเลือก Select vocation มันก้ Insert ข้อมูลลงใน Record vocation อะครับ พอเข้าใจใช่ป่ะครับ
|
|
|
|
|
Date :
2014-04-11 15:18:47 |
By :
Harizadoz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[/head]
<select name="tablesel" >
<option value="bachelor">bachelor</option>
<option value="vocation">vocation</option>
</select>
[head]Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
</head>
<body>
<?
$objConnect = mysql_connect("localhost","root","12345") or die("Error Connect to Database");
$objDB = mysql_select_db("fprojectscience");
for($i=1;$i<=$_POST["hdnMaxLine"];$i++)
{
if($_POST["txtCustomerID$i"] != "")
{
$tbName=$_POST['tablesel']; // รับชื่อตารางที่เลือก
$strSQL ="insert into ".$tbName; //เพิ่มข้อมูลลงในตารางที่เลือกมา
$strSQL .="(id_subject,subject,credit,cound) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtCustomerID$i"]."','".$_POST["txtName$i"]."', ";
$strSQL .="'".$_POST["txtEmail$i"]."' ";
$strSQL .=",'".$_POST["txtCountryCode$i"]."')";
$objQuery = mysql_query($strSQL);
}
}
echo "Save Done. Click <a href='phpMySQLListRecord.php'>here</a> to view.";
echo $objQuery;
mysql_close($objConnect);
?>
<select name="tablesel" >
<option value="bachelor">bachelor</option>
<option value="vocation">vocation</option>
</select>
</body>
</html>
ประมาณนี้มั้งครับ ...ลองปรับใช้ดูนะครับ
|
|
|
|
|
Date :
2014-04-11 15:35:35 |
By :
pokultra |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้พอดีเลยครับ ขอบคุณมากครับ
|
|
|
|
|
Date :
2014-04-11 19:49:49 |
By :
Harizadoz |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|