0
<?php
session_start();
include_once 'Dbconnect.php';

$error = false;

if( isset($_POST['btn-classroom']) ) {
  $classroom_name = trim($_POST['classroom_name']);
  $classroom_name = strip_tags($classroom_name);
  $classroom_name = htmlspecialchars($classroom_name);

  $users_id = $_SESSION['users'];

  if(empty($classroom_name)){
   $error = true;
   $classroom_nameError = "Please enter a classroom name.";
  }

  if(!error){
      $query = "INSERT INTO classroom (classroom_name, users_id) VALUES('$classroom_name', '$users_id')";
      $result = mysql_query($query);

      if($result){
          $errTyp = "success";
          $errMSG = "Classroom successfully created!";
          unset($classroom_name);
      }
      else{
          $errTyp = "danger";
          $errMSG = "Something went wrong, try again later...";
      }
  }
}
?>

<!DOCTYPE HTML>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<center>
<br>
<br>
<div id="login-form">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">
    <?php
   if ( isset($errMSG) ) {

    ?>
    <div class="form-group">
             <div class="alert alert-<?php echo ($errTyp=="success") ? "success" : $errTyp; ?>">
    <span class="glyphicon glyphicon-info-sign"></span> <?php echo $errMSG; ?>
                </div>
             </div>
                <?php
   }
   ?>
   <div class="form-group">
             <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
             <input type="text" name="classroom_name" class="form-control" placeholder="Classroom Name" value="<?php echo $classroom_name ?>" />
                </div>
                <span class="text-danger"><?php echo $classroom_nameError; ?></span>
            </div>
            <br>
            <div class="form-group">
             <button type="submit" class="btn btn-block btn-primary" name="btn-classroom">Create</button>
            </div>
    </form>
</div>
</center>
</body>
</html>

I want to add the value of users_id into table classroom where it is a foreign key but is a primary key in users table. users_id is assigned automatically during sign up and is auto incremented. This code is not working. Please Help. I am new to php. Thank You

1 Answers1

0

Because you are not using $error variable here:

if(!error){

You need to use your $error variable here.

It's better to use error_reporting() in your development mode not for production, this will help you to find errors and warnings.

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

Also, note that, your code is open for SQL injection, you can use Prepared Statement, this will help you to prevent your queries with SQL attack: How can I prevent SQL injection in PHP?

Do you know, mysql_* is deprecated and closed in PHP 7.

Community
  • 1
  • 1
devpro
  • 16,184
  • 3
  • 27
  • 38