Jump to content

Recommended Posts

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.

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.

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.

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.