pdo检索数据并填充记录 [英] pdo to retrieve data and populate a record

查看:41
本文介绍了pdo检索数据并填充记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过有关PDO的问题用户将记录添加到数据库PDO ,现在我无法选择数据并将它们插入到html表单中,以便允许用户选择什么并因此将记录添加到db表中

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表示该代码区域无效:

The error Call to a member function execute() on a non-object means this area of the code is invalid:

$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()是一个函数,所以在=后面跟随它是没有意义的
  • 整理代码以提高可读性
    • Use double-quote if you want to use newlines
    • Know that prepare() is a function, so following it with = doesn't make sense
    • Tidy your code for readability
    • 这篇关于pdo检索数据并填充记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆