PHP & MySQL Undefined variable problem

Posted by comma on Stack Overflow See other posts from Stack Overflow or by comma
Published on 2010-04-23T23:33:25Z Indexed on 2010/04/23 23:43 UTC
Read the original article Hit count: 229

Filed under:
|

I keep getting the following error Undefined variable: id on line 91 can some one help me correct this problem?

The error is on this line.

$query2 = "INSERT INTO users_skills (skill_id, user_id, date_created) VALUES ('$id', '$user_id', NOW())";

MySQL database tables.

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
skill VARCHAR(255) NOT NULL,
experience VARCHAR(255) NOT NULL,
years VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);


CREATE TABLE users_skills (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
skill_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
date_created DATETIME UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

Here is the PHP & MySQL code.

if (isset($_POST['info_submitted'])) {

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT learned_skills.*, users_skills.* 
                               FROM learned_skills 
                               INNER JOIN users_skills ON learned_skills.id = users_skills.skill_id 
                               WHERE user_id='$user_id'");

        if (!$dbc) {
                print mysqli_error($mysqli);
                return;
        }

$user_id = '5';

$skill = $_POST['skill'];
$experience = $_POST['experience'];
$years = $_POST['years'];

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT learned_skills.*, users_skills.* FROM learned_skills INNER JOIN users_skills ON users_skills.skill_id = learned_skills.id WHERE users_skills.user_id='$user_id'");

if (mysqli_num_rows($dbc) == 0) {

if (isset($_POST['skill']) && trim($_POST['skill'])!=='') {

                $mysqli = mysqli_connect("localhost", "root", "", "sitename");
                $query1 = mysqli_query($mysqli,"INSERT INTO learned_skills (skill, experience, years) 
                                                VALUES ('" . $skill . "', '" . $experience . "', '" . $years . "')");

        if (mysqli_query($mysqli, $query1)) {
                print mysqli_error($mysqli);
                return;
        }

        $mysqli = mysqli_connect("localhost", "root", "", "sitename");
        $dbc = mysqli_query($mysqli,"SELECT id 
                                     FROM learned_skills 
                                     WHERE id='" . $skill . "' 
                                     AND experience='" . $experience . "' 
                                     AND years='" . $years . "'");

        if (!$dbc) {
            print mysqli_error($mysqli);
        }  else {
            while($row = mysqli_fetch_array($dbc)){ 
                $id = $row["id"];
            }
        }

        $query2 = "INSERT INTO users_skills (skill_id, user_id, date_created) VALUES ('$id', '$user_id', NOW())";
}

}

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql