Jump to content

Recommended Posts

I'm trying to save the result of this query into the table members but it wont do anything...It works when I type the qry directly into phpmyadmin but wouldn't with mysql...

anyone know what's wrong with it??

 

 

  $qry = "SELECT accesslevel.level FROM accesslevel, teacher WHERE accesslevel.position= teacher.position AND teacher.teacherid='teacherid'";

  $result=mysql_query($qry);

  $result = mysql_fetch_array($result);

 

  //Create INSERT query

  $qry = "INSERT INTO members(firstname, lastname, level, teacherid, passwd) VALUES('$fname','$lname','$result','$teacherid','".md5($_POST['password'])."')";

  $result = mysql_query($qry);

  //Check whether the query was successful or not

  if($result) {

      header("location: register-success.php");

      exit();

  }else {

      die("Query failed");

  }

Link to comment
https://forums.phpfreaks.com/topic/98065-inserting/
Share on other sites

Where are the variables you're using coming from?

 

try:

<?php
$qry = "INSERT INTO members(firstname, lastname, level, teacherid, passwd) VALUES('$fname','$lname','$result','$teacherid','".md5($_POST['password'])."')";
   $result = mysql_query($qry) or die("Problem with the query: $qry<br>" . mysql_error());
?>

 

And see if that reports an error.

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501729
Share on other sites

This is the full code

 

<?php
//Start session
session_start();

//Array to store validation errors
$errmsg_arr = array();

//Validation error flag
$errflag = false;

//Connect to mysql server
$link = mysql_connect("localhost","*****","*****");
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db("e1420022_echan334");
if(!$db) {
	die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
	$str = @trim($str);
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
	return mysql_real_escape_string($str);
}

//Sanitize the POST values
$fname = clean($_POST['fname']);
$lname = clean($_POST['lname']);
$teacherid = clean($_POST['teacherid']);
$password = clean($_POST['password']);
$cpassword = clean($_POST['cpassword']);

//Input Validations
if($fname == '') {
	$errmsg_arr[] = 'First name missing';
	$errflag = true;
}
if($lname == '') {
	$errmsg_arr[] = 'Last name missing';
	$errflag = true;
}
if($teacherid == '') {
	$errmsg_arr[] = 'Teacher ID missing';
	$errflag = true;
}
if(strlen($teacherid)<>3) {
	$errmsg_arr[] = 'Teacher ID must be 3 characters long';
	$errflag = true;
}
if($password == '') {
	$errmsg_arr[] = 'Password missing';
	$errflag = true;
}
if(strlen($password) <5) {
	$errmsg_arr[] = 'Password must be more than 6 character length';
	$errflag = true;
}
if($cpassword == '') {
	$errmsg_arr[] = 'Confirm password missing';
	$errflag = true;
}
if( strcmp($password, $cpassword) != 0 ) {
	$errmsg_arr[] = 'Passwords do not match';
	$errflag = true;
}


//Check for duplicate teacher ID
$qry = "SELECT count(*) AS c FROM members WHERE teacherid='$teacherid'";
$result = mysql_query($qry);
if($result) {
	$result_array = mysql_fetch_assoc($result);
	if($result_array['c'] > 0) {
		$errmsg_arr[] = 'Login ID already in use';
		$errflag = true;
	}
	@mysql_free_result($result);
}
else {
	die("Query failed");
}

//Checking that teacher ID exists on Teachers table
	 $qry = "SELECT teacherid FROM teacher WHERE teacherid='$teacherid'";
	 if ($result = mysql_query($qry)) {
	   if (mysql_num_rows($result)) {
	     $result_array = mysql_fetch_assoc($result);
	   } else {
  	    $errmsg_arr[] = 'Teacher has not been registered on position/teacher table';
  	    $errflag = true;
  	  }
  	} else {
  	  die("Query failed");
  	}

//If there are input validations, redirect back to the registration form
if($errflag) {
	$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
	session_write_close();
	header("location: register-form.php");
	exit();
}

//userlevel
$qry = "SELECT accesslevel.level FROM accesslevel, teacher WHERE accesslevel.position= teacher.position AND teacher.teacherid='teacherid'";
   	$level=mysql_query($qry);
  	 $level = mysql_fetch_array($level);


//Create INSERT query
$qry = "INSERT INTO members(firstname, lastname, level, teacherid, passwd) VALUES('$fname','$lname','$level','$teacherid','".md5($_POST['password'])."')";
$result = mysql_query($qry);
//Check whether the query was successful or not
if($result) {
	header("location: register-success.php");
	exit();
}else {
	die("Query failed");
}
?>

 

(edited by kenrbnsn to add the


tags and to remove username/password)

Link to comment
https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501759
Share on other sites

You're problem is here:

<?php
$qry = "SELECT accesslevel.level FROM accesslevel, teacher WHERE accesslevel.position= teacher.position AND teacher.teacherid='teacherid'";
   	$level=mysql_query($qry);
  	 $level = mysql_fetch_array($level);
?>

This will result in the variable $level containing an array. You need to get the field value instead.

 

Try:

<?php
$qry = "SELECT accesslevel.level as level FROM accesslevel, teacher WHERE accesslevel.position= teacher.position AND teacher.teacherid='teacherid'";
   	$rs=mysql_query($qry);
  	 $row = mysql_fetch_assoc($rs);
         $level = $row['level'];
//Create INSERT query
$qry = "INSERT INTO members(firstname, lastname, level, teacherid, passwd) VALUES('$fname','$lname','$level','$teacherid','".md5($_POST['password'])."')";
$result = mysql_query($qry);
//Check whether the query was successful or not
if($result) {
	header("location: register-success.php");
	exit();
}else {
	die("Query failed");
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501775
Share on other sites

Time to put some debugging echo's in:

<?php
$qry = "SELECT accesslevel.level as level FROM accesslevel, teacher WHERE accesslevel.position= teacher.position AND teacher.teacherid='teacherid'";
echo '$qry: <pre>' . $qry . '</pre>';
   	$rs=mysql_query($qry);
  	 $row = mysql_fetch_assoc($rs);
echo '<pre>$row:' . print_r($row,true) . '</pre>';
         $level = $row['level'];
//Create INSERT query
$qry = "INSERT INTO members(firstname, lastname, level, teacherid, passwd) VALUES('$fname','$lname','$level','$teacherid','".md5($_POST['password'])."')";
echo '$qry: <pre>' . $qry . '</pre>';
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501796
Share on other sites

ok....this comes up

 

$qry:

 

SELECT accesslevel.level as level FROM accesslevel, teacher WHERE accesslevel.position= teacher.position AND teacher.teacherid='teacherid'

 

$row:

 

$qry:

 

INSERT INTO members(firstname, lastname, level, teacherid, passwd) VALUES('Scott','Naismith','','SNS','a0212b785deda3f1dcfb196e4b7ba6a1')

 

 

Link to comment
https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501802
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.