ec Posted March 26, 2008 Share Posted March 26, 2008 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/ Share on other sites More sharing options...
kenrbnsn Posted March 26, 2008 Share Posted March 26, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501729 Share on other sites More sharing options...
ec Posted March 26, 2008 Author Share Posted March 26, 2008 No, it's still not working...it stores everything but the $result which should be three what do u mean by the variables? Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501743 Share on other sites More sharing options...
kenrbnsn Posted March 26, 2008 Share Posted March 26, 2008 Where are the variables $fname, $lname, $result, $teacherid being set? Please post all of your code between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501755 Share on other sites More sharing options...
ec Posted March 26, 2008 Author Share Posted March 26, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501759 Share on other sites More sharing options...
kenrbnsn Posted March 26, 2008 Share Posted March 26, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501775 Share on other sites More sharing options...
ec Posted March 27, 2008 Author Share Posted March 27, 2008 Thanks, but no...it's still no storing the level... Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501786 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2008 Share Posted March 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501796 Share on other sites More sharing options...
ec Posted March 27, 2008 Author Share Posted March 27, 2008 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') Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501802 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2008 Share Posted March 27, 2008 This tells me that the first query isn't returning any information, so there is nothing in the variable $level. You have to figure out why the query isn't returning what you expect. Ken Quote Link to comment https://forums.phpfreaks.com/topic/98065-inserting/#findComment-501807 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.