简体   繁体   English

PHP + HTML:动态创建表单

[英]PHP + HTML: create a form dynamically

I'm trying to creat a form dynamically depending on the number of rows of a table in a database. 我试图根据数据库中表的行数动态创建表单。 I tried this and it's nor working: 我尝试了这个,但没有用:

require_once('mysqli_connect.php');

//I select the colum w_spanish from the table selected by the user
$q="SELECT w_spanish FROM ".$_GET['name'];

$r=@mysqli_query($dbc, $q);

echo '<FORM METHOD="POST" ACTION="Correction.php">';
echo '<TABLE BORDER="1">';

//Here is where I generate dinamically a table that can be filled by user
while ($row=mysqli_fetch_array($r, MYSQLI_ASSOC)){

$aux=$row['w_spanish'];
echo '<TR><TD>'.$aux.'</TD><TD><INPUT TYPE="TEXT" NAME="Sol_'.$aux.'" SIZE="20"></TD></TR>';

}

echo '</TABLE>';
echo '<P><INPUT TYPE="SUBMIT" VALUE="Submit" ></P></FORM>'; 


mysqli_close($dbc);

So when I press submit, the information is not sent to "Correction.php", and I think it's because I creating the HTML form inside php code. 因此,当我按Submit时,信息不会发送到“ Correction.php”,这是因为我在php代码中创建了HTML表单。 How could I do it right?? 我该怎么办?

First off - remove the @ from the @mysqli statement as it is masking any errors that maybe happening. 首先-从@mysqli语句中删除@,因为它掩盖了可能发生的任何错误。

Secondly take the generated code and paste it into http://validator.w3.org/#validate_by_input and see if there are any HTML errors and adjust where necessary. 其次,将生成的代码粘贴到http://validator.w3.org/#validate_by_input中 ,查看是否存在HTML错误,并在必要时进行调整。

Thirdly, since the user can select which table to read then your data needs to be super-sanitised as you certainly don't want sql injection attacks here. 第三,由于用户可以选择要读取的表,因此您的数据需要进行超级清理,因为您当然不希望在此处进行sql注入攻击。

The problem may be the query you are running. 问题可能出在您正在运行的查询。 Without knowing more information, my guess would be your query isn't getting anything. 在不了解更多信息的情况下,我猜测您的查询没有任何结果。 Try dumping the row in each iteration and see what spits out. 尝试在每次迭代中转储该行,看看会吐出什么。 You may be looking for something like: 您可能正在寻找类似的东西:

$q="SELECT w_spanish FROM tableName WHERE name = " . $_GET['name'];

If that's not it, it could also be the fact that since you are only grabbing one column from the database, you don't need access the information with $aux=$row['w_spanish']; 如果不是这样,也可能是这样的事实,因为您只从数据库中获取一列,因此不需要使用$aux=$row['w_spanish'];访问信息$aux=$row['w_spanish']; . You can just use: 您可以使用:

$aux=$row;

That I'm not 100% on though. 那我不是100%。 Try dumping each row with var_dump() and see what pops out. 尝试使用var_dump()转储每一行,然后查看弹出的内容。

First declare $row , then use a do-while loop. 首先声明$row ,然后使用do-while循环。

$row = mysqli_fetch_array($r, MYSQLI_ASSOC) do{  
    $aux=$row['w_spanish']; 
    echo '<TR><TD>'.$aux.'</TD><TD><INPUT> TYPE="TEXT"NAME="Sol_'.$aux.'" SIZE="20"></TD></TR>';
 }while ($row=mysqli_fetch_array($r, MYSQLI_ASSOC))

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

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