gpt4 book ai didi

php - pdo 检索数据并填充记录

转载 作者:行者123 更新时间:2023-11-30 22:38:52 26 4
gpt4 key购买 nike

I have already asked a question about PDO user add records to database PDO, now I am unable to select data and insert them into a html form in order to allow a user what to choose and as a consequence to add record into a db table

<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

try {
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to database<br />';
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
<?php
if ($_GET['action'] == 'edit') {
//retrieve the record's information
$sth = $dbh->prepare = 'SELECT
nome, cognome, indirizzo, civico, citta,
prov
FROM
tagesroma
WHERE
id = ' . $_GET['id'];
$sth = $dbh->execute();
extract($sth = $dbh->fetch());
} else {
//set values to blank
$nome = '';
$cognome = '';
$indirizzo = '';
$civico = 0;
$citta = '';
$prov = '';
}
?>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo ucfirst($_GET['action']); ?> Tages</title>
<style type="text/css">
<!--
#error { background-color: #600; border: 1px solid #FF0; color: #FFF;
text-align: center; margin: 10px; padding: 10px; }
-->
</style>
</head>
<body>
<?php
if (isset($_GET['error']) && $_GET['error'] != '') {
echo '<div id="error">' . $_GET['error'] . '</div>';
}
?>
<form action="commit.php?action=<?php echo $_GET['action']; ?>&type=tages"
method="post" accept-charset="UTF-8">
<table>
<tr>
<td>Nome</td>
<td><input type="text" name="nome" value="<?php echo $nome; ?>"/></td>
</tr><tr>
<td>Cognome</td>
<td><select name="cognome"></td>
<?php
//seleziona il tipo di cognome
$sth = $dbh->prepare = 'SELECT
cognome
FROM
tagesroma';
$sth->execute();
//popola con i risultati
while ($row = $sth->fetch()) {
foreach ($dbh->$row as $value) {
if ($row['id'] == $cognome) {
echo '<option value="' . $row['id'] .
'" selected="selected">';
} else {
echo '<option value="' . $row['id'] . '">';
}
}
}
?>
</select></td>
</tr><tr>
<td colspan="2" style="text-align: center;">
<?php
if ($_GET['action'] == 'edit') {
echo '<input type="hidden" value="' . $_GET['id'] . '" name="id" />';
}
?>
<input type="submit" name="submit"
value="<?php echo ucfirst($_GET['action']); ?>" />
</td>
</tr>
</table>
</form>
</body>
</html>

the error I am dealing with is the following:

Fatal error: Call to a member function execute() on a non-object on line 76

最佳答案

错误 Call to a member function execute() on a non-object 意味着这部分代码是无效的:

$sth = $dbh->prepare = 'SELECT
nome, cognome, indirizzo, civico, citta,
prov
FROM
tagesroma
WHERE
id = ' . $_GET['id'];
$sth = $dbh->execute();

正确的做法是:

$sth = $dbh->prepare("
SELECT nome, cognome, indirizzo, civico, citta, prov
FROM tagesroma
WHERE id = ?
");
$sth->execute(array($_GET['id']));
  • 如果要使用换行符,请使用双引号
  • 知道 prepare() 是一个函数,所以在它后面加上 = 没有意义
  • 整理代码以提高可读性

关于php - pdo 检索数据并填充记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31650860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com