php - 将行中的数据放入无 php 站点进行编辑

标签 php mysql database forms

我的 php 代码有问题。第一个站点上有一个包含所有数据的列表。数据表正确显示。表的最后一列是一个指向下一页的链接,该链接应传递行的 ID。

这是链接:

print '<a href="editsr.php?id='.$row['id'].'" class="btn btn-default btn1">Ändern</a>';

但我现在无法将数据输入到输入字段中进行编辑。表单显示正确,但我在每个输入字段中都有此错误:


Warning: Illegal string offset 'vorname' in /home_pr5/d/e/deniseli.ch/htdocs/www.deniseli.ch/T .... tor/editsr.php on line 130
S

这是 editsr.php:

<?php
session_start();
require_once("inc/config.inc.php");
require_once("inc/functions.inc.php");

$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM users WHERE id = $id");
$result = $statement->execute(array('id' => ['id']));
$user = $statement->fetch();


include("templates/header.inc.php");

if(isset($_GET['save'])) {
	$save = $_GET['save'];
	
	if($save == 'personal_data') {
		$vorname = trim($_POST['vorname']);
		$nachname = trim($_POST['nachname']);
		$adresse = trim($_POST['adresse']);
		$plz = trim($_POST['plz']);
		$ort = trim($_POST['ort']);
		$geburtstag = trim($_POST['geburtstag']);
		$handy = trim($_POST['handy']);
		$liga = trim($_POST['liga']);
		$verein = trim($_POST['verein']);
		$bank = trim($_POST['bank']);
		$iban = trim($_POST['iban']);
		
		if($vorname == "" || $nachname == "" || $adresse == "" || $plz == "" || $ort == "" || $handy == "" || $liga == "" || $verein == "") {
			$error_msg = "Bitte alle Angaben ausfüllen.";
		} else {
			$statement = $pdo->prepare("UPDATE users SET vorname = :vorname, nachname = :nachname, adresse = :adresse, plz = :plz, ort = :ort, geburtstag = :geburtstag, handy = :handy, liga = :liga, verein = :verein, bank = :bank, iban = :iban, id = :id, updated_at=NOW() WHERE id = $id");
			$result = $statement->execute(array('vorname' => $vorname, 'nachname'=> $nachname,'adresse' => $adresse, 'plz' => $plz, 'ort' => $ort, 'geburtstag' => $geburtstag, 'handy' => $handy, 'liga' => $liga, 'verein' => $verein,'bank' => $bank, 'iban' => $iban, 'id' => $user['id'] ));
			
			$success_msg = "Daten erfolgreich gespeichert.";
		}
	} else if($save == 'email') {
		$passwort = $_POST['passwort'];
		$email = trim($_POST['email']);
		$email2 = trim($_POST['email2']);
		
		if($email != $email2) {
			$error_msg = "Die eingegebenen E-Mail-Adressen stimmten nicht überein.";
		} else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
			$error_msg = "Bitte eine gültige E-Mail-Adresse eingeben.";
		} else if(!password_verify($passwort, $user['passwort'])) {
			$error_msg = "Bitte korrektes Passwort eingeben.";
		} else {
			$statement = $pdo->prepare("UPDATE users SET email = :email WHERE id = $id");
			$result = $statement->execute(array('email' => $email));
				
			$success_msg = "E-Mail-Adresse erfolgreich gespeichert.";
		}
		
	} else if($save == 'passwort') {
		$passwortAlt = $_POST['passwortAlt'];
		$passwortNeu = trim($_POST['passwortNeu']);
		$passwortNeu2 = trim($_POST['passwortNeu2']);
		
		if($passwortNeu != $passwortNeu2) {
			$error_msg = "Die eingegebenen Passwörter stimmten nicht überein.";
		} else if($passwortNeu == "") {
			$error_msg = "Das Passwort darf nicht leer sein.";
		} else if(!password_verify($passwortAlt, $user['passwort'])) {
			$error_msg = "Bitte korrektes Passwort eingeben.";
		} else {
			$passwort_hash = password_hash($passwortNeu, PASSWORD_DEFAULT);
				
			$statement = $pdo->prepare("UPDATE users SET passwort = :passwort WHERE id = $id");
			$result = $statement->execute(array('passwort' => $passwort_hash));
				
			$success_msg = "Passwort erfolgreich gespeichert.";
			
		}
	}
}


?>

<div class="container main-container">

<h1>Schiedsrichter Profil bearbeiten</h1>

<?php 
if(isset($success_msg) && !empty($success_msg)):
?>
	<div class="alert alert-success">
		<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
	  	<?php echo $success_msg; ?>
	</div>
<?php 
endif;
?>

<?php 
if(isset($error_msg) && !empty($error_msg)):
?>
	<div class="alert alert-danger">
		<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
	  	<?php echo $error_msg; ?>
	</div>
<?php 
endif;
?>

<div>

  <!-- Nav tabs -->
  <ul class="nav nav-tabs" role="tablist">
    <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Übersicht</a></li>
    <li role="presentation"><a href="#data" aria-controls="home" role="tab" data-toggle="tab">Persönliche Daten</a></li>
    <li role="presentation"><a href="#email" aria-controls="profile" role="tab" data-toggle="tab">E-Mail</a></li>
    <li role="presentation"><a href="#passwort" aria-controls="messages" role="tab" data-toggle="tab">Passwort</a></li>
  </ul>

  <!-- Übersicht-->
  <div class="tab-content">
    <div role="tabpanel" class="tab-pane active" id="home">
    	<br>
    	<form action="?save=personal_data&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
    		<div class="form-group">
    			<label for=inputVorname class="col-sm-2 control-label">Vorname</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputNachname" name="nachname" type="text" value="<?php echo htmlentities($user['nachname']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputAdresse" class="col-sm-2 control-label">Adresse</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputAdresse" name="adresse" type="text" value="<?php echo htmlentities($user['adresse']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputPLZ" name="plz" type="text" value="<?php echo htmlentities($user['plz']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputOrt" class="col-sm-2 control-label">Ort</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputOrt" name="ort" type="text" value="<?php echo htmlentities($user['ort']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputGeburtstag" class="col-sm-2 control-label">Geburtsdatum</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputGeburtstag" name="geburtstag" type="text" value="<?php echo htmlentities($user['geburtstag']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputEmail" class="col-sm-2 control-label">E-Mail</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputEmail" name="email" type="email" value="<?php echo htmlentities($user['email']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputHandy" class="col-sm-2 control-label">Handy</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputHandy" name="handy" type="text" value="<?php echo htmlentities($user['handy']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputLiga" class="col-sm-2 control-label">Liga</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputLiga" name="liga" type="text" value="<?php echo htmlentities($user['liga']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputVerein" class="col-sm-2 control-label">Verein</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputVerein" name="verein" type="text" value="<?php echo htmlentities($user['verein']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputBank" class="col-sm-2 control-label">Bankname</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputBank" name="bank" type="text" value="<?php echo htmlentities($user['bank']); ?>" readonly>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputIban" class="col-sm-2 control-label">IBAN</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputIban" name="iban" type="text" value="<?php echo htmlentities($user['iban']); ?>" readonly>
    			</div>
    		</div>
    		
    	</form>
    </div>

  <!-- Persönliche Daten-->
    <div role="tabpanel" class="tab-pane" id="data">
    	<br>
    	<form action="?save=personal_data&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
    		<div class="form-group">
    			<label for="inputVorname" class="col-sm-2 control-label">Vorname</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" required>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputNachname" class="col-sm-2 control-label">Nachname</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputNachname" name="nachname" type="text" value="<?php echo htmlentities($user['nachname']); ?>" required>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputAdresse" class="col-sm-2 control-label">Adresse</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputAdresse" name="adresse" type="text" value="<?php echo htmlentities($user['adresse']); ?>" required>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputPLZ" class="col-sm-2 control-label">PLZ</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputPLZ" name="plz" type="text" value="<?php echo htmlentities($user['plz']); ?>" required>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputOrt" class="col-sm-2 control-label">Ort</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputOrt" name="ort" type="text" value="<?php echo htmlentities($user['ort']); ?>" required>
    			</div>
    		</div>
    		    		
    		<div class="form-group">
    			<label for="inputGeburtstag" class="col-sm-2 control-label">Geburtsdatum</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputGeburtstag" name="geburtstag" type="text" value="<?php echo htmlentities($user['geburtstag']); ?>" placeholder="01.01.2000">
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputHandy" class="col-sm-2 control-label">Handy</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputHandy" name="handy" type="text" value="<?php echo htmlentities($user['handy']); ?>" required>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputLiga" class="col-sm-2 control-label">Liga</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputLiga" name="liga" type="text" value="<?php echo htmlentities($user['liga']); ?>" required>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputVerein" class="col-sm-2 control-label">Verein</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputVerein" name="verein" type="text" value="<?php echo htmlentities($user['verein']); ?>" required>
    			</div>
    		</div>
    		    		
    		<div class="form-group">
    			<label for="inputBank" class="col-sm-2 control-label">Bankname</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputBank" name="bank" type="text" value="<?php echo htmlentities($user['bank']); ?>" placeholder="Postfinance">
    			</div>
    		</div>
    		
			<div class="form-group">
    			<label for="inputIban" class="col-sm-2 control-label">IBAN</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputIban" name="iban" type="text" value="<?php echo htmlentities($user['iban']); ?>" placeholder="CHxx xxxx xxxx xxxx xxxx x">
    			</div>
    		</div>

    		<div class="form-group">
			    <div class="col-sm-offset-2 col-sm-10">
			      <button type="submit" class="btn btn-primary">Speichern</button>
			      <a class="btn btn-danger" href='internal.php'>Abbrechen</a>
			    </div>
			</div>
    	</form>
    </div>
    
    <!-- Änderung der E-Mail-Adresse -->
    <div role="tabpanel" class="tab-pane" id="email">
    	<br>
    	<p>Zum Änderen deiner E-Mail-Adresse gib bitte dein aktuelles Passwort sowie die neue E-Mail-Adresse ein.</p>
    	<form action="?save=email&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
    		<div class="form-group">
    			<label for="inputPasswort" class="col-sm-2 control-label">Passwort</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputPasswort" name="passwort" type="password" required>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputEmail" class="col-sm-2 control-label">E-Mail</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputEmail" name="email" type="email" value="<?php echo htmlentities($user['email']); ?>" required>
    			</div>
    		</div>
    		
    		
    		<div class="form-group">
    			<label for="inputEmail2" class="col-sm-2 control-label">E-Mail (wiederholen)</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputEmail2" name="email2" type="email"  required>
    			</div>
    		</div>
    		
    		<div class="form-group">
			    <div class="col-sm-offset-2 col-sm-10">
			      <button type="submit" class="btn btn-primary">Speichern</button>
			      <a class="btn btn-danger" href='spielliste.php'>Abbrechen</a>
			    </div>
			</div>
    	</form>
    </div>
    
    <!-- Änderung des Passworts -->
    <div role="tabpanel" class="tab-pane" id="passwort">
    	<br>
    	<p>Zum Änderen deines Passworts gib bitte dein aktuelles Passwort sowie das neue Passwort ein.</p>
    	<form action="?save=passwort&id=<?php echo $_GET['id'] ?>" method="post" class="form-horizontal">
    		<div class="form-group">
    			<label for="inputPasswort" class="col-sm-2 control-label">Altes Passwort</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputPasswort" name="passwortAlt" type="password" required>
    			</div>
    		</div>
    		
    		<div class="form-group">
    			<label for="inputPasswortNeu" class="col-sm-2 control-label">Neues Passwort</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputPasswortNeu" name="passwortNeu" type="password" required>
    			</div>
    		</div>
    		
    		
    		<div class="form-group">
    			<label for="inputPasswortNeu2" class="col-sm-2 control-label">Neues Passwort (wiederholen)</label>
    			<div class="col-sm-10">
    				<input class="form-control" id="inputPasswortNeu2" name="passwortNeu2" type="password"  required>
    			</div>
    		</div>
    		
    		<div class="form-group">
			    <div class="col-sm-offset-2 col-sm-10">
			      <button type="submit" class="btn btn-primary">Speichern</button>
			      <a class="btn btn-danger" href='spielliste.php'>Abbrechen</a>
			    </div>
			</div>
    	</form>
    </div>
  </div>
</div>
</div>

<?php 
include("templates/footer.inc.php")
?>

更新:新代码editsr.php。与选项卡配合良好,仅在刷新站点后显示更改。

最佳答案

仔细看看你的代码

在顶部,您正在使用 $user 变量,如下所示

$id = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM users WHERE id = :id");
$result = $statement->execute(array('id' => ['id']));
$user = $statement->fetch(); // here

在 php 代码的底部,您再次使用 $user 变量,如下所示

$id = $_GET['id'];
$user = "SELECT * FROM users WHERE id = :id"; // here

?>

在您尝试获取的输入上,例如 htmlentities($user['vorname']);

<div class="form-group">
                <label for=inputVorname class="col-sm-2 control-label">Vorname</label>
                <div class="col-sm-10">
                    <input class="form-control" id="inputVorname" name="vorname" type="text" value="<?php echo htmlentities($user['vorname']); ?>" readonly>
                </div>
            </div>

这就是每次输入都会出错的问题:)

关于php - 将行中的数据放入无 php 站点进行编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46237019/

相关文章:

Php 按日期排序多个 xmlDoc

mysql - Knex NodeJS 如何选择每行具有最大日期的行

php - SQLSTATE[HY000] [1045] 使用 CakePHP 拒绝用户 'username' @'localhost' 的访问

php - select查询中的where条件

database - laravel 5 在运行时更改数据库名称

php - AJAX 之后 Jquery 不工作

php - 加载 ajax 内容后 jquery 工具提示不起作用

mysql - 为什么某些 Rails 项目使用多个具有不同扩展名的 database.yml 文件(mysql、postgresql 等)?

Mysql 一对一 vs null vs json

PHP - 优化查找数组中的最近点