php - Enter value of user_id in another table according to user login session -


<?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 classroom name.";   }    if(!error){       $query = "insert classroom (classroom_name, users_id) values('$classroom_name', '$users_id')";       $result = mysql_query($query);        if($result){           $errtyp = "success";           $errmsg = "classroom 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 add value of users_id table classroom foreign key primary key in users table. users_id assigned automatically during sign , auto incremented. code not working. please help. new php. thank you

because not using $error variable here:

if(!error){ 

you need use $error variable here.

it's better use error_reporting() in development mode not production, find errors , warnings.

// same error_reporting(e_all); ini_set('error_reporting', e_all);  // report php errors (see changelog) error_reporting(e_all); 

also, note that, code open sql injection, can use prepared statement, prevent queries sql attack: how can prevent sql injection in php?

do know, mysql_* deprecated , closed in php 7.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -