gpt4 book ai didi

PHP 表单更新页面

转载 作者:行者123 更新时间:2023-11-29 12:30:54 26 4
gpt4 key购买 nike

我正在尝试为我的表单应用程序创建一个更新页面,以便用户可以更改表单中的内容。问题是,我不断收到错误,例如:

注意:第 72 行未定义索引

$PersonId = $_GET['PersonId'];

<?php


function connect_db() {


$mysqli = new mysqli('localhost', 'root', '', 'db_lab2');


if (!$mysqli->set_charset("utf8")) {
echo "Fel vid inställning av teckentabell utf8: %s\n". $mysqli->error;
} else {

}

if ($mysqli->connect_errno) {
echo "Misslyckades att ansluta till MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

return $mysqli;
}


?>
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>

<?php

if(isset($_POST['spara'])){


$PersonId = $_POST['PersonId'];
$Fnamn = $_POST['Fnamn'];
$Enamn = $_POST['Enamn'];
$Gatuadress = $_POST['Gatuadress'];
$Postnummer = $_POST['Postnummer'];
$Postadress = $_POST['Postadress'];
$Telefon = $_POST['Telefon'];
$Epost = $_POST['Epost'];

$sql = "UPDATE person SET Fnamn='$Fnamn', Enamn='$Enamn', Gatuadress='$Gatuadress', Postnummer='$Postnummer', Postadress='$Postadress',
Telefon='$Telefon', Epost='$Epost' WHERE PersonId=$PersonId";


if($mysqli = connect_db()) {

$mysqli->query($sql);
print_r($mysqli->error);
}




echo "Personen är nu ändrad<br />";
echo "<a href='person.php'>Tillbaka till listningssidan</a>";
}

else{


$PersonId = $_GET['PersonId'];


$sql = "SELECT * FROM person WHERE PersonId=$PersonId";


if($mysqli = connect_db()) {

$result = $mysqli->query($sql);
print_r($mysqli->error);
}
$myRow = $result->fetch_array()

?>

<form action="update_person.php?PersonId=<?php echo $PersonId ?>" method="post">
<table cellpadding="3" cellspacing="0" border="1">
<tr><td>Person</td><td><input type="text" name="PersonId" value="<? echo $myRow[0]; ?>" /></td></tr>
<tr><td>Förnamn</td><td><input type="text" name="Fnamn" value="<? echo $myRow[1]; ?>" /></td></tr>
<tr><td>Efternamn</td><td><input type="text" name="Enamn" value="<? echo $myRow[2]; ?>" /></td></tr>
<tr><td>Gatuadress</td><td><input type="text" name="Gatuadress" value="<? echo $myRow[3]; ?>" /></td></tr>
<tr><td>Postnummer</td><td><input type="text" name="Postnummer" value="<? echo $myRow[4]; ?>" /></td></tr>
<tr><td>Postadress</td><td><input type="text" name="Postadress" value="<? echo $myRow[5]; ?>" /></td></tr>
<tr><td>Telefon</td><td><input type="text" name="Telefon" value="<? echo $myRow[6]; ?>" /></td></tr>
<tr><td>Epost</td><td><input type="text" name="Epost" value="<? echo $myRow[7] ?>" /></td></tr>
<tr><td colspan="2"><input type="submit" name="spara" value="Spara" />
</table>
</form>
<?php
}
?>


</body>
</html>

最佳答案

<?php
function connect_db() {
$mysqli = new mysqli('localhost', 'root', '', 'db_lab2');

if (!$mysqli->set_charset("utf8")) {
echo "Fel vid inställning av teckentabell utf8: %s\n". $mysqli->error;
}

if ($mysqli->connect_errno) {
echo "Misslyckades att ansluta till MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

return $mysqli;
}


?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
if ( isset ( $_POST['spara'] ) ) {
$PersonId = $_POST['PersonId'];
$Fnamn = $_POST['Fnamn'];
$Enamn = $_POST['Enamn'];
$Gatuadress = $_POST['Gatuadress'];
$Postnummer = $_POST['Postnummer'];
$Postadress = $_POST['Postadress'];
$Telefon = $_POST['Telefon'];
$Epost = $_POST['Epost'];

if ( $mysqli = connect_db() ) {
$sql = "UPDATE person SET Fnamn='$Fnamn', Enamn='$Enamn', Gatuadress='$Gatuadress', Postnummer='$Postnummer', Postadress='$Postadress', Telefon='$Telefon', Epost='$Epost' WHERE PersonId=". $mysqli->escape_string($PersonId);
$mysqli->query ( $sql );
print_r ( $mysqli->error );
}

echo "Personen är nu ändrad<br />";
echo "<a href='person.php'>Tillbaka till listningssidan</a>";
} else {
if ( isset ( $_GET['PersonId'] ) ) {
if ( $mysqli = connect_db() ) {
$PersonId = $_GET['PersonId'];
$sql = "SELECT * FROM person WHERE PersonId=$PersonId";
$result = $mysqli->query($sql);
print_r ( $mysqli->error );
$myRow = $result->fetch_array();
}
}
?>

<form action="update_person.php?PersonId=<?php echo $PersonId ?>" method="post">
<table cellpadding="3" cellspacing="0" border="1">
<tr><td>Person</td><td><input type="text" name="PersonId" value="<? echo $myRow[0]; ?>" /></td></tr>
<tr><td>Förnamn</td><td><input type="text" name="Fnamn" value="<? echo $myRow[1]; ?>" /></td></tr>
<tr><td>Efternamn</td><td><input type="text" name="Enamn" value="<? echo $myRow[2]; ?>" /></td></tr>
<tr><td>Gatuadress</td><td><input type="text" name="Gatuadress" value="<? echo $myRow[3]; ?>" /></td></tr>
<tr><td>Postnummer</td><td><input type="text" name="Postnummer" value="<? echo $myRow[4]; ?>" /></td></tr>
<tr><td>Postadress</td><td><input type="text" name="Postadress" value="<? echo $myRow[5]; ?>" /></td></tr>
<tr><td>Telefon</td><td><input type="text" name="Telefon" value="<? echo $myRow[6]; ?>" /></td></tr>
<tr><td>Epost</td><td><input type="text" name="Epost" value="<? echo $myRow[7]; ?>" /></td></tr>
<tr><td colspan="2"><input type="submit" name="spara" value="Spara" />
</table>
</form>
<?php
}
?>
</body>
</html>

应该可以。

关于PHP 表单更新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542065/

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