douwei3172 2010-11-12 22:04
浏览 18
已采纳

PHP / Mysql条件语句

I have a simple page/database setup where you can increment or decrement the point total for person A or person B.

I've gotten some help, throwing some things into their own function to clean things up. I'm new to php and mysql; reading over what I have I understand all the logic, I just can't figure out how to finish it off, in the sense that as it is now, any button will add a point; I need to get the Subtract point button working too.

My database is table 'user' with points and name within

Here's what I have:

<?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  );

?>
  • 写回答

2条回答 默认 最新

  • duanchu0031 2010-11-12 23:01
    关注

    Looks like you don't necessarily need the first SQL query either. You can use the current value in the database and have it update itself, as long as the points field is numeric and not a string, for example. Try this for the major PHP portion of your code.

    <?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'];
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档