QUERY 和 ECHO Data INTO HTML INPUT 字段 [英] QUERY AND ECHO Data INTO HTML INPUT fields
问题描述
我编写了一个脚本,简而言之,它应该从数据库中查询数据并将结果回显到 HTML 表单字段中.然而,我一直没有成功.请看下面的代码:
I've written a script that in short is supposed to query data from the database and echo a result into a HTML form field. However, I have been unsuccessful. Please see code below:
<?php
include("dbconfig.php");
$val = '6';
$result = mysqli_query("Select * from test where testid= '$val'");
$name = (mysqli_num_rows($result)==1) ? mysqli_fetch_assoc($result) : null;
if(is_array($name)){
?>
<html>
<body>
<form>
Name: <input type="text" id="firstname" value="<?php echo $name['firstname']; ?>"/>
</form>
<?php
} else {
echo "No such name exists";
}
?>
</body>
</html>
谁能告诉我我做错了什么.因为它不会在该领域产生任何影响,而且我觉得它很烦人,因为我遇到的大多数脚本都与这个非常相似.
Can someone please tell me what I'm doing wrong. Because it won't echo anything into the field and I find it rather annoying because majority of the scripts I've come across are quite similar to this one.
非常感谢您的帮助.
谢谢,
索海尔.
推荐答案
我已经测试了下面的,它工作正常.@Fred-ii- 为您提供了大量有用的信息,尤其是使用错误调试时 - 但您确实需要提供您缺少的连接对象.
I have tested the below and it works OK. @Fred-ii- gave you loads of good info, especially using error debugging - but you do need to supply the connection object which you were missing.
<?php
error_reporting( E_ALL );
include("conn.php");
$val = 6;
/* What is the name of the $connection object ? */
$result = mysqli_query( $conn, "Select * from `test` where `testid`='$val'" );
$name=( $result ) ? mysqli_fetch_assoc( $result ) : false;
?>
<html>
<head>
<title>Ya gotta have a title...</title>
</head>
<body>
<?php
if( !empty( $name ) ){
echo "
<form>
Name: <input type='text' id='firstname' value='{$name['firstname']}'/>
</form>";
} else {
echo "No such name exists";
}
?>
</
这篇关于QUERY 和 ECHO Data INTO HTML INPUT 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!