PHP 更新/使用 MySQL 进行行编辑

发布于 2024-12-22 15:02:54 字数 1114 浏览 1 评论 0原文

所以我一直在开发一款国家模拟器游戏。它将在线,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-12-29 15:02:54

如果您的变量在会话中,请尝试:

//or if a constant then,
$result = mysql_query("UPDATE members SET buildings ='10' WHERE login='".SESS_NATION."'") 
or die(mysql_error());  

//first one
$result = mysql_query("UPDATE members SET buildings ='10' WHERE login='".$_SESSION['SESS_NATION']."'") 
or die(mysql_error());  

希望有帮助

If your variables are in session, try:

//or if a constant then,
$result = mysql_query("UPDATE members SET buildings ='10' WHERE login='".SESS_NATION."'") 
or die(mysql_error());  

//first one
$result = mysql_query("UPDATE members SET buildings ='10' WHERE login='".$_SESSION['SESS_NATION']."'") 
or die(mysql_error());  

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文