我有一个简单的页面/数据库设置,您可以增加或减少A人或B人的总分。
我得到了一些帮助,将一些东西投入到他们自己的功能中来清理。我是php和mysql的新手;阅读我所拥有的东西我理解所有的逻辑,我只是无法弄清楚如何完成它,在某种意义上,就像现在一样,任何按钮都会增加一个点;我需要让Subtract点按钮工作。
我的数据库是“用户”表,其中包含点和名称
这就是我所拥有的:
<?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 );
?>
答案 0 :(得分:1)
有很多错误:
我试着把它清理一下。这里和那里可能没有什么错误(我没有测试过),但是演示了我刚刚说过的内容:
<?php
function print_r_dump( $val )
{
echo '<pre>';
print_r( $val );
echo '</pre>';
}
$link = mysql_connect('mysql.xxxxx.net', 'xxxxx', 'xxxxx');
mysql_select_db('xxxxx_points', $link);
if ( isset( $_POST ) )
{
$str = "select * from user where name = '";
$str .= $_POST['person'];
$str .= "'";
$result = mysql_query( $str , $link);
if($result) {
$row = mysql_fetch_assoc($result) ;
$points = $row['points'];
if(condition for adding) { // probably if(isset($_POST[submit_button_add))
$str = 'update user set points = '. ($points+1) . ' where name = \''. $_POST['person'] . '\'';
$result = mysql_query( $str , $link);
} else if(condition for subtracting){// probably if(isset($_POST[submit_button_sub))
$str = 'update user set points = '. ($points-1) . ' where name = \''. $_POST['person'] . '\'';
$result = mysql_query( $str , $link);
}
$str = "SELECT * FROM user WHERE 1";
$result = mysql_query( $str , $link);
if($result) {
$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_add" value="Add point" />
<input type="submit" name="submit_button_sub" 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>
答案 1 :(得分:1)
看起来您不一定需要第一个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'];
}
?>