Jump to content

sabinmash

Members
  • Posts

    42
  • Joined

  • Last visited

Everything posted by sabinmash

  1. Gaahh... the original line and your correction looked so similar i had to look 5 times before I caught it. Works now. Thank you so much!
  2. Been just trying to test out some basic code that utilizes gd. In Chrome i receive a blank page. In firefox I get "The image "image.php" cannot be displayed because it contains errors". There is no whitespace before or after the php tags, which seems to cause this error sometimes. $image = imagecreate(200,20); $background = imagecolorallocate($image, 0, 0, 0); $foreground = imagecolorallocate($image,255,255,255); imagestring($image, 5, 5, 1, "Test", $foreground); header("Content-type: image/jpeg"); $imagejpeg($image); I am just following a tutorial, and at this point in the tutorial i should get at least some garbled text, if not an image. I looked at other common causes. gd2 is un-commented in php.ini. phpinfo shows: GD Support enabled GD Version bundled (2.0.34 compatible) FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.4.3 GIF Read Support enabled GIF Create Support enabled JPEG Support enabled libJPEG Version 6b PNG Support enabled libPNG Version 1.2.46 WBMP Support enabled XBM Support enabled gd.jpeg_ignore_warning 0 0 I have not found any other solutions to try, or maybe I have made an error is checking these solutions. Any advice with be great! Thank you for your time.
  3. preg_replace() asks that "Delimiter must not be alphanumeric or backslash" in the pattern. So I changed $new_text = preg_replace($_POST['withthis'] ,$_POST['withthis'],$_POST['text']); to this $replacethis = $_POST['replacethis']; $new_text = preg_replace("/$replacethis/",$_POST['withthis'],$_POST['text']); It works fine, but out of curiosity, is there any way to have the POST variable as a parameter directly, and why does it not work? Just to try it, I attempted: "/$_POST['withthis']/" and $_POST["/'withthis'/"] and both do not work. str_replace is a better option I think, but I am just trying to get a better understanding of this delimiter rule. Thanks for your time!
  4. Well the short answer is, cause I don't know any better. PHP noob, and general programming noob. Still have yet to take a design methods class. Thank you about using the using mysqli_fetch_array on a delete query, I completely did not realize. I was trying to use some previously written code from SELECT query and didn't realize that a DELETE would not return anything at first since its not selecting something.
  5. Oh god. I apologize. it seems I asked the wrong (silly) question. The query works, so i must have made a mistake when testing it. The code using this DELETE doesn't not work however, and I believe it has something to do with the syntax of the SQL in PHP. function data_retrieve($get_query){ $conn = db_connect(); $query = $get_query; $result = $conn->query($query); return $result; } function delete_user_answer_query($table, $field1, $field1_value, $field2, $field2_value) { //$sql = "DELETE FROM user_question_answer WHERE user_id = 1 AND question_id = 7 "; $query = "DELETE FROM " . $table . " WHERE " . $field1 . " = " . $field1_value . " AND " . $field2 . " = " . $field2_value; return $query; } function get_query_row($delete_user_answer_query) { $query = $delete_user_answer_query; $result = data_retrieve($query); while ($row = mysqli_fetch_array($result)) { echo $row[0]; return $row[0]; } } session_start(); echo delete_user_answer_query('user_question_answer', 'user_id', $_SESSION['user_id'], "question_id", 7); $delete_user_answer_query = delete_user_answer_query('user_question_answer', 'user_id', $_SESSION['user_id'], "question_id", 7); get_query_row($delete_user_answer_query); $question_id = get_question_id(query_question_id(7)); This code echoes : DELETE FROM user_question_answer WHERE user_id = 1 AND question_id = 7 . Which works fine when i tested it in directly in the database. this prints along with the error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Alumni Survey Site\survey_queries.php on line 52. Which is where it reads: " while ($row = mysqli_fetch_array($result))"
  6. For example: DELETE FROM user_question_answer WHERE user_id = 1 AND question_id = 7 AND `answer_id` = 3 This sql query has no error, but reports deleting 0 rows. user_id and question_id fields are both foreign keys in this table. Could that be why it's not working, yet not reporting an error? Thank you for your time.
  7. Oh, i didn't know about the IN clause, that really makes things a lot simpler, thank you! I am trying to make this a function to be used multiple times. However, nothing is being checked in the checkboxes when i use it as a function. function create_questions($question, $total_options){ for($i=0 ; $i<$total_options; $i++) { $question[] = array($i); } return $question; } function uncheck_questions($question, $total_options){ for($i=0 ; $i<$total_options; $i++) { $question[$i] = 'unchecked'; } return $question; } function data_retrieve($question_numb, $total_options, $sql_in_num, $id_being_checked, $get_query){ $conn = db_connect(); //create the question selection array that will hold the "checked" or "unchecked" variables. $question_numb = create_questions($question_numb, $total_options); //uncheck all of the $question_numb = uncheck_questions($question_numb, $total_options); // The IN operator allows you to specify multiple values in a WHERE clause, allowing you to grab all your results with one query. $sql_in = $sql_in_num; // Remember, don't wrap integers with apostrophes in your query. $query = $get_query; $result = $conn->query($query) or die(mysql_error()); while ($row = mysqli_fetch_array($result)) { //echo $row['id_being_checked']; $question_numb[intval($row[$id_being_checked]) - 1] = 'checked'; } } The query function is here. function get_question4_data($sql_in) { // Remember, don't wrap integers with apostrophes in your query. $query = "SELECT degree_id FROM `user-degree` WHERE `user_id` = " . $_SESSION['user_id'] . " AND degree_id IN (" . $sql_in . ")"; return $query ; }
  8. Thank you this will certainly help shorten the code a bit. If only it worked! I have spent so many hours on this problem, and of course the solution is going to be relatively simple i bet.
  9. I've been trying to simplify this code, let alone make it work. It's a loop to change the different question checkboxes to "checked" if certain degree_ids are found. All of the examples i have looked at are very strange and I'm not sure how to go about this, thought it seems to be a common question. Everyone seems to have their own way. Any advice would be greatly appreciated. for($i =1; $i <= $arraycount; $i++){ $query = "SELECT degree_id FROM `user-degree` WHERE `user_id` = '".$user_id."' AND degree_id = '".$i."'"; $result = $conn->query($query) or die(mysql_error()); $row = mysqli_fetch_array($result) ; echo $row['degree_id']; if(!$y){ $y = 0; } if ($row['degree_id'] == 1){ $q4[0] = 'checked'; } if ($row['degree_id'] == 2){ $q4[1] = 'checked'; } if ($row['degree_id'] == 3){ $q4[2] = 'checked'; } if ($row['degree_id'] == 4){ $q4[3] = 'checked'; } if ($row['degree_id'] == 5){ $q4[4] = 'checked'; } if ($row['degree_id'] == 6){ $q4[5] = 'checked'; } if ($row['degree_id'] == 7){ $q4[6] = 'checked'; } if ($row['degree_id'] == { $q4[7] = 'checked'; } if ($row['degree_id'] == 9){ $q4[8] = 'checked'; } }
  10. Gosh, i just figured it out. $user_degree_insert_query = "INSERT INTO `user-degree`( user-degree didn't have the surrounding ``. Why is it that sometimes it seems i need these and sometimes not.
  11. An insert function is not inserting. I keep getting "Could not store data, please try again." After some probing, it seems that "$result = $conn->query($user_degree_insert_query); " is where the problem is. Variables before it seem to pass testing. I can't figure out why this line is messing thins up though. Any idea? Thank you in advance for your help! The "//$degree_id = mysql_fetch_array($degree_id_query, MYSQL_BOTH) ;" is commented out because it was giving me trouble, i replaced it with the line above that $conn = db_connect(); $arraycount = count($degree_Array); //loop through the categories chosen and add them each into member-category for($i =0; $i < $arraycount; $i++){ $degree_id_query = "SELECT degree_id FROM degree WHERE degree_type ='".$degree_Array[$i]."'"; $result = $conn->query($degree_id_query); //place the id found into the array, which automatically cancatonates it. $degree_id = $result->fetch_array(); //$degree_id = mysql_fetch_array($degree_id_query, MYSQL_BOTH) ; $user_degree_insert_query = "INSERT INTO user-degree ( `user_id` , `degree_id` ) VALUES ( '".$user_id."', '".$degree_id[0]."' )"; $result = $conn->query($user_degree_insert_query); if($result) { header("Location: survey2.php"); }else { echo "<p>Could not store data, please try again.</p>"; exit;
  12. Great scott! I checked it before and completely overlooked the missing variable. I turned it into username rather than user_id, because it's more readily available and both are unique. the function now looks like this: function get_survey_query1($gender,$birth_range,$degree_year,$username) { $survey1_query = "UPDATE `alumni_survey`.`user` SET `gender` = '".$gender."', `birth_range` = '".$birth_range."', `degree_year` = '".$degree_year."' WHERE `user`.`username` = '".$username."'"; return $survey1_query; } Thank you again.
  13. I just tried this, but unfortunately nothing is updated: The query is put into insert_survey() as a parameter. $survey1_query = get_survey_query1(); // Check that the user filled out the form and inserted details into the database if (($gender) && ($birth_range) && ($degree_year)) { // able to insert into database if(insert_survey($_POST, $survey1_query) != false ) { // echo $survey1_query; // exit; header("Location: survey2.php"); } else { echo "<p>Could not store data, please try again.</p>"; The insert inside insert_survey($survey_answers, $sql) : $query = $sql; $result = $conn->query($query); And here is the function I am getting it from. I dont get any "Could not store data" error, the query seems to go through. I'm not sure what's wrong. Thanks again for you help. function get_survey_query1() { $survey1_query = "UPDATE `alumni_survey`.`user` SET `gender` = '".$_POST['gender']."', `birth_range` = '".$_POST['birth_range']."', `degree_year` = '".$_POST['degree_year']."' WHERE `user`.`user_id` = '".$user_id."'"; return $survey1_query; }
  14. Thanks for the advice, and double thanks for making me laugh out loud while learning (and not being too harsh either lol)! OK I reworked this whole thing. Turns out no user would be using this survey unless they were already given a username and password, so there is only a need for updating, not inserting. And it works fine. This code below also commented out the extract function and accesses the $_POST variables directly. But i don't think this is what you mean by "Access the variable keys directly in the array". I tried using this for example: $survey_answers[$gender], but blank data was updated into the db. At the same time, I am trying to push this further and make this query modular for other survey pages. I don't really know if that's possible here though. Google examples I have found are a bit over my head at this time, so maybe I will just pre-make the other page's query's and ad them into this function with a 2nd parameter that define's the update query. function insert_survey($survey_answers) { // extract order_details out as variables //extract($survey_answers); $username = $_SESSION['valid_user']; $conn = db_connect(); // select user_id based on the entered data $query = "SELECT user_id FROM `user` WHERE `username` = '".$username."'"; $result = $conn->query($query); //if the resulting user_id exists (AND has the POST data just entered, since the query must match those to return a row) , if($result->num_rows>0) { //return the current row of the result set as an object and place it into user variable $user = $result->fetch_object(); //set variable for user_id to the fetched user object data representing the user_id int he db( because it was fetched from the sql select result) $user_id = $user->user_id; //update the fields since the user exists already. $query = "UPDATE `alumni_survey`.`user` SET `gender` = '".$_POST['gender']."', `birth_range` = '".$_POST['birth_range']."', `degree_year` = '".$_POST['degree_year']."' WHERE `user`.`user_id` = '".$user_id."'"; // $query = "UPDATE `alumni_survey`.`user` // SET `gender` = '".$gender."', // `birth_range` = '".$birth_range."', // `degree_year` = '".$degree_year."' // WHERE `user`.`user_id` = '".$user_id."'"; $result = $conn->query($query); if (!$result) { return false; } } return $user_id; }
  15. Come to think of it, i don't really know how i can properly troubleshoot this situation myself. Does a "die(mysql_error()" or something similar, belong after where the insert_survey1() function is used? Or inside that function itself?
  16. Thank fyou for the sql WHERE advice. It didn't fix the problem however. Well I am still getting "Could not story data" from this code here: if (($gender) && ($birth_range) && ($degree_year)) { // able to insert into database if(insert_survey1($_POST) != false ) { header("Location: survey2.php"); } else { echo "<p>Could not store data, please try again.</p>"; } } else { echo "<p>You did not fill in all the fields, please try again.</p><hr />"; }
  17. Does anyone see anything wrong with the SQL? This function keeps returning false. Thank you for your time. function insert_survey1($survey1_anwers) { // extract order_details out as variables extract($survey1_anwers); $username = $_SESSION['valid_user']; $conn = db_connect(); // select user_id based on the entered data $query = "SELECT user_id FROM user WHERE gender = '".$gender."', birth_range = '".$birth_range."', degree_year = '".$degree_year."' username = '".$username."'"; $result = $conn->query($query); //if the resulting user_id exists (AND has the POST data just entered, since the query must match those to return a row) , if($result->num_rows>0) { //return the current row of the result set as an object and place it into user variable $user = $result->fetch_object(); //set variable for user_id to the fetched user object data representing the user_id int he db( because it was fetched from the sql select result) $user_id = $user->user_id; //update the fields since the user exists already. $query = "UPDATE user SET gender = '".$gender."', birth_range = '".$birth_range."', degree_year = '".$degree_year."' WHERE user_id = '".$user_id."'"; // $query = "UPDATE `alumni_survey`.`user` // SET `gender` = '".$gender."', // `birth_range` = '".$birth_range."', // `degree_year` = '".$degree_year."' // WHERE `user`.`user_id` = '".$user_id"'"; } else { //otherwise, if the user doesn't exist already insert this new data. $query = "INSERT INTO user (gender, birth_range, degree_year) VALUES('".$gender."','".$birth_range."','".$degree_year."') WHERE user_id = '".$user_id."'"; $result = $conn->query($query); if (!$result) { return false; } } return $user_id; }
  18. So it works now when i comment out: $result = @$conn->query($query); and when I delete $row = $result->fetch_object() , (with or without the "==") from if (!$result || $row = $result->fetch_object()) { The now working function looks like this: function get_admin_status($username) { // query database for the name for the user's admin status $conn = db_connect(); $result = $conn->query("SELECT admin FROM user WHERE username = '".$username."'"); //$result = @$conn->query($query); if (!$result) { return false; } $row = $result->fetch_object(); return $row->admin; } Is there any way I can make this better? Best practices I am missthing? Thanks again for your time.
  19. OK, done as told. It's returning "bool(false)" But the SQL select looks ok. I'm not sure why it's not selecting. The db_connect() function works in another page, and I get no connection error. Also, what did moving "$row = $result->fetch_object()" into the if statement do? (And I take it I am supposed to change it to "$row =="?) Once I do this, it still dumps "NULL". function get_admin_status($username) { // query database for the name for the user's admin status $conn = db_connect(); $result = $conn->query("select admin from user where username='".$username."'"); if (!$result || $row == $result->fetch_object()) { return false; } return $row->admin; }
  20. Yes the admin value of the user I am using is "y". These things are all in php files that essential just process things between pages that output. This is why i have $_SESSION['valid_user'] = $username; $_SESSION['admin'] = $admin; in there. There is a session start not shown about these session variable assignements, and the session continues to survey1.php, where i then print $_SESSION['valid_user'] and $_SESSION['admin'] only only the valid user variable prints anything. Because I am getting the valid user value from a form, and the admin status I am getting from an SQL select, I cannot just copy what I did to get the valid user session variable.
  21. Hi all! I am trying to get an admin status ("y" or "n" to be retieved from an SQL select. I do so in the following function: function get_admin_status($username) { // query database for the name for a category id $conn = db_connect(); $result = $conn->query("select admin from user where username='".$username."'"); $result = @$conn->query($query); if (!$result) { return false; } $num_cats = @$result->num_rows; if ($num_cats == 0) { return false; } $row = $result->fetch_object(); return $row->admin; } I then utilize this function in another file in the following code. However, the adminhome.php page never loads. It always goes to "survey1.php" . I'm not sure why this is happening. Any help would be appreciated. Thanks for you time! if ($username && $password) { // they have just tried logging in login($username, $password); $admin = get_admin_status($username); if($admin == "y"){ header("Location: adminhome.php"); } else{ //login($username, $password); // if they are in the database register the user id $_SESSION['valid_user'] = $username; $_SESSION['admin'] = $admin; header("Location: survey1.php"); }
  22. Silly me, the typos are now obvious. Seems to be fixed now, not sure why Chrome was inserting twice, separating processes into difference php files helps though.
  23. OK, I fixed it. I took the php insert code and separated them onto their own insertmember.php, insertcategory.php processing page, that redirect to one another. It's almost working fine, but I still cant for the life of me get this one table to update with a loop. for ($i=0; 1<categoryArray; $i++){ $membercategoryquery = "INSERT INTO `hobbyclub`.`member-category` ( `ID` , `member_id` , `category_id` ) VALUES ( NULL , '$member_id[0]', '$categoryArray[i]' )"; mysql_query($membercategoryquery); } Any suggestions for this? It looks right to me but nothing happens.
  24. Got FF and tried it. It was chrome. The sql inserts one into the database with FF, and 2 with Chrome. And FOUR in chrome if I keep this code: <?php //if the query did not work, display error, otherwise display msg. if (!mysql_query($memberquery, $con)){ die('Error: ' . mysql_error()); echo '<h2>Sorry, there was a problem processing your registration, hit the back button and try again!</h2>'; //if the query worked, display success message and a link to see the roster. } else { echo '<h2>Thanks '.$_SESSION['firstname'].'! You\'ve been registered!</h2>'; echo '<p> Now that you\'ve registered, meet the rest of the team. Click this <a href="roster.php"><b>roster</b></a> link to see your new club mates.</p>'; mysql_query($test); } //close connection if (!$con) { mysql_close($con); } ?> Also, in my last post i gave "the full code", it was hte wrong page, sorry for hte confusion. This is hte right page below. Works in FF for the original sql insert. Now I want a 2nd insert for an associative table, called member-category. I am making an array into a string with the vales separated by commas, and removing the final comma. I am trying to stick it into another sql insert. Nothing gets inserted into the member-category table though. <?php $link_register = 1; session_start(user); include("header.php"); $firstname = $_SESSION['firstname']; $middleinitial = $_SESSION['middleinitial']; $lastname = $_SESSION['lastname']; $phone = $_SESSION['phone']; $email = $_SESSION['email']; $street = $_SESSION['street']; $city = $_SESSION['city']; $state = $_SESSION['state']; $categoryArray = $_SESSION['categoryArray']; $gender = $_SESSION['gender']; $con = mysql_connect("localhost", "root", ""); if (!$con) { die("Could not connect: ". mysql_error()); } //The database is selected with the mysql_select_db() function. mysql_select_db("hobbyclub", $con); //put the data insert into a varaible so it can be checked later $memberquery = "INSERT INTO member (firstname, middleinitial, lastname, phone, email, street, city, state, gender) VALUES ('$firstname', '$middleinitial', '$lastname', '$phone', '$email', '$street', '$city', '$state', '$gender')"; //echo "names".$categoryArray[0].' '.$categoryArray[1].' '.$categoryArray[2].' '.$categoryArray[3]; if ($categoryArray){ foreach ($categoryArray as $category){ $categoryString .= "'$".$category."',"; } } //redefine named arrays as their number, so they can go into member-category table if ($categoryArray[0]){ $categoryArray[0] = 0; } if ($categoryArray[1]){ $categoryArray[1] = 1; } if ($categoryArray[2]){ $categoryArray[2] = 2; } if ($categoryArray[3]){ $categoryArray[3] = 3; } //select memberid from db, put it in var, $querymemberid = mysql_query("SELECT * FROM member WHERE email='$email'"); //then put that + all the category array bacj into member-categeroy table $member_id = mysql_num_rows($querymemberid); //concatonate array vars foreach ($categoryArray as $category){ $category_id .= "'".$category."',"; } echo "member id: ".$member_id ; //varaible with string of arrays now inside have the last comma stripped off. $category_id = rtrim($category_id, ",") ; echo $category_id; $membercategoryquery = "INSERT INTO member-category (member_id, category_id) VALUES ('$member_id', '$category_id')"; $test = "INSERT INTO member-category (member_id, category_id) VALUES ('1', '2')"; //insert the data //mysql_query($memberquery); mysql_query($membercategoryquery); mysql_query($test); ?> <div id="banner-wrap"> <div id="banner" class="container_24 rounded"> <?php //if the query did not work, display error, otherwise display msg. if (!mysql_query($memberquery, $con)){ die('Error: ' . mysql_error()); echo '<h2>Sorry, there was a problem processing your registration, hit the back button and try again!</h2>'; //if the query worked, display success message and a link to see the roster. } else { echo '<h2>Thanks '.$_SESSION['firstname'].'! You\'ve been registered!</h2>'; echo '<p> Now that you\'ve registered, meet the rest of the team. Click this <a href="roster.php"><b>roster</b></a> link to see your new club mates.</p>'; mysql_query($test); } //close connection if (!$con) { mysql_close($con); } ?> </div> </div> <!-- end banner-wrap --> <?php include("footer.php"); ?>
×
×
  • 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.