Jump to content

[SOLVED] Problem with insert in correct format


grumpy

Recommended Posts

Hi--

I'm having trouble with an insert. My form has Slect Multiple field where users can select category names. This then goes to my processing page. I'm trying to then insert the categories into my table, cat_relations, by the ID matches the category (from my table work_cats), with each category ID in its own row. The insert is working but its inserting the category name rather then the ID that matches the category. I need it to insert the ID that matched the category selected not the category name string. Below is my code. What am I doing wrong? Thanks for your help.

 

// my form

<?

$sql = "SELECT * FROM work_cat ORDER BY cat_name ASC";

 

$result = mysql_query($sql);

 

 

 

?>

<form name="addcats" action="do_addcats2.php" method="POST">

<p>Select a first primary category<br>

<SELECT name="co_workcat[]" size="4" multiple>

<?php

while ($row = mysql_fetch_array($result)) {

$i = $row["cat_name"];

echo '<OPTION value="' . $i . '">' . $i . '</OPTION>';

}

?>

</select>

<p><input type=submit name=submit value=SUBMIT>

 

 

//my processing page

 

$ID = 4;

 

$cats = $_POST['co_workcat'];

foreach($cats as $cat) {

$cat_list1 .= $cat . ' ';

}

;

 

 

$query = mysql_query("SELECT ID FROM work_cat where cat_name = '$cat_list1'");

$result = $query;

// put data into drop-down list box

while ($row = mysql_fetch_array($result)) {

echo ($row["ID"]);

$cat_list1 = $row["ID"];

};

 

 

$sql = "INSERT INTO cat_relations SET relation_usr_id='$ID',relation_cat='$cat_list1'";

if (@mysql_query($sql)) {

echo ("Good");

 

} else {

echo("<p>Error adding cats: " .

mysql_error() . "</p>");

}

 

 

 

 

 

Link to comment
Share on other sites

hope this will work

 

for processing page

 

$

$ID = 4;
$cats = $_POST['co_workcat'];
foreach($cats as $cat) {
$cat_list1 .= $cat . ' ';
}
;


$query = mysql_query("SELECT ID FROM work_cat where cat_name = '$cat_list1'");
$result = $query;
// put data into drop-down list box
while ($row = mysql_fetch_array($result)) { 
echo ($row["ID"]);
$cat_list1 = $row["ID"];
};

foreach ($cats as $strKey => $strValue)
[
$sql = "INSERT INTO cat_relations SET relation_usr_id='$ID',relation_cat='$strValue'"; 
if (@mysql_query($sql)) { 
echo ("Good"); 

} else { 
echo("<p>Error adding cats: " . 
mysql_error() . "</p>"); 
}
}

 

Link to comment
Share on other sites

Nows its retrieving the ID's but:

1 its entering the category id's into the relation_usr_id column, one per row, instead of the relation_usr_id

2. its inserting all the category ID's into each row in the relation_cat column, insted of one category id per row

 

 

Link to comment
Share on other sites

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.