简体   繁体   English

如何将php变量放入HTML表单

[英]How to put a php variable into an HTML form

I have code that queries an SQL database and displays all of the users in a table, that all works fine, from there you are able to click any user and it displays another table that contains all of their information, that also works fine. 我有查询SQL数据库并在一个表中显示所有用户的代码,它们运行正常,从那里您可以单击任何用户,并且它显示另一个包含其所有信息的表,该表也运行良好。

The problem I am running into here is on the page that displays the table of the selected users information. 我在这里遇到的问题在显示所选用户信息表的页面上。 On that page I have a button that allows the user information to be edited and update the DB. 在该页面上,我有一个按钮,可用于编辑用户信息和更新数据库。

Here is the code for the button: 这是按钮的代码:

 print "<form method='post' action='adminedituser.php'>
<input type='hidden' name='id' value='$id' />   
<input type='submit' value='Click here to edit this user' />
</form>";

When I click this it takes me to a page with an input box that needs to display the current username and allow the user to put in a new value and submit. 单击此按钮后,我将进入带有输入框的页面,该输入框需要显示当前用户名并允许用户输入新值并提交。

I'm trying to call a php variable inside of the HTML form I have: 我正在尝试在我拥有的HTML表单内调用php变量:

<?php

$id = filter_input(INPUT_POST, "id"); 
//Make db connection
$conn=mysql_connect("localhost","root","")or die(mysql_error());

//Step 2: select your db to use
mysql_select_db("final",$conn);

//Step 3: Write the sql that we want to run against the database
$result = mysql_query("select id, firstname, lastname, email, phone, username, password, favchar, bio from user where id=$id");

$username = $result["username"];

?>


<form method='post' id='myForm' name='input' action='adduser.php'>
Username: <input type='text' value="<?php echo ($username); ?>" id='uName' name='uName'>
<input type='submit'>
</form>

The problem is that the variable $username is not being displayed as the text box value. 问题是变量$ username没有显示为文本框值。

What am I doing wrong? 我究竟做错了什么?

Firstly, if I don't say it someone else is: Use mysqli instead of mysql . 首先,如果我不说其他人,那就是:使用mysqli代替mysql You can still use the procedural style, if you want (although I can say firsthand it is VERY easy to learn the object style), you just need to do a quick brush up on mysqli . 如果愿意,您仍然可以使用过程样式(尽管我可以直接说一遍,学习对象样式非常容易),您只需要快速了解mysqli Mysql is deprecated, so it is sort of useless to use. 不建议使用Mysql ,因此使用起来有点无用。 You'll have to learn it eventually, so when you're building a new program, it might be easiest. 您最终必须学习它,因此在构建新程序时,这可能是最简单的。

Moving on to what you are doing wrong, is that you have not actually fetched anything. 继续您做错的事情,就是您实际上没有获取任何东西。 MySQL will give you an object, and then you need to sort the rows. MySQL将给您一个对象,然后您需要对行进行排序。

So, what you need to do is pull mysql_fetch_array . 因此,您需要做的是pull mysql_fetch_array

You can do so like this: 您可以这样做:

while($row = mysql_fetch_array($result)) {
    $username = $row["username"];
}

The form code shuld be changed to: 表单代码应更改为:

<input type='hidden' name='id' value='<?= $id ?>' />

You forgot to "print" the $id value. 您忘记了“打印” $ id值。

and the code of adminedituser.php: 和adminedituser.php的代码:

$id = filter_input(INPUT_POST, "id"); 

Get the "id" field from POST request of your form. 从表单的POST请求中获取“ id”字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM