将 mySql 数据库中的数据加载到 HTML 文本框
Load Data from a mySql DB in to HTML textBox
我正在尝试从我页面中的数据库 table 加载数据。
我在下面报告的这个是一个示范性的例子,来解释我想如何实现它......
实际上我们正在谈论数据库的 100 个字段和 1000 个并传递 table ...
的代码行
<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$idCantiere = $_GET['idCantiere'];
$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
$result = mysqli_query($sql1, $conn);
$details = mysqli_fetch_array($result);
$savedNomeCantiere = $details["nomeCantiere"];
$savedCodiceCommessa = $details["codiceCommessa"];
$savedIndirizzoCantiere = $details["indirizzoCantiere"];
var_dump($details);
$result1 = $conn->query($sql1);
echo($nomeCantiere);
?>
<html>
<head>
</head>
<body>
<table width="300" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
</tr>
</table>
<br/>
</body>
</html>
我尝试使用这种类型的上传,将 "echo" 放入文本框的 "value" 中,但它不起作用。
此行用于通过页面重定向导出"id"。
$idCantiere = $_GET['idCantiere'];
如果我想尝试 var_dump($details)
它 return NULL
你的代码应该是这样的
<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$idCantiere = $_GET['idCantiere'];
$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '{$idCantiere'}";
$result = $conn->query($sql1);
$details = $result->fetch_array();
$savedNomeCantiere = $details["nomeCantiere"];
$savedCodiceCommessa = $details["codiceCommessa"];
$savedIndirizzoCantiere = $details["indirizzoCantiere"];
var_dump($details);
$result1 = $conn->query($sql1);
echo($nomeCantiere);
?>
<html>
<head>
</head>
<body>
<table width="300" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
</tr>
</table>
<br/>
</body>
</html>
其中一个问题是您传递的 $conn
和 $sql1
顺序错误。
$result = mysqli_query($sql1, $conn);
应该是 $result = mysqli_query($conn, $sql1);
if i want to try a var_dump($details) it return NULL
这正是您在使用前需要检查 $result
的原因。 mysqli_query
returns 如果成功则 object
或如果失败 false
- http://php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues.
你不需要在这里花哨,但你至少应该做一个完整性检查。
...
$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
$result = mysqli_query($sql1, $conn);
if ($result !== false) {
$details = mysqli_fetch_array($result);
...
此外,我必须指出 "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
非常危险,因为您无法控制 $idCantiere
。请查找 SQL 注入以及如何避免它。
我正在尝试从我页面中的数据库 table 加载数据。
我在下面报告的这个是一个示范性的例子,来解释我想如何实现它...... 实际上我们正在谈论数据库的 100 个字段和 1000 个并传递 table ...
的代码行<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$idCantiere = $_GET['idCantiere'];
$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
$result = mysqli_query($sql1, $conn);
$details = mysqli_fetch_array($result);
$savedNomeCantiere = $details["nomeCantiere"];
$savedCodiceCommessa = $details["codiceCommessa"];
$savedIndirizzoCantiere = $details["indirizzoCantiere"];
var_dump($details);
$result1 = $conn->query($sql1);
echo($nomeCantiere);
?>
<html>
<head>
</head>
<body>
<table width="300" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
</tr>
</table>
<br/>
</body>
</html>
我尝试使用这种类型的上传,将 "echo" 放入文本框的 "value" 中,但它不起作用。
此行用于通过页面重定向导出"id"。
$idCantiere = $_GET['idCantiere'];
如果我想尝试 var_dump($details)
它 return NULL
你的代码应该是这样的
<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$idCantiere = $_GET['idCantiere'];
$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '{$idCantiere'}";
$result = $conn->query($sql1);
$details = $result->fetch_array();
$savedNomeCantiere = $details["nomeCantiere"];
$savedCodiceCommessa = $details["codiceCommessa"];
$savedIndirizzoCantiere = $details["indirizzoCantiere"];
var_dump($details);
$result1 = $conn->query($sql1);
echo($nomeCantiere);
?>
<html>
<head>
</head>
<body>
<table width="300" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
</tr>
<tr>
<td>Cost</td>
<td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
</tr>
</table>
<br/>
</body>
</html>
其中一个问题是您传递的 $conn
和 $sql1
顺序错误。
$result = mysqli_query($sql1, $conn);
应该是 $result = mysqli_query($conn, $sql1);
if i want to try a var_dump($details) it return NULL
这正是您在使用前需要检查 $result
的原因。 mysqli_query
returns 如果成功则 object
或如果失败 false
- http://php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues.
你不需要在这里花哨,但你至少应该做一个完整性检查。
...
$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
$result = mysqli_query($sql1, $conn);
if ($result !== false) {
$details = mysqli_fetch_array($result);
...
此外,我必须指出 "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
非常危险,因为您无法控制 $idCantiere
。请查找 SQL 注入以及如何避免它。