简体   繁体   English

在输入/回显行中标记PHP表单字段

[英]Labelling PHP form fields in the input/echo line

I have this code from my site http://www3.londonmet.ac.uk:8008/~iia0014/userdetails.php 我从我的网站http://www3.londonmet.ac.uk:8008/~iia0014/userdetails.php获得了这段代码

I wanted to know how to correctly format the form nicely giving names for the fields in the same line I echo the database rows so it is clear what is what. 我想知道如何正确地格式化表格,并在我回显数据库行的同一行中给出字段的名称,因此很清楚是什么。 For example, I tried: 例如,我尝试过:

<input value="<?php echo "ID: " . $row["id"]; ?>"><br>

What should I add or remove to make the above line work properly showing: ID: Some id here? 我应该添加或删除哪些内容才能使上述行正常显示:ID:此处有些ID?

but this concatenated the ID word with the row id itself, inserting the "ID: " inside the form and I wanted it to be outside nicely. 但这将ID字与行ID本身连接起来,在表单内插入“ ID:”,我希望它可以很好地放在外部。 I can add a table to the form and give a name for the th's... But is it the only way? 我可以在表格中添加表格并为其指定名称...但这是唯一的方法吗? Can I just do it the easy way, all in one line to make my code clearer? 我能以一种简单的方式做到这一点,使代码清晰吗? Plus, can I incorporate the delete and update buttons in an easier way as well, instead of writing a function separately, perhaps a new function on the same line? 另外,我是否也可以以一种更简单的方式合并删除和更新按钮,而不是单独编写一个函数,也许在同一行上编写一个新函数? I am looking for a cleaner code with the least number of lines. 我正在寻找行数最少的更干净的代码。

<?php session_start(); ?>
<?php include "includes/connection.php"; ?>

<?php

$query = mysql_query("SELECT id, username, password, email, name, aim, admin, time,          phone, address
FROM users 
WHERE username = '".$_SESSION['myusername']."' ");


while($row = mysql_fetch_array($query) ) 
{ 

?>

<input value="<?php echo $row["id"]; ?>"><br>
<input value="<?php echo $row["username"];  ?> "><br>
<input value="<?php echo $row['password'];  ?> "><br>
<input value="<?php echo $row['email'];  ?> "><br>
<input value="<?php echo $row['name'];  ?> "><br>
<input value="<?php echo $row['aim'];  ?> "><br>
<input value="<?php echo $row['admin'];  ?> "><br>
<input value="<?php echo $row['time'];  ?> "><br>
<input value="<?php echo $row['phone'];  ?> "><br>
<input value="<?php echo $row['address'];  ?> "><br>

<?php 
}
?>

You can do a few things: 您可以做一些事情:

For your initial query, 对于您的初始查询,

$myusername = $_SESSION['myusername'];
$query = mysql_query("SELECT id, username, password, email, name, aim, admin, time,          phone, address FROM users WHERE username = '$myusername' ");

This is not meant for a production environment, but since you're learning it's always good to have things separated so you can better understand what is going on. 这并不意味着要在生产环境中使用,但是由于您正在学习,最好将各个部分分开,这样您就可以更好地了解发生了什么。

If the user edits their information (update the db), you could make a form with a single button inside it, method=post and action=database.php?action=edit. 如果用户编辑其信息(更新数据库),则可以创建一个表单,其中带有一个按钮,即method = post和action = database.php?action = edit。 And inside database.php, 在database.php里面,

if(isset($_GET['action']) == 'edit') { 

// set all posted values to variables
// run the update query... UPDATE ... WHERE username = '$myusername'

} 

For the delete, make another form with another single button, similarly: action=database.php?action=delete. 对于删除,请使用另一个按钮创建另一个表单,类似地:action = database.php?action = delete。 And inside database.php, 在database.php里面,

if(isset($_GET['action']) == 'delete') { 

// run the delete query... DELETE FROM $tbl WHERE username = '$myusername'

} 

You don't have to put everything in the same line, less lines isn't always better. 您不必将所有内容都放在同一行,更少的行并不总是更好。
About the input field, your code should be something like: 关于输入字段,您的代码应类似于:

<label>ID</label>
<input type="text" name="id" value="<?php echo $row['id']; ?>">

And by the way, in php, single quotes are for strings, double quotes are for strings and variables(maybe not the right technical definition). 顺便说一句,在php中,单引号用于字符串,双引号用于字符串和变量(可能不是正确的技术定义)。 So when you concatenate, you should do: 'string '.$var; 因此,在连接时,您应该执行以下操作: 'string '.$var; instead of "string ".$var . 而不是"string ".$var
And when you are echoing, you should use commas to 'join' strings and variables, so you are parsing parameters to the echo function instead of concatenating. 在回显时,应使用逗号“联接”字符串和变量,以便将参数解析为echo函数,而不是进行串联。 This is faster because it outputs the data directly. 因为它直接输出数据,所以速度更快。 Unlike concatenating, which joins the string first and then outputs it. 与串联不同,串联是先连接字符串然后输出。 So it would be echo 'string ', $var . 因此它将是echo 'string ', $var
Also, when you are using double quotes, it's faster to use {$var} for variables. 另外,当您使用双引号时,对变量使用{$var}更快。 For example: 例如:

mysql_query("SELECT `name` FROM `table` WHERE `id`='{$_SESSION['id']}'");

Note that in mysql queries, fields and tables are written between grave accents ( ` ) , and values between single quotes ( ' ) . 请注意,在mysql查询中,字段和表写在重音(`)之间,值写在单引号(')之间

Now back to your code. 现在回到您的代码。 If all the input fields are going to be the same type, lets say type="text" , then you can do this: 如果所有输入字段都将是同一类型,那么说type="text" ,那么您可以这样做:

while ($row = mysql_fetch_array($query)) {
    foreach ($row as $key => $value) {
        echo
       "<div>
            <label>{ucfirst($key)}</label>
            <input type="text" name="{$key}" value="{$value}">
        </div>";
    }
}

ucfirst()

Returns a string with the first character of str capitalized, if that character is alphabetic. 如果字符串是字母,则返回字符串的第一个字符为大写。

And for the update and delete buttons you can put two buttons with different name attributes: 对于更新和删除按钮,您可以放置​​两个具有不同name属性的按钮:

 <input type="submit" name="update" value="Update"> <input type="submit" name="delete" value="Delete"> 

And then check if isset() . 然后检查isset()

 if (isset($_POST['update'])) { //update data } else if (isset($_POST['delete'])) { //delete data } 

And always remember to sanitize user inputs: 并始终记得清理用户输入:

 function sanitize($str) { return mysql_real_escape_string(htmlspecialchars(trim(stripslashes($str)))); } 

And instead of sanitizing one by one you can: 而不是一个个地消毒,您可以:

 $data = array_map('sanitize', $_POST); 

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

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