簡體   English   中英

使用PHP HTML表單的MySQL表更新無法正常工作

[英]MySQL Table Update with PHP HTML Form isn't Working

我正在嘗試使用PHP表單更新mySQL表。 當前,我已經設置了所有代碼,但是當更新表數據age時,它將表中的所有年齡設置為“ 0”。 我不知道為什么,但是任何指導都會受到高度贊賞。 謝謝。

凱爾西

<?php
    $hostname = "---------";//host name
    $dbname = "-------";//database name
    $username = "-------------";//username you use to login to php my admin
    $password = "--------";//password you use to login

    //CONNECTION OBJECT
    //This Keeps the Connection to the Databade
    $conn = new MySQLi($hostname, $username, $password, $dbname) or die('Can not connect to database')      
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php

$id=$_GET['FirstName'];

//Create a query
$sql = "SELECT * FROM Persons";
//submit the query and capture the result
$result = $conn->query($sql) or die(mysql_error());
$query=getenv(QUERY_STRING);
parse_str($query);
?>
<h2>Update Record <?php echo $sql;?></h2>
<form action="" method="post">
<?php


    while ($row = $result->fetch_assoc()) {?>

<table border="0" cellspacing="10">
<tr>
<td>age:</td> <td><input type="text" name="Age" value="<?php echo $row['Age']; ?>"></td>
</tr>
<tr>
<td><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"></td>
</tr>
</table>
<?php   }
    ?>
</form>
<?php
    if(isset($_POST['Submit'])){//if the submit button is clicked

    $sql="UPDATE Persons SET Age='".$_POST['Age']."'";
    $conn->query($sql) or die("Cannot update");//update or error
    }
?>


</body>
</html>

現在,按照其書面說明, UPDATE查詢將更新整個Persons表,而不是單個記錄。

UPDATE Persons SET Age=15 WHERE id = 5將僅更新一條記錄,該記錄與整個表的值相對應。

security risk) to put raw post values directly into an SQL string. 另外,將原始發布值直接放入SQL字符串也不是一件好事(又有安全風險)。 在將值放入數據庫查詢之前,應始終保持頭腦清醒。

在語句本身內部添加POST的好方法。

嘗試這個:

<?php
if(isset($_POST['Submit'])){//if the submit button is clicked

$age = $_POST['Age'];

$sql="UPDATE Persons SET Age='".$age."' WHERE firstname ='".$id."'";
$conn->query($sql) or die("Cannot update");//update or error
}
?>

您的頁面令人困惑。 您甚至沒有正確輸出結果。 嘗試這個:

<?php
$hostname = "---------";//host name
$dbname = "-------";//database name
$username = "-------------";//username you use to login to php my admin
$password = "--------";//password you use to login

//CONNECTION OBJECT
//This Keeps the Connection to the Databade
$conn = new MySQLi($hostname, $username, $password, $dbname) or die('Can not connect to database')      
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>

<?php

//$id=$_GET['FirstName'];

//Create a query
$sql = "SELECT * FROM Persons";
//submit the query and capture the result
$result = $conn->query($sql) or die(mysql_error());
$query=getenv(QUERY_STRING);
parse_str($query);
?>
<h2>Update Record <?php echo $sql;?></h2>
<form action="" method="post" enctype='multipart/form-data'>
<?php


while ($row = $result->fetch_assoc()) {?>

<table border="0" cellspacing="10">
<tr>
<td>Age:</td>
<td><?php echo $row['Age'];?></td> 
<td><?php echo $row['FirstName'];?></td>
<td><input type="text" name="Age"></td>
<td><input type=hidden" name="firstName" value="<?php echo $row['FirstName'];?>"></td>

</tr>

<tr>
<td><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"></td>
</tr>
</table>
<?php   }
?>
</form>

<?php
if(isset($_POST['Submit'])){//if the submit button is clicked
$age = $_POST['Age'];
$id = $_POST['firstName'];

$sql="UPDATE Persons SET Age='".$age."' WHERE firstname ='".$id."'";
$conn->query($sql) or die("Cannot update");//update or error
}
?>


</body>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM