保存按钮:如何使用结果更新另一个表?

时间:2013-11-07 20:09:42

标签: php sql save

我有一个食谱表,每个食谱旁边都有一个保存按钮。单击此按钮时,我希望检索配方名称并将其保存到用户的配置文件中(添加到数据库中)。

然而,我遇到了一些问题。我有一个保存按钮,它链接到'save_recipe.php'。目前,当我选择保存时,我被定向到一个空白页面。我觉得这与我创建保存按钮的方式有关,因为没有显示echo语句。

按钮的代码是:

<html>
<body>

  <form action="../user profile/save_recipe.php" method="post">
   <input type="submit" name="submit" class="btn" value="Save Recipe"></td>
 </form> 

</body>   
</html>

save_recipe的代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>BMI Result</title>
</head>
<link rel="stylesheet" href="css.css" type="text/css" />
<body>
    <?php

    error_reporting(E_ALL &~ E_NOTICE);

    // Start the session
    session_start();

        // If the session vars aren't set, try to set them with a cookie
    if (!isset($_SESSION['user_id'])) {
        if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
            $_SESSION['user_id'] = $_COOKIE['user_id'];
            $_SESSION['username'] = $_COOKIE['username'];
        }
    }
    echo gfgfg;


    // Make sure the browser is transmitting in UTF-8
    header('Content-type: text/html; charset=utf-8');

        // Clear the error message
    $error_msg = "";

    //if (isset($_SESSION['user_id']))
    //  echo $_SESSION['user_id'];

    $dbc = mysqli_connect('localhost', 'root', 'root', 'help_me_be_healthy') or die("Error " . mysqli_error($dbc));
    mysqli_set_charset($dbc, "utf8");

    echo gfdgfdg;

    if(isset($_POST['submit']))
    {
        $recipe_name = mysqli_result(mysqli_query("SELECT `recipe_1_name`, `recipe_2_name` FROM `carbohydrates`");

                  //"SELECT `recipe_1_name` OR `recipe_2_name` FROM `carbohydrates` WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
            $query = "UPDATE `users` SET `recipes_saved` = $recipe_name WHERE `user_id` = '" . $_SESSION['user_id'] . "'";
            $data= mysqli_query($dbc,$query);

            echo '$recipe_name';
            );

    }
    ?>

任何建议都会非常感激,我对此很新!

1 个答案:

答案 0 :(得分:0)

$ dbc = mysqli_connect('localhost','root','root','help_me_be_healthy')或死(“错误”。)

你有几个mysqli_connect实例。它应该是mysql_connect。另外,你真的不应该使用root作为u-name&amp;对词。这是PHP IIRC的默认安装。