简体   繁体   English

如何在html输入文本中显示mysql数据?

[英]How to show mysql data in html input text?

I have created html file with generated text, and i want to populate fields like name and date from mysql database. 我用生成的文本创建了html文件,我想从mysql数据库中填充诸如名称和日期之类的字段。 The thing is i am trying to show data from mysql in html input text tag. 问题是我试图在html输入文本标签中显示来自mysql的数据。 But it only shows letter S. This is my php code: 但是它只显示字母S。这是我的php代码:

    <?php



    $db_host = ''; // Server Name
    $db_user = ''; // Username
    $db_pass = ''; // Password
    $db_name = ''; // Database Name

    $con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

    if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = "SELECT ime_prezime,datumpocetka,datumzavrsetka,broj_dana,sektor,
mesto,tip,obrazlozenje,vreme_slanja
FROM godisnji
WHERE id=74";
mysqli_close($con);
?>

And this is a part of my html code: 这是我的html代码的一部分:

<p style="text-align:justify">1. Zaposlenom <input  name="lname" type="text" value="<?php  echo $result['ime_prezime'];?>"/>
na poslovima $posao, se odobrava
korišćenje godišnjeg odmora za 2018. godinu, u trajanju od 20 radnih dana.</p>

Just because... 只是因为...

Now this is very basic and you should learn to use PDO or at least the mysqli class. 现在这是非常基础的,您应该学习使用PDO或至少使用mysqli类。 But for now... 但现在...

This is a little demonstration code... ( It may have bugs as I have not run it ) 这是一些演示代码...(可能有错误,因为我没有运行它)

<?php
// The Database Connection Stuff should be in it's own file
// and included in each file that requires a database connection.
$db_host = ''; // Server Name
$db_user = ''; // Username
$db_pass = ''; // Password
$db_name = ''; // Database Name
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


// Create the SQL
$sql = "SELECT ime_prezime,datumpocetka,datumzavrsetka,broj_dana,sektor,
mesto,tip,obrazlozenje,vreme_slanja
FROM godisnji
WHERE id=74";

// Run the SQL statement
$query = mysqli_query($con,$sql);
// Read back the ROW as an associative array
$result = mysqli_fetch_assoc($query);

// DEBUG to learn what you get  back and CHECK it's what you expect!
var_dump($result);
// If it exists...
echo ($result['ime_prezime']);

mysqli_close($con);

// Then Read up on how to use PDO or mysqli Class

And as a suggestion, due to the fact you won't always have a valid value you could use instead of... 并建议您,由于您并不总是拥有有效值,因此可以代替...

<?php  echo $result['ime_prezime'];?>

Try this 尝试这个

<?= isset($result['ime_prezime'])?$result['ime_prezime']:'';?>

The above tests whether $result['ime_prezime'] exists, and if it does it will print it, else it will print ''. 上面的命令测试$ result ['ime_prezime']是否存在,如果打印,将打印它,否则将打印''。

Look up "Ternary Operators in PHP" 查找“ PHP中的三元运算符”

<?php

$db_host = ''; // Server Name
$db_user = ''; // Username
$db_pass = ''; // Password
$db_name = ''; // Database Name

// Create connection
$conn = new mysqli($db_host, $db_user , $db_pass , $db_name );


if ($conn->connect_error)
{
echo "Failed to connect to MySQL: " . $conn->connect_error;
}

$result = "SELECT ime_prezime,datumpocetka,datumzavrsetka,broj_dana,sektor,
           mesto,tip,obrazlozenje,vreme_slanja FROM godisnji WHERE id=74";

$sqlQuery = $conn->query($result);

$value = $sqlQuery->fetch_assoc()['ime_prezime'];



echo "<p style='text-align:justify'>1. Zaposlenom <input 
        name='lname'type='text' value='".$value."'/></p>";

$conn->close();

?>

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

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