我对php页面的背景颜色变化有问题。 首先,我想从DATABASE中获取颜色名称以使当前颜色不变。我的意思是,如果用户将背景颜色更改为BLUE,例如,它将是常量和永久性的。所以,我将颜色存储在数据库中,但问题是我无法根据用户选择的颜色更新数据库。
这是数据库中的当前颜色(这部分工作很好,我只是为了让一切易于理解而添加了这一部分);
<body name="body" id="body" bgcolor="
<?php
mysql_connect("localhost","DB","password") or die("ERROR!!");
mysql_select_db("DB") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB");
$sql = mysql_query("SELECT color FROM background");
$color = 'color';
while($rows = mysql_fetch_assoc($sql))
echo $rows[color];
?>
">
这是我更新数据库的一部分;
<?php
$con = mysql_connect("localhost","DB","password") or die("ERROR!!");
mysql_select_db("DB") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB");
if (isset( $_SESSION['CurrentUser'])){
echo '<form method="post"><center><input type="submit" name="background" value="Change background">';
echo '<input type="submit" name="color" value="Change font color">';
echo '<input type="submit" name="size" value="Change font size"></center></form><br/>';
if (isset( $_POST['background'])){
echo '<span style="color:black"><center>Choose background color</center></span><br/>';
echo '<center><a href="?colour=blue"><img src="blue.png" width="20" height="50"></a></center>';
echo '<center><a href="?colour=black"><img src="black.jpg" width="20" height="50"></a></center>';
echo '<center><a href="?colour=silver"><img src="silver.jpg" width="20" height="50"></a></center>';
echo '<center><a href="?colour=red"><img src="red.png" width="20" height="50"></a></center><br/><br/>';
$Color = $_POST['colour']; // THIS LINE IS THE PROBLEM
if ($Color == 'blue'){
$sql = "UPDATE background SET color='blue' WHERE id=1";
mysql_query($sql,$con); }
if ($Color == 'black'){
$sql = "UPDATE background SET color='black' WHERE id=1";
mysql_query($sql,$con); }
if ($Color == 'silver'){
$sql = "UPDATE background SET color='silver' WHERE id=1";
mysql_query($sql,$con); }
if ($Color == 'red'){
$sql = "UPDATE background SET color='red' WHERE id=1";
mysql_query($sql,$con); }
}
}
?>
</form>
</td></tr></table>
一切都运作得好!我的意思是如果我想要更新数据库,那么就没有与MYSQL连接或其相关的问题。问题是我无法获得“颜色”参数的值来更新数据库。
感谢
答案 0 :(得分:0)
当您回显结果时,您在color
之前忘记了一个美元符号:
$color = 'color';
while($rows = mysql_fetch_assoc($sql))
echo $rows[color];
应该是:
$color = 'color';
while($rows = mysql_fetch_assoc($sql))
echo $rows[$color];
答案 1 :(得分:0)
How about this:
//....
echo '<form method="post"><center><input type="submit" name="background" value="Change background">';
echo '<input type="submit" name="color" value="Change font color">';
echo '<input type="submit" name="size" value="Change font size"></center></form><br/>';
if (isset( $_POST['background'])){
echo '<span style="color:black"><center>Choose background color</center></span><br/>';
echo '<center><a href="?colour=blue"><img src="blue.png" width="20" height="50"></a></center>';
echo '<center><a href="?colour=black"><img src="black.jpg" width="20" height="50"></a></center>';
echo '<center><a href="?colour=silver"><img src="silver.jpg" width="20" height="50"></a></center>';
echo '<center><a href="?colour=red"><img src="red.png" width="20" height="50"></a></center><br/><br/>';
}
if( !empty( $_GET['colour'] )) {
$Color = $_GET['colour'];
if ($Color == 'blue'){
$sql = "UPDATE background SET color='blue' WHERE id=1";
mysql_query($sql,$con); }
if ($Color == 'black'){
$sql = "UPDATE background SET color='black' WHERE id=1";
mysql_query($sql,$con); }
if ($Color == 'silver'){
$sql = "UPDATE background SET color='silver' WHERE id=1";
mysql_query($sql,$con); }
if ($Color == 'red'){
$sql = "UPDATE background SET color='red' WHERE id=1";
mysql_query($sql,$con); }
}
}
?>
</form>
答案 2 :(得分:0)
然后尝试
$Color = $_GET['colour'];