Delaran Posted May 7, 2007 Share Posted May 7, 2007 Hey guys, I'm here again with another novice question. I feel like I should know this one since I went over every tutorial I could find but for some reason, I just can't get this to work. It runs the whole script and tells me that the data was entered correctly, however when I query the database via phpmyadmin it returns -no results-. Obviously, something is wrong. I checked my connection, used multiple print lines to see where it was stopping and it isn't. The code thinks it is inserting these statements no problem, but in reality it isn't. Anyone have suggestions? Code is as follows: /* assign variables */ $id = mysql_escape_string($_GET['id']); $section = mysql_escape_string($_POST['section']); $specialty = mysql_escape_string($_POST['specialty']); /* the function we call to insert.*/ function insert_areas($areas) { print("Function: insert_areas called<br>"); /* create the sql insert query */ $query = create_checkbox_query($areas, "listing_areas"); /* execute the query */ mysql_query($query); } /* helper function for insert_areas(). generates the sctual SQL query */ function create_checkbox_query($arr, $table) { print("Function: create_checkbox_query called<br>"); $q = "INSERT INTO listing_areas (id, section, checked, specialty) VALUES"; foreach ($arr as $check) { $q .= " ( $id , $section, $check , $specialty )" . ","; } /* remove the last comma and return */ return substr($q, 0, -1); } print("<p>At end of php, now I need to call the insert function<p>"); insert_areas($_POST['areas']); ?> All done, entries entered correctly. <br> <a href="http://moogar.home/~pwalton/main.php">Insert/update another entry?</a> </html> As always, any help is much appreciated! I've been banging my head on the desk for hours now. Quote Link to comment https://forums.phpfreaks.com/topic/50419-create_checkbox_query-returns-failed-result/ Share on other sites More sharing options...
benjaminbeazy Posted May 7, 2007 Share Posted May 7, 2007 change this mysql_query($query); to mysql_query($query) or die(mysql_error()); to see if there's an error with the query Quote Link to comment https://forums.phpfreaks.com/topic/50419-create_checkbox_query-returns-failed-result/#findComment-247680 Share on other sites More sharing options...
Delaran Posted May 8, 2007 Author Share Posted May 8, 2007 Alright, I get this error message: SQL query is INSERT INTO listing_areas (directory_listing_id, section, checked, specialty) VALUESYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , 1 , ), ( , , 4 , )' at line 1 Basically, I modified my code and added those mysql_error() codes, along with a print statement to show the exact statement. The problem lies somewhere in this code: function create_checkbox_query($arr, $table) { print("Function: create_checkbox_query called<br>"); $q = "INSERT INTO listing_areas (directory_listing_id, section, checked, specialty) VALUES"; print("SQL query is $q"); foreach ($arr as $check) { $q .= " ( $id , $section, $check , $specialty )" . ","; } /* remove the last comma and return */ return substr($q, 0, -1); } But I'm not sure where. I'll try to play around with it. Quote Link to comment https://forums.phpfreaks.com/topic/50419-create_checkbox_query-returns-failed-result/#findComment-248289 Share on other sites More sharing options...
Delaran Posted May 8, 2007 Author Share Posted May 8, 2007 omg I really can't figure this out. If I keep the code as: /* helper function for insert_areas(). generates the sctual SQL query */ function create_checkbox_query($arr, $table) { print("Function: create_checkbox_query called<br>"); foreach ($arr as $check); $q = "INSERT INTO listing_areas (section, checked, specialty) VALUES ( $section, $check , $specialty )"; print("Query is $q"); /* remove the last comma and return */ return substr($q, 0, -1); } It returns error -- Query is INSERT INTO listing_areas (section, checked, specialty) VALUES ( , 8 , )You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 8 ,' at line 1 However, when I remove the bottom part: /* helper function for insert_areas(). generates the sctual SQL query */ function create_checkbox_query($arr, $table) { print("Function: create_checkbox_query called<br>"); foreach ($arr as $check); $q = "INSERT INTO listing_areas (section, checked, specialty) VALUES ( $section, $check , $specialty )"; print("Query is $q"); It returns error -- Query is INSERT INTO listing_areas (section, checked, specialty) VALUES ( , 8 , )Query was empty I'm completely stuck. These three bits of data are being sent as POST and I am escaping their strings, but nothing shows up. Quote Link to comment https://forums.phpfreaks.com/topic/50419-create_checkbox_query-returns-failed-result/#findComment-248366 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.