PHP 更新/使用 MySQL 进行行编辑
所以我一直在开发一款国家模拟器游戏。它将在线,我在 CSS 和 HTML 方面有很多经验,但在 PHP 方面没有。我在 Java 和 C++ 方面也有很多经验。我已经建立了数据库,并使用一个功能正常的登录和注册系统,以及一个显示国家当前统计数据的功能会员索引。但是,我完全无法弄清楚如何制作一个可以更改用户信息(例如国家名称和当前可用资金)的脚本。这是我微弱的尝试:(它试图将 10 座建筑物添加到总数中。SESS_ 是会话中的变量)
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$result = mysql_query("UPDATE members SET buildings ='10' WHERE login='SESS_NATION'")
or die(mysql_error());
$result = mysql_query("SELECT * FROM members WHERE login='SESS_NATION'")
or die(mysql_error());
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
echo $row['login']." - ".$row['Buildings']. "<br />";
?>
So I've been working on a nation simulator game. Its gonna be online and I have a lot of experience with CSS and HTML, but not with PHP. I also have a lot of experience with Java and C++. I have the data base setup and working with a functioning login and register system, along with a functioning member index displaying the nations current stats. However, I have been completely unable to figure out how to make a script that would change user information such as nation name and current funds available. This is my feeble attempt at one: (It's attempting to add 10 buildings to the total. SESS_ are variables in a session)
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$result = mysql_query("UPDATE members SET buildings ='10' WHERE login='SESS_NATION'")
or die(mysql_error());
$result = mysql_query("SELECT * FROM members WHERE login='SESS_NATION'")
or die(mysql_error());
// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
echo $row['login']." - ".$row['Buildings']. "<br />";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的变量在会话中,请尝试:
希望有帮助
If your variables are in session, try:
Hope it helps