Forum Moderators: coopster

Message Too Old, No Replies

Adding values from dropdowns into db

Won't work if one not selected from each dropdown

         

ksdfla

5:49 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



Hi,

My form has 6 dropdowns, 3 each for vendor selection and 3 each for category selection. If I choose a vendor/category from each of the dropdowns, all is well. But, if I choose only from the first vendor dropdown, and only from the first category dropdown, I get this error message:

Error in query: INSERT INTO recipes (titleID, vendorID, categoryID, approved, date_submitted) VALUES (17, 1, 6, 'Y', NOW()), (17, Select One, Select One, 'Y', NOW()), (17, Select One, Select One, 'Y', NOW()). 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 'Select One, Select One, 'Y', NOW()), (17, Select One, Select On

The 'Select One' is what seems to be causing the problem... but why and what can I do about it?

Here's the code that processes the "add_recipe" page:


<?php

// put variables into something usable
$vendor1 = $_POST['vendor1'];
$vendor2 = $_POST['vendor2'];
$vendor3 = $_POST['vendor3'];
$category1 = $_POST['category1'];
$category2 = $_POST['category2'];
$category3 = $_POST['category3'];
$imagepath = $_POST['imagepath'];
$title = $_POST['title'];
$ingredients = nl2br($_POST['ingredients']);
$directions = nl2br($_POST['directions']);
$contributor = $_POST['contributor'];

// connect to the database
require ("../Connections/mysql_connect2.php");

// add the recipe to the recipe_titles table
$query = "INSERT INTO recipe_titles (imagepath, title, ingredients, directions, contributor) VALUES ('$imagepath', '$title', '$ingredients', '$directions', '$contributor')";
 
// run the query
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$tid = mysql_insert_id(); // get the title id

// insert values into recipes table
if ($tid > 0) { // if it ran ok

// build the query
$query = 'INSERT INTO recipes (titleID, vendorID, categoryID, approved, date_submitted) VALUES ';
if ($_POST['vendor1'] OR $_POST['category1'] > 0) {
 $query .= "($tid, {$_POST['vendor1']}, {$_POST['category1']}, 'Y', NOW()), ";
 }
if ($_POST['vendor2'] OR $_POST['category2'] > 0) {
 $query .= "($tid, {$_POST['vendor2']}, {$_POST['category2']}, 'Y', NOW()), ";
 }
if ($_POST['vendor3'] OR $_POST['category3'] > 0) {
 $query .= "($tid, {$_POST['vendor3']}, {$_POST['category3']}, 'Y', NOW()), ";
 }
 $query = substr ($query, 0, -2); // Chop off last comma and space
 
// run the query
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

if ($result) {
echo '<p><strong>Thank you for your submission</strong></p>';
}

else { // if it did not run okay
echo '<p><font color = "red">Your submission could not be processed due to a system error.</font></p>';
}
}

?>

And the dropdowns in the form:


<?php

// connect to recipe database
require_once ("../Connections/mysql_connect2.php");

// create sql statement
// select vendor names for drop-down list

// create sql statement
// make category pull-down menus from recipe_category table
$query = "SELECT * FROM recipe_vendor ORDER BY vendor ASC";
$result = @mysql_query ($query);
$pulldown = '<option>Select One</option>';
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$pulldown .= "<option value=\"{$row['vendorID']}\">{$row['vendor']}</option>\n";
}
?>

<tr>
<td align="right">Vendor 1:</td>
<td><select name="vendor1"><?php echo $pulldown;?></select></td>
</tr>
<tr>
<td align="right">Vendor 2:</td>
<td><select name="vendor2"><?php echo $pulldown;?></select>
(optional)</td>
</tr>
<tr>
<td align="right">Vendor 3:</td>
<td><select name="vendor3"><?php echo $pulldown;?></select>
(optional)</td>
</tr>

<?php

// create sql statement
// make category pull-down menus from recipe_category table
$query = "SELECT * FROM recipe_category ORDER BY category ASC";
$result = @mysql_query ($query);
$pulldown = '<option>Select One</option>';
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$pulldown .= "<option value=\"{$row['categoryID']}\">{$row['category']}</option>\n";
}
?>
<tr>
<td align="right">Category 1:</td>
<td><select name="category1"><?php echo $pulldown;?></select></td>
</tr>
<tr>
<td align="right">Category 2:</td>
<td><select name="category2"><?php echo $pulldown;?></select>
(optional)</td>
</tr>
<tr>
<td align="right">Category 3:</td>
<td><select name="category3"><?php echo $pulldown;?></select>
(optional)</td>
</tr>
<tr>
<td align="right">Image Path:</td>
<td><input name="imagepath" type="text" id="imagepath" value="recipeimages/clearpixel.gif" size="50"></td>
</tr>
<tr>
<td align="right">Title:</td>
<td><input name="title" type="text" id="title" size="50"></td>
</tr>
<tr>
<td align="right" valign="top">Ingredients:</td>
<td><textarea name="ingredients" cols="50" rows="10" id="ingredients"></textarea></td>
</tr>
<tr>
<td align="right" valign="top">Directions:</td>
<td><textarea name="directions" cols="50" rows="10" id="directions"></textarea></td>
</tr>
<tr>
<td align="right">Contributor:</td>
<td><input name="contributor" type="text" id="contributor" size="50"></td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>

Hope someone can help. I've been working on this all day today with no luck.

Thanks,
ksp

sned

6:02 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



Just from taking a quick glance at your code, could it be that the Select One needs to be in quotes? --
(17, 'Select One', 'Select One', 'Y', NOW())

ksdfla

6:41 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



Hi,

Thanks, I wondered about that too, but I don't think so... of course, I could be wrong. Since that's inserted by the script:

if (($_POST['vendor1']) OR ($_POST['category1']) > 0) {
$query .= "($tid, {$_POST['vendor1']}, {$_POST['category1']}, 'Y', NOW()), ";

I don't know how I'd get the apostrophes in there.

What I've don't to get by for now is to add a value of 'none' to the vendor and category tables. If there aren't any vendors and 'none' is selected in the 2nd and 3rd dropdowns, then it submits without an error, although this is not an efficient way to do this. If there is only one category and one vendor, there should be just one entry in the 'recipe_titles' table, but since the other dropdowns have a value of 'none', then there are two more fields added to the database to reflect that.

Thanks for your suggestion... if that is the problem, I don't know what to do to correct it... I'm very slow at grasping some of the concepts in php.

Kind regards,
Kathy

sned

6:46 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



To add the ' s into the query, I think you could do something like:

$query .= "($tid, '{$_POST['vendor1']}', '{$_POST['category1']}', 'Y', NOW()), ";

Just add them in there, or, just to make the query easier to read, get the variables before hand:

$vendor1 = $_POST['vendor1'];
$category1 = $_POST['category1'];

$query .= "($tid, '$vendor1', '$category1', 'Y', NOW()), ";

Hope that helps, good luck.

ksdfla

8:10 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



Thank you! I no longer get the error message and the items are inserted into the database.

Now I realize I have another question...

If no value is selected on the second or third drop downs, a value of 0 is still inserted in the database. So, one recipe is responsible for two extra entries in the 'recipes' table.

recipeID titleID vendorID categoryID
25 9 1 4
26 9 0 0
27 9 0 0

instead of just

recipeID titleID vendorID categoryID
25 9 1 4

Any thoughts on what I can do about that?

In the database (phpmsql through phpmyadmin), titleID, vendorID and categoryID are unsigned and null, with a default value of 0. I've tried to delete the default value of 0, including deleting those fields and re-creating them, but the default value of 0 always appears there. Is that default value the problem?

Thanks again for the help, it's a relief to have the other problem solved! Hopefully this other will be something simple...

sned

8:23 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



I'm not sure what your form processing script looks like, but I guess the easiest thing would be to check if nothing is selected in the 2nd and 3rd dropdowns, then don't insert anything at all into the database.

From the looks of this
-- if (($_POST['vendor1']) OR ($_POST['category1']) > 0) --

that works for the first dropdown, do you have that same type of checking in place for the 2nd and 3rd dropdowns?

ksdfla

8:58 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



I have the same thing for the second and third dropdowns, so I don't understand why the values are still being inserted...

if ($tid > 0) { // if it ran ok

// build the query
$query = 'INSERT INTO recipes (titleID, vendorID, categoryID, approved, date_submitted) VALUES ';
if (($_POST['vendor1']) OR ($_POST['category1']) > 0) {
//$query .= "($tid, '{$_POST['vendor1']}', '{$_POST['category1']}', 'Y', NOW()), ";
$query .= "($tid, '$vendor1', '$category1', 'Y', NOW()), ";
}
if (($_POST['vendor2']) OR ($_POST['category2']) > 0) {
// $query .= "($tid, '{$_POST['vendor2']}', '{$_POST['category2']}', 'Y', NOW()), ";
$query .= "($tid, '$vendor2', '$category2', 'Y', NOW()), ";
}
if (($_POST['vendor3']) OR ($_POST['category3']) > 0) {
//$query .= "($tid, '{$_POST['vendor3']}', '{$_POST['category3']}', 'Y', NOW()), ";
$query .= "($tid, '$vendor3', '$category3', 'Y', NOW()), ";
}
$query = substr ($query, 0, -2); // Chop off last comma and space

// run the query
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

and the dropdowns in the html form:

// create sql statement
// make vendor pull-down menus from recipe_vendor table
$query = "SELECT * FROM recipe_vendor ORDER BY vendor ASC";
$result = @mysql_query ($query);
$pulldown = '<option>Select One</option>';
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$pulldown .= "<option value=\"{$row['vendorID']}\">{$row['vendor']}</option>\n";
}
?>

<tr>
<td align="right">Vendor 1:</td>
<td><select name="vendor1"><?php echo $pulldown;?></select></td>
</tr>
<tr>
<td align="right">Vendor 2:</td>
<td><select name="vendor2"><?php echo $pulldown;?></select>
(optional)</td>
</tr>
<tr>
<td align="right">Vendor 3:</td>
<td><select name="vendor3"><?php echo $pulldown;?></select>
(optional)</td>
</tr>

This project is so close to being done, but this part is really baffling...

Thanks...

sned

10:08 pm on Jun 14, 2004 (gmt 0)

10+ Year Member



Maybe this was just a typo .. but try this in your if statements:

instead of --
if (($_POST['vendor2']) OR ($_POST['category2']) > 0)

try
if (($_POST['vendor2']) > 0 ¦¦ ($_POST['category2']) > 0)

and the same for the other ifs

jatar_k

10:13 pm on Jun 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



also remember that the WebmasterWorld software breaks pipes

the ¦ in copied code must be replaced with a real pipe character

ksponline

12:54 am on Jun 15, 2004 (gmt 0)

10+ Year Member



I'm home now instead of work (sorry, have a different login on my old home computer)...

I tried the pipe but got the same results...

Is there another--better--way to accomplish the same thing? I'm desperate to get this up and running but I don't know what else to try.

Thanks very much for your time and suggestions.

m_shroom

1:31 am on Jun 15, 2004 (gmt 0)

10+ Year Member



Do not trust user input

Test all user submited variables for valid values.
If one fails send it back to them for correction.
Do no querys untill validation passes.

m_shroom

1:38 am on Jun 15, 2004 (gmt 0)

10+ Year Member



Also if a user returned variable has no value you may have to set a defult value to prevent query errors.

ksponline

1:48 am on Jun 15, 2004 (gmt 0)

10+ Year Member



I'm going to add verification when I know the script is running okay... javascript on the client side, backed up by php validation in the script.

The default values are 0... so it seems like the script should work. That's what's really puzzling:

$query = 'INSERT INTO recipes (titleID, vendorID, categoryID, approved, date_submitted) VALUES ';
if (($_POST['vendor1']) ¦¦ ($_POST['category1']) > 0) {
//$query .= "($tid, '{$_POST['vendor1']}', '{$_POST['category1']}', 'Y', NOW()), ";
$query .= "($tid, '$vendor1', '$category1', 'Y', NOW()), ";
}

Thanks...