我正在使用PHP的网站上工作,我希望在单击按钮时更新数据库。但由于某些原因,无论何时加载页面,代码都会运行,我不希望这样,因为它可能会弄乱整个代码。如何让脚本自动停止运行?
<?php
ob_start();
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user'])) {
header("Location: index.php");
exit;
}
$condition = empty($_POST['sender']) || empty($_POST['reciever']);
if ($condition) {
die; // if your post data is empty PHP will no longer be executed
}
$res= "SELECT * FROM users WHERE userId=".$_SESSION['user'];
$mysqli->query($con, $res); // you are doing nothing with it in your code, why?
$name = $_POST['sender'];
$name = $_POST['reciever'];
$query = "UPDATE users SET userCoins = userCoins + 1 WHERE userName='Morgan'";
$res = $mysqli->query($con, $query);
if ($res) {
$error = "Success!";
} else {
$error = "Something Went Wrong!";
}
?>
<!DOCTYPE html>
<html>
<?php header("Access-Control-Allow-Origin: http://www.py69.esy.es"); ?>
<head>
<title>ServiceCoin</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="scripts/home/index.css" />
</head>
<body>
<ul>
<li><a href="#" class="a">ServiceCoin.com(image)</a></li>
<li><a href="logout.php?logout" class="a">Sign Out</a></li>
<li><a href="#" class="a">Contact</a></li>
<li><a href="#" class="a">Get Service Coins</a></li>
<li><a href="#" class="a">News</a></li>
<li><a href="settings.php" class="a">Settings</a></li>
<li><a href="#" class="a">Referrals</a></li>
<li><a href="service.php" class="a">Services</a></li>
<li><a href="home.php" class="a">Home</a></li>
</ul>
<br /><br />
<center>
<h3>Welcome, <?php echo $userRow['userName']; ?>. You Currently Have <span id="services"><?php echo $userRow['userCoins']; ?></span> Service Coins</h3>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
<input type="text" name="sender" class="form-control" placeholder="Enter Your Wallet Key" value="<?php echo $row['userCoins']; ?>" maxlength="15" />
<span class="text-danger"><?php echo $error; ?></span>
</div>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicons glyphicons-lock"></span></span>
<input type="text" name="reciever" class="form-control" placeholder="Enter The Recievers Wallet Key" value="<?php echo $row['userCoins']; ?>" maxlength="15" />
<span class="text-danger"><?php echo $error; ?></span>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-block btn-primary" name="send">Sign Up</button>
</div>
</form>
</center>
</body>
</html>
<?php ob_end_flush(); ?>
更新
我的页面现在完全是白色的。
class Hello_World
{
public:
void hello(size_t number){
cout << "hello " << number << endl;
}
};
template<size_t SIZE, class T>
void foo()
{
T t;
t.hello(SIZE);
foo<SIZE-1, Hello_World>();
}
template<class T>
void foo<0,T>()
{
cout << "end." << endl;
}
int main()
{
foo<4,Hello_World>();
}
答案 0 :(得分:1)
您应该使用MySQLi
。关于mysql_*
为什么不能阅读here
<强>解决方案强>
您的代码如下所示:
$mysqli = new mysqli("localhost", "my_user", "my_password", "table_name"); // here you will need your connection data, you can store it in dbconnect.php.
// if session is not set this will redirect to login page
if(!isset($_SESSION['user'])) {
header("Location: index.php");
exit;
}
$condition = empty($_POST['sender']) || empty($_POST['reciever']);
if (!$condition) {
$res= "SELECT * FROM users WHERE userId=".$_SESSION['user'];
$mysqli->query($res); // you are doing nothing with it in your code, why?
$name = $_POST['sender'];
$reciever = $_POST['reciever'];
$query = "UPDATE users SET userCoins = userCoins + 1 WHERE userName='Morgan'";
$res = $mysqli->query($query);
if ($res) {
$error = "Success!";
} else {
$error = "Something Went Wrong!";
echo "Error: ".$mysqli->error; // here you can check your errors
}
}
<强>手册强>
有关MySQLi
的更多信息,请参阅PHP Manual