PHP 将DB中的return值放入输入框
PHP Put return value from DB to input box
美好的一天!,我有一个问题。我有下面的代码,return 来自我的数据库的 pos_name 和 train_name 的值。我的问题是如何将 returned 值放入每个文本框都有唯一名称的文本框内。因为我要把它保存到另一个数据库。
我想要这样的东西
<input type="text" name="<?php echo $row["pos_name"][0] ?>" value="<?php echo $row["train_name"][0] ?>"
<input type="text" name="<?php echo $row["pos_name"][1] ?>" value="<?php echo $row["train_name"][1] ?>"
// The number of textbox will depend on the number of returned value or the ($i)
//MY PHP CODE =========
<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("tms",$con);
$Query="SELECT id,pos_name,train_name FROM pos_train_db ORDER BY id DESC LIMIT 15";
$sql = mysql_query($Query, $con);
$dyn_table = '<table border="1" cellpadding="10">';
while( $row = mysql_fetch_array($sql)){
$id = $row["id"];
$pos_name = $row["pos_name"];
$train_name = $row["train_name"];
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dyn_table .= '<tr><td>' . $pos_name . '</td>';
} else {
$dyn_table .= '<td>' . $pos_name . '</td>';
}
$i++;
}
$dyn_table .= '</tr></table>';
?>
<?php
//SOME TESTING CODE
echo $dyn_table;
echo $i;
echo "<input type='text' value=" . "$pos_name" . ">";
?>
请帮忙谢谢!
对于 OP 的要求不是很清楚,但是 - 我想这可能是答案的一部分:
<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("tms",$con);
$Query="SELECT id,pos_name,train_name FROM pos_train_db ORDER BY id DESC LIMIT 15";
$sql = mysql_query($Query, $con);
$dyn_table = '<table border="1" cellpadding="10">';
while( $row = mysql_fetch_array($sql)){
$id = html_entity_decode($row["id"]);
$pos_name = html_entity_decode($row["pos_name"]);
$train_name = html_entity_decode($row["train_name"]);
echo "<input type=\"text\" name=\"".$id."\" value=\"".$train_name."\"></input>";
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dyn_table .= '<tr><td>' . $pos_name . '</td>';
} else {
$dyn_table .= '<td>' . $pos_name . '</td>';
}
$i++;
}
$dyn_table .= '</tr></table>';
?>
美好的一天!,我有一个问题。我有下面的代码,return 来自我的数据库的 pos_name 和 train_name 的值。我的问题是如何将 returned 值放入每个文本框都有唯一名称的文本框内。因为我要把它保存到另一个数据库。
我想要这样的东西
<input type="text" name="<?php echo $row["pos_name"][0] ?>" value="<?php echo $row["train_name"][0] ?>"
<input type="text" name="<?php echo $row["pos_name"][1] ?>" value="<?php echo $row["train_name"][1] ?>"
// The number of textbox will depend on the number of returned value or the ($i)
//MY PHP CODE =========
<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("tms",$con);
$Query="SELECT id,pos_name,train_name FROM pos_train_db ORDER BY id DESC LIMIT 15";
$sql = mysql_query($Query, $con);
$dyn_table = '<table border="1" cellpadding="10">';
while( $row = mysql_fetch_array($sql)){
$id = $row["id"];
$pos_name = $row["pos_name"];
$train_name = $row["train_name"];
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dyn_table .= '<tr><td>' . $pos_name . '</td>';
} else {
$dyn_table .= '<td>' . $pos_name . '</td>';
}
$i++;
}
$dyn_table .= '</tr></table>';
?>
<?php
//SOME TESTING CODE
echo $dyn_table;
echo $i;
echo "<input type='text' value=" . "$pos_name" . ">";
?>
请帮忙谢谢!
对于 OP 的要求不是很清楚,但是 - 我想这可能是答案的一部分:
<?php
$con = mysql_connect("localhost","root","");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("tms",$con);
$Query="SELECT id,pos_name,train_name FROM pos_train_db ORDER BY id DESC LIMIT 15";
$sql = mysql_query($Query, $con);
$dyn_table = '<table border="1" cellpadding="10">';
while( $row = mysql_fetch_array($sql)){
$id = html_entity_decode($row["id"]);
$pos_name = html_entity_decode($row["pos_name"]);
$train_name = html_entity_decode($row["train_name"]);
echo "<input type=\"text\" name=\"".$id."\" value=\"".$train_name."\"></input>";
if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
$dyn_table .= '<tr><td>' . $pos_name . '</td>';
} else {
$dyn_table .= '<td>' . $pos_name . '</td>';
}
$i++;
}
$dyn_table .= '</tr></table>';
?>