gpt4 book ai didi

PHP/Mysql 条件语句

转载 作者:行者123 更新时间:2023-11-29 09:16:07 24 4
gpt4 key购买 nike

我有一个简单的页面/数据库设置,您可以在其中增加或减少 A 或 B 的总分。

我得到了一些帮助,将一些东西放入它们自己的函数中来清理东西。我是 php 和 mysql 的新手;阅读我所拥有的内容,我理解所有的逻辑,我只是​​不知道如何完成它,从某种意义上说,就像现在一样,任何按钮都会添加一个点;我也需要让减点按钮正常工作。

我的数据库是表“用户”,其中包含点和名称

这是我所拥有的:

<?php
function print_r_dump( $val )
{
echo '<pre>';
print_r( $val );
echo '</pre>';
}

//if ( isset( $_POST ) || isset( $_POST['Add point'] ) )
if ( isset( $_POST ) )
{

//Prints what's in post
print_r_dump( $_POST );

$str = "select * from user where name = '";
$str .= $_POST['person'];
$str .= "'";

$link = mysql_connect('mysql.xxxxx.net', 'xxxxx', 'xxxxx');
mysql_select_db('xxxxx_points', $link);
$result = mysql_query( $str , $link);

$row = mysql_fetch_assoc($result) ;
$points = $row['points'];

$str = 'update user set points = '. ($points+1) . ' where name = \''. $_POST['person'] . '\'';
$result = mysql_query( $str , $link);
}




$link = mysql_connect('mysql.xxxxx.net', 'xxxxx', 'xxxxx');
mysql_select_db('xxxxx_points', $link);

$str = "SELECT points FROM user WHERE name='Person A'" ;
$str = "SELECT * FROM user ";
$result = mysql_query( $str , $link);


$results = array();
while ( $row = mysql_fetch_assoc($result) )
{
$results[ $row['name'] ] = $row['points'];
}

?>

Person A's count: <?php echo $results['Person A']; ?>
<br>


<form method="post" action="index.php">
<input type="submit" name="submit_button" value="Add point" />
<input type="submit" name="submit_button" value="Subtract point" />
<input type="hidden" name="person" value="Person A" />
</form>

<br>

Person B's count: <?php echo $results['Person B']; ?>
<br>

<form method="post" action="index.php">
<input type="submit" name="submit_button" value="Add point" />
<input type="submit" name="submit_button" value="Subtract point" />
<input type="hidden" name="person" value="Person B" />
</form>

<?

print_r_dump( $results );

?>

最佳答案

看来您也不一定需要第一个 SQL 查询。例如,只要点字段是数字而不是字符串,您就可以使用数据库中的当前值并让它自行更新。对代码的主要 PHP 部分尝试此操作。

<?php
function print_r_dump( $val )
{
echo '<pre>';
print_r( $val );
echo '</pre>';
}

if ( isset( $_POST ) )
{

//Prints what's in post
print_r_dump( $_POST );

$link = mysql_connect('mysql.xxxxx.net', 'xxxxx', 'xxxxx');
mysql_select_db('xxxxx_points', $link);

$str = "";

if ($_POST['submit_button']=='Add point')
{
$str = 'update user set points = points+1 where name = \''. $_POST['person'] . '\'';
}
else if ($_POST['submit_button']=='Subtract point')
{
$str = 'update user set points = points-1 where name = \''. $_POST['person'] . '\'';
}
if ($str)
{
$result = mysql_query( $str , $link);
}
}

$str = "SELECT points FROM user WHERE name='Person A'" ;
$str = "SELECT * FROM user ";
$result = mysql_query( $str , $link);


$results = array();
while ( $row = mysql_fetch_assoc($result) )
{
$results[ $row['name'] ] = $row['points'];
}

?>

关于PHP/Mysql 条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4169479/

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