gpt4 book ai didi

php - 如何将 HTML 表单中的值添加到数据库中的值

转载 作者:行者123 更新时间:2023-11-29 13:56:14 25 4
gpt4 key购买 nike

我正在尝试创建一个表单,以允许用户将表单中的数据更新为数据库中的现有金额。这是我到目前为止所拥有的,它的值(value)似乎翻了一番。我想我需要从数据库中提取值,然后从表单中添加数据。

<?php 
$username = "username";
$password = "password";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "<font face=tahoma color=#ff000><b>Connected to MySQL</b></font><br><br>";

//select a database to work with
$selected = mysql_select_db("pdogclan_points",$dbhandle)
or die("Did this change");

// Formulate Query
$_POST["filter"];
$memid = mysql_real_escape_string($_POST["Member_ID"]);
$query = sprintf("SELECT Member_ID, Bank, Reward_1, Reward_2, Reward_3 FROM Points_Rewards WHERE Member_ID = '$memid'") or die("Could Not Formulate the Query");

//execute the SQL query and return records
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

//fetch tha data from the database
while ($row = mysql_fetch_array($result))
echo "<table width=750 cellspacing=2 cellpadding=2 border=2>
<tr>
<td bgcolor=#000000 width=150><font face=tahoma color=white>ID: {$row['Member_ID']}</font></td>".
"<td width=150><font face=tahoma>Bank: {$row['Bank']}</td>".
"<td width=150><font face=tahoma>Reward 1: {$row['Reward_1']}</td>".
"<td width=150><font face=tahoma>Reward 2: {$row['Reward_2']}</td> ".
"<td width=150><font face=tahoma>Reward 3: {$row['Reward_3']}</td>
</tr>
</table><br></font>";//display the results
// Formulate Update Query
$_POST["submit"];
$memid = mysql_real_escape_string($_POST["Member_ID"]);
$query = sprintf("SELECT Member_ID, Bank, Reward_1, Reward_2, Reward_3 FROM Points_Rewards WHERE Member_ID = '$memid'") or die("Could Not Formulate the Query");

while ($row = mysql_fetch_array($result))
{
$bankdb = $row['Bank'];
$reward1db = $row['Reward_1'];
$reward2db = $row['Reward_2'];
$reward3db = $row['Reward_3'];
}
echo $bank;
echo $reward1;
echo $reward2;
echo $reward3;
$memid = mysql_real_escape_string($_POST["Member_ID"]);
$bank = $_POST['bank'];
$reward1 = $_POST['reward1'];
$reward2 = $_POST['reward2'];
$reward3 = $_POST['reward3'];



$query = "UPDATE Points_Rewards Set Bank = ('$bank' + '$bankdb'), Reward_1 = ('$reward1' + '$reward1'), Reward_2 = ('$reward2' + '$reward2'), Reward_3 = ('$reward3' + '$reward3') WHERE Member_ID = '$memid'";
$result = mysql_query($query) or die(mysql_error());



if(mysql_query($query)){
echo "updated";}
else{
echo "fail";}

//close the connection
mysql_close($dbhandle);
?>

最佳答案

只需使用基本 HTML 创建一个表单,将从数据库获取的数据存储在 PHP 变量中,然后使用 PHP 标签显示该数据,如下所示:

<form action="..." method="post" >
<?php
$memid = mysql_real_escape_string($_POST["Member_ID"]);
$query = sprintf("SELECT Member_ID, Bank, Reward_1, Reward_2, Reward_3 FROM Points_Rewards WHERE Member_ID = '$memid'") or die("Could Not Formulate the Query");

while ($row = mysql_fetch_array($result))
{
?>
<input type="text" name="r1" value="<?php echo $row['Reward_1']; ?>" /> ;
<input type="text" name="r2" value="<?php echo $row['Reward_2']; ?>" /> ;
<input type="text" name="r3" value="<?php echo $row['Reward_3']; ?>" /> ;
...
<?php
}
?>

...
</form>

关于php - 如何将 HTML 表单中的值添加到数据库中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15823105/

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