【问题标题】:pdo to retrieve data and populate a recordpdo 检索数据并填充记录
【发布时间】:2015-07-27 10:35:28
【问题描述】:

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

<?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>

我正在处理的错误如下:

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

【问题讨论】:

  • 在 HTML 输出开始之前不要关闭您的 PHP 标记,否则您将遇到“标头已发送”错误。

标签: php mysql pdo


【解决方案1】:

错误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() 是一个函数,所以在它后面加上= 没有意义
  • 整理代码以提高可读性

【讨论】:

  • 查询下方的else 条件呢
  • @Riccardo990 我不确定你在问什么,但看起来你应该使用数组而不是大量变量。
  • 所以您的建议是消除else
  • @Riccardo990 else 通常是个好主意——如果您还有其他问题,请开始一个新线程。如果我的答案修复了执行错误,请将此答案标记为已接受。
  • 我会尝试你的答案,并在几分钟内尽快让你
【解决方案2】:

您没有正确使用准备好的语句。试试这个:

   <?php 

   $id = $_GET['id'];

   $sth = $dbh->prepare("SELECT
        nome, cognome, indirizzo, civico, citta,
        prov
    FROM
        tagesroma
    WHERE
        id =  :id");

   $sth->bindParam(":id",$id,PDO::PARAM_INT);


  $sth->execute();


   ?>

【讨论】:

  • 不需要创建$id 变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
相关资源
最近更新 更多