SELECT语句的结果作为输入值PHP
我有一个与一行结果相呼应的查询SELECT语句的结果作为输入值PHP,php,sql,forms,Php,Sql,Forms,我有一个与一行结果相呼应的查询 <?php $sql = "SELECT dba_name, contact_owner, phone, confirmation_code, physical_address, physical_city, physical_state, physical_zip, urep FROM mpas"; $result = $conn->query($sql); ?> 稍后,我回显一些结果供用户查看 <?php if ($result-
<?php
$sql = "SELECT dba_name, contact_owner, phone, confirmation_code, physical_address, physical_city, physical_state, physical_zip, urep FROM mpas";
$result = $conn->query($sql);
?>
稍后,我回显一些结果供用户查看
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
.....
?>
在代码中,我想使用与隐藏字段的值相同的结果。我试过几件事都没有成功。该值为空。这是我最近的一次尝试
<?php echo "<input hidden name='confc' value='".$row['confirmation_code']."'>" ; ?>
我认为您没有正确地声明隐藏:
<input type="hidden" ...
我很确定在此期间您已经完成了另一个查询(即使没有,也没有循环任何内容)。试着这样做:
<?php
$codes = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$codes[] = $row['confirmation_code'];
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
.....
foreach ($codes as $c) {
echo "<input type='hidden' name='confc' value='".$c."'>" ;
}
?>
if (isset($all_rows[1])) { //Access the row with primary key 1 (as an example)
echo "<input type=\"hidden\" name='confc' value='".$all_rows[1]['confirmation_code']."'>" ;
}
没有必要重复你所需要的
<input type="hidden" name="confc" value="<?php echo $row['confirmation_code']; ?>" />
如果要多次使用,为什么不将该值设置为php变量?
(这可能无法解决问题,但可以解决一些可能的问题)
然后将其作为值放置:
<input type="hidden" name="confc" value="<?php echo $confc; ?>">
为什么不将其存储在变量中
while($row = $result->fetch_assoc()) {
$confirmation_code = $row["confirmation_code"];
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
.....
<?php echo "<input hidden name='confc' value='"$confirmation_code"'>" ; ?>
while($row=$result->fetch_assoc()){
$confirmation_code=$row[“confirmation_code”];
echo“确认码:“.$row[“确认码”]。”
”;
.....
您可以声明另一个数组,该数组存储SELECT数据库表的主键引用的所有行。如果SELECT列都不是主键,则必须将其添加到SELECT语句中,例如id column
$sql = "SELECT id, dba_name, contact_owner, phone, confirmation_code, physical_address, physical_city, physical_state, physical_zip, urep FROM mpas";
if ($result->num_rows > 0) {
$all_rows = array();
while($row = $result->fetch_assoc()) {
$all_rows[$row["primary_key_of_your_table"]] = $row;
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
}
}
$sql=“从mpas中选择id、dba姓名、联系人、电话、确认码、物理地址、物理城市、物理州、物理邮政编码、urep”;
如果($result->num_rows>0){
$all_rows=array();
而($row=$result->fetch_assoc()){
$all_rows[$row[“您的_表的主键”]=$row;
echo“确认码:“.$row[“确认码”]。”
”;
}
}
因此,以后可以使用$all_行打印一些HTML,如下所示:
<?php
$codes = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$codes[] = $row['confirmation_code'];
echo "<strong>Confirmation Code: " . $row["confirmation_code"]. "<br></strong>";
.....
foreach ($codes as $c) {
echo "<input type='hidden' name='confc' value='".$c."'>" ;
}
?>
if (isset($all_rows[1])) { //Access the row with primary key 1 (as an example)
echo "<input type=\"hidden\" name='confc' value='".$all_rows[1]['confirmation_code']."'>" ;
}
if(isset($all_rows[1]){//使用主键1访问该行(例如)
回声“;
}
您是在同一个页面上执行此操作?还是在正在检索的页面上有连接对象?您是在while内部还是外部回显隐藏的输入?在代码中,我想使用相同的结果您是想在while循环中再次使用此结果,还是让while循环使用t完成的所有结果集Henry你在循环外做第二个回声,这意味着你只有最后一个“假”从fetch调用返回的值。这是一个奇怪的输入错误,不是我的否决票,但这是错误的@Fred ii-我没有注意到,我复制并粘贴了他的代码,谢谢though@Fred-嘿,你推荐什么php书籍吗?对不起,我不知道有什么书籍。我从网上把所有的东西都拿出来,然后放在书堆上;-)书籍是给孩子的:)我的fiNGER在索引页中的输入速度比剪纸搜索快,而且他们在@Fred ii-postsNot上也能找到更好的答案。但你也重复了OP的一个语法错误你也重复了OP的语法错误我认为这是故意的,这样他就可以看到[她]他的字段在不必检查来源的情况下工作。无论哪种方式,它对问题(或答案)都没有影响.是的,我知道。这个问题是一个常见的问题。许多人因此而获得了反对票,我可以诚实地说,这不是我。那些在试图帮助别人时给出答案的人,不应该因为别人的错误而受到惩罚。我修好它是为了让每个人都开心。好吧。好吧,OP似乎在某个地方跑了。我们来看看OP有什么需要做的说说这一切吧。
if (isset($all_rows[1])) { //Access the row with primary key 1 (as an example)
echo "<input type=\"hidden\" name='confc' value='".$all_rows[1]['confirmation_code']."'>" ;
}