我试图在文本框中插入一个数值,然后在点击提交时将该值添加到表格中的值。
例如,我有一个带有名为customers的表的数据库。 表客户具有名称和余额列。 我想在文本框中输入一个数字,然后在单击提交按钮时,我希望它将输入的值添加到余额列中的值,并在其中进行更新。 换句话说,如果数据库列余额的值为50.那么当我输入一个值时,假设为20,数据库中更新的余额将为70。
我试过这个,但我只将值增加1,因为我不知道如何将输入的文本框值输入变量。
<?php
session_start();
//if user is not logged in, do nothing
if(!isset($_SESSION['email']))
{
exit('Not logged in.');
}
try
{
$dbh = new PDO("mysql:host=localhost;dbname=classicmodels","root", NULL);
}
catch(PDOException $e)
{
exit('Database connection failed: ' . $e->getMessage());
}
$stmt = $dbh->prepare("UPDATE customers SET balance = balance + 1 WHERE email = :email");
$stmt->bindParam(':email', $_SESSION['email']) or exit("bind param failed.");
$stmt->execute() or exit("UPDATE failed.");
// loggedin.php
<?php
//if user is not logged in, then redirect to the login page
session_start();
if(!isset($_SESSION['email']))
{
header('Location: ./main.php');
}
//get the balance from database for logged in user
//connect to the database
try
{
$dbh = new PDO("mysql:host=localhost;dbname=classicmodels", "root", NULL);
}
catch (PDOException $e)
{
exit('Database Connection Failed: ' . $e->getMessage());
}
//retrieve the click count for the logged in user
$stmt = $dbh->prepare("SELECT balance FROM customers WHERE email = :email");
$stmt->bindParam(':email', $_SESSION['email']);
$stmt->execute() or exit("SELECT failed.");
//retrieve the firstname for logged in user
$stmt1 = $dbh->prepare("SELECT firstName FROM customers WHERE email = :email");
$stmt1->bindParam(':email', $_SESSION['email']);
$stmt1->execute() or exit("SELECT failed.");
//retrieve the lastname for logged in user
$stmt2 = $dbh->prepare("SELECT lastName FROM customers WHERE email = :email");
$stmt2->bindParam(':email', $_SESSION['email']);
$stmt2->execute() or exit("SELECT failed.");
//if there is no such user, then redirect to login page
if($stmt->rowCount() == 0)
{
header('Location: ./main.php');
exit();
}
//extract the balance
$row = $stmt->fetch() or exit("fetch failed.");
$balance = $row["balance"];
//extract the name
$row = $stmt1->fetch() or exit("fetch failed.");
$first = $row["firstName"];
//extract the name
$row = $stmt2->fetch() or exit("fetch failed.");
$last = $row["lastName"];
?>
<head>
<title>LAB 3 - HTML & Web Programming</title>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<!--<h1><center>LAB 3 - HTML & Web Programming</center></h1>-->
<body>
<table>
<tr>
<th colspan="6"><center>Banking Example</center></th>
</tr>
<tr>
<td>
Welcome,<br> <?php print($first) ?> <?php print($last) ?>
</td>
<td>
Your Balance is: $<?php print($balance) ?><br><br>
<form action="/deposit.php" method=post>
Deposit: <input type="text" name="DepositAmount" value=""><br>
<input type="submit" value="Submit">
</form>
<form action="/withdraw.php" method=post>
Withdraw: <input type="text" name="WithdrawAmount" value=""><br>
<input type="submit" value="Submit">
</form>
<form action = "logout.php" method = "post">
<input type = "submit" value = "Logout"/>
</form>
<button type="button" onclick="alert('Your Balance is: $<?php print($balance)?>')">Check Balance</button>
</td>
</tr>
</table>
</body>
<div id = "error_message"></div>
<script>
function record_click()
{
var httpRequest = new XMLHttpRequest();
if(!httpRequest)
{
alert('Browser not supported');
return;
}
httpRequest.onreadystatechange = function()
{
if(httpRequest.readyState === XMLHttpRequest.DONE)
{
if(httpRequest.status === 200)
{
if(httpRequest.responseText.length > 0)
{
document.getElementById('error_message').innerHTML = httpRequest.responseText;
}
}
else
{
alert('There was a problem with the request.');
return;
}
}
};
httpRequest.open('POST', 'https://cse405-luisgeesb.c9users.io/login/deposit.php');
httpRequest.send();
}
(function()
{
var count = <?php print($click_count)?>;
var counter = document.getElementById('counter');
var button = document.getElementById('button');
button.onclick = function()
{
counter.innerHTML = --count;
record_click();
}
})();
</script>
// deposit.php
<?php
session_start();
//if user is not logged in, do nothing
if(!isset($_SESSION['email']))
{
exit('Not logged in.');
}
try
{
$dbh = new PDO("mysql:host=localhost;dbname=classicmodels","root", NULL);
}
catch(PDOException $e)
{
exit('Database connection failed: ' . $e->getMessage());
}
$stmt = $dbh->prepare("UPDATE customers SET balance = balance + :DepositAmount WHERE email = :email");
//$stmt->bindParam(':email', $_SESSION['email']) or exit("bind param failed.");
$stmt->bindParam(':DepositAmount', $_POST['DepositAmount']) or exit("bind param failed.");
//$stmt->execute() or exit("UPDATE failed.");
$stmt->execute(array(':email'=> $_SESSION['email'],':DepositAmount' => $_POST['DepositAmount'])) or exit("UPDATE failed.");
?>
答案 0 :(得分:0)
// ...
$sql = "UPDATE customers
SET balance = balance + :transaction
WHERE email = :email";
$stmt = $dbh->prepare($sql);
$stmt->execute(array(
':email'=> $_SESSION['email'],
':transaction' => $_POST['transaction']
))
or exit("UPDATE failed.");
您使用<form>
<input type="text" name="transaction" />
使用完整的代码并且有点干净,你有:
<?php
//if user is not logged in, then redirect to the login page
session_start();
if(!isset($_SESSION['email']))
header('Location: ./main.php');
//get the balance from database for logged in user
//connect to the database
try {
$dbh = new PDO("mysql:host=localhost;dbname=classicmodels", "root", NULL);
}
catch (PDOException $e) {
exit('Database Connection Failed: ' . $e->getMessage());
}
try {
// if we have a Deposit
if (isset($_POST['DepositAmount'])) {
$stmt = $dbh->prepare(
"UPDATE customers
SET balance = balance + :DepositAmount
WHERE email = :email";
);
$stmt->execute(array(
':email'=> $_SESSION['email'],
':DepositAmount' => $_POST['DepositAmount']
));
}
// if we have a Withdraw
if (isset($_POST['WithdrawAmount'])) {
$stmt = $dbh->prepare(
"UPDATE customers
SET balance = balance - :WithdrawAmount
WHERE email = :email";
);
$stmt->execute(array(
':email'=> $_SESSION['email'],
':WithdrawAmount' => $_POST['WithdrawAmount']
));
}
// anyway, we get our data back
$stmt = $dbh->prepare(
"SELECT balance, firstName, lastName
FROM customers
WHERE email = :email"
);
$stmt->execute(array(':email' => $_SESSION['email']));
$row = $stmt->fetch();
} catch (Exception $e) { // error handle
exit('ERROR : '. $e->getMessage());
}
//if there is no such user, then redirect to login page
if(empty($row))
{
header('Location: ./main.php');
exit();
}
$balance = $row["balance"];
$first = $row["firstName"];
$last = $row["lastName"];
?>
<head>
<title>LAB 3 - HTML & Web Programming</title>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<!--<h1><center>LAB 3 - HTML & Web Programming</center></h1>-->
<body>
<table>
<tr>
<th colspan="6"><center>Banking Example</center></th>
</tr>
<tr>
<td>
Welcome,<br> <?php print($first) ?> <?php print($last) ?>
</td>
<td>
Your Balance is: $<?php print($balance) ?><br><br>
<form action="" method=post>
Deposit: <input type="text" name="DepositAmount" value=""><br>
<input type="submit" value="Submit">
</form>
<form action="" method=post>
Withdraw: <input type="text" name="WithdrawAmount" value=""><br>
<input type="submit" value="Submit">
</form>
<form action = "logout.php" method = "post">
<input type = "submit" value = "Logout"/>
</form>
<button type="button" onclick="alert('Your Balance is: $<?php print($balance)?>')">Check Balance</button>
</td>
</tr>
</table>
</body>
<div id = "error_message"></div>
<script>
function record_click()
{
var httpRequest = new XMLHttpRequest();
if(!httpRequest)
{
alert('Browser not supported');
return;
}
httpRequest.onreadystatechange = function()
{
if(httpRequest.readyState === XMLHttpRequest.DONE)
{
if(httpRequest.status === 200)
{
if(httpRequest.responseText.length > 0)
{
document.getElementById('error_message').innerHTML = httpRequest.responseText;
}
}
else
{
alert('There was a problem with the request.');
return;
}
}
};
httpRequest.open('POST', 'https://cse405-luisgeesb.c9users.io/login/deposit.php');
httpRequest.send();
}
(function()
{
var count = <?php print($click_count)?>;
var counter = document.getElementById('counter');
var button = document.getElementById('button');
button.onclick = function()
{
counter.innerHTML = --count;
record_click();
}
})();
</script>
答案 1 :(得分:-1)
我不是PDO的专家,我不知道这样的语法是否有效但提交表单时,检索表单的新值($ _GET或$ _POST)并使用该值而不是声明中的数字1 ......类似......
$new_value = $_GET['new_value_from_the_form'];
$stmt = $dbh->prepare("UPDATE customers SET balance = balance +
$new_value WHERE email = :email");