Jump to content

Recommended Posts

Hey guys,

 

I've got this script:

 

$fieldset = $_POST['tfa_Item']; 
$field = $_POST['tfa_ItemName'];
print_r($field);
echo"<br/>";
$field1 = $_POST['tfa_ItemMake'];
print_r($field1);
echo"<br/>";
$field2 =$_POST['tfa_ItemDetails'];
print_r($field2);
echo"<br/>";
$field3 =$_POST['tfa_ItemPrice'];
print_r($field3);
echo"<br/>";

$q1 = "INSERT INTO items_claimed (master_id) VALUES ('$master_id')";

for ($i = 0; $i < count($field); $i++)
  $qSet[] = '(\'' . $field[$i] . '\', \'' . $field2[$i] . '\', \'' . $field2[$i] . '\', \'' . $field3[$i] . '\')';

$q = 'INSERT INTO items_claimed (`item_name`, `item_make`, `item_details`, `item_price`) VALUES ' . implode(', ', $qSet);

if (!mysql_query($q) )
  echo 'could not add!<br>' . mysql_error();

 

which is doing a great job - inserting an array in mySql db table:

 

However I now want to connect tables and I am using:

$master_id = mysql_insert_id($connect);

 

 

So far so good but when I change the query to:

 

$q = 'INSERT INTO items_claimed (`master_id`, `item_name`, `item_make`, `item_details`, `item_price`) VALUES  ' . $master_id .' , ' . implode(', ', $qSet);

 

displays the following error: 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 '84 , ('desk', 'oak', 'oak', '789'), ('notebook', 'nice', 'nice', '')' at line 1

 

where 84 is the master id, ('desk', 'oak', 'oak', '789') - is my first array and ('notebook', 'nice', 'nice', '') - my second array within the multidimensional array.

 

 

Any help will be greatly appreciated!

 

Link to comment
https://forums.phpfreaks.com/topic/99376-need-help-with-mysql-syntax-please/
Share on other sites

 

Hi,

 

Replace this line

 

$q = 'INSERT INTO items_claimed (`master_id`, `item_name`, `item_make`, `item_details`, `item_price`) VALUES  ' . $master_id .' , ' . implode(', ', $qSet);

with this

$q = 'INSERT INTO items_claimed (`master_id`, `item_name`, `item_make`, `item_details`, `item_price`) VALUES  (' . $master_id .' , ' . implode(', ', $qSet));

 

I think you have missed out the () after VALUES. Still learning myself, so im not 100% sure

if this is the actual problem.

try this

$q = 'INSERT INTO items_claimed (`master_id`, `item_name`, `item_make`, `item_details`, `item_price`) VALUES  (' . $master_id .' , \'' . implode('\', \'', $qSet).'\')';

 

i only skimmed the question (note they are 2x  single quotes,not double quotes)

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.