需要帮助将信息插入数据库并将其返回

时间:2018-01-20 04:30:43

标签: php

在我的代码中,我有三个单选按钮,用户单击以选择每个标题,文本和按钮的颜色为十六进制值。并且它正在正确地发送信息,但是当运行的代码将所有内容放入数据库时​​,它将用户发送到主页,说明颜色已经更改,但它没有。

这是我的PHP代码:

session_start();

if (isset($_SESSION['username'])) {

include 'databaseconnection.php';

$username = $_SESSION['username'];

$titlecolor = $_POST['titlecolor'];

$textcolor= $_POST['textcolor'];

$buttoncolor = $_POST['buttoncolor'];

$sql = "SELECT * FROM users WHERE username='$username'";

$result = mysqli_query($con, $sql);

$resultCheck = mysqli_num_rows($result);

if ($resultCheck < 1) {

    header("Location: index.php?wronghappened");

    exit();

}

else {

    if ($row = mysqli_fetch_assoc($result)) {

        $update = "UPDATE students (titlecolor, `textcolor`, buttoncolor) VALUES (`$titlecolor`, `$textcolor`, `$buttoncolor`) WHERE uname='$uname'";

        $_SESSION['id'] = $row['id'];

        $_SESSION['firstname'] = $row['firstname'];

        $_SESSION['lastname'] = $row['lastname'];

        $_SESSION['email'] = $row['email'];

        $_SESSION['username'] = $row['username'];

        $_SESSION['text'] = $row['text'];

        $_SESSION['title'] = $row['title'];

        $_SESSION['button'] = $row['button'];

        header("Location: home.php?color_changed");

        exit();

    }
}

}

else {

header("Location: index.php?not_in");

exit();

}

4 个答案:

答案 0 :(得分:0)

您没有使用更新查询。执行更新查询,以便更新数据库。

$update = "UPDATE students (titlecolor, `textcolor`, buttoncolor) VALUES (`$titlecolor`, `$textcolor`, `$buttoncolor`) WHERE uname='$uname'";

mysql_query($update);

答案 1 :(得分:0)

而不是

  $update = "UPDATE students (titlecolor, `textcolor`, buttoncolor) VALUES (`$titlecolor`, `$textcolor`, `$buttoncolor`) WHERE uname='$uname'"; 

变量textcolor作为字符串发送到服务器,而不是变量,因为您已将其包装在qoutes中。您应该只将qoutes包装在要作为值发送的字符串周围。请改用:

  $update = "UPDATE students (titlecolor, textcolor, buttoncolor) VALUES (`$titlecolor`, `$textcolor`, `$buttoncolor`) WHERE uname='$uname'"; 

然后执行此代码,运行

 mysql_query($con, $update);

答案 2 :(得分:0)

试试这个

$update = "UPDATE students SET titlecolor='$titlecolor', textcolor='$textcolor', buttoncolor='$buttoncolor' WHERE uname='$uname'";
$updateResult = mysqli_query($con, $update);

而不是

$update = "UPDATE students (titlecolor, `textcolor`, buttoncolor) VALUES (`$titlecolor`, `$textcolor`, `$buttoncolor`) WHERE uname='$uname'";

如果您不使用mysqli_query执行查询,则不会显示任何效果。

此外,我建议您将来使用PDO而不是mysqli。为此reasons

快乐编码:)

答案 3 :(得分:0)

用以下内容替换您的查询:

$updatequery = "UPDATE students set titlecolor='$titlecolor', textcolor='$textcolor', buttoncolor='$buttoncolor' WHERE uname='$uname'";

现在检查查询是否成功执行了?

if (mysqli_query($con, $updatequery))
{
  echo "successfully execute.....";
//do something here  
}
else
{
echo "Sorry query can't executed!";
echo "My Query ".$updatequery;
}

如果查询未执行,那么您将在“我的查询”之后看到您的查询,因此复制该查询并在您的mysql中执行它然后您将发现主要错误。