0

Im using the following function:

function Add_Oddjob ($Add_Oddjob){
            global $MemberID;
            $update = array();
            array_walk($Add_Oddjob, 'array_sanitize');

            foreach($Add_Oddjob as $field=>$data){ //loop through update data in Add_Oddjob.php
                $update[] = '`' . $field . '` = \'' . $data . '\''; 
            }
            mysql_query("INSERT INTO `oddjob`($field) VALUES ($data)");

Add_Oddjob.php

if (isset($_POST['OddJobName']) && isset($_POST['Description']) && isset($_POST['DaysAvailable']) && empty($errors) === true){//if (empty($_POST) === false && empty($errors) === true) { //if (isset(empty($_POST['OddJobName'])) && isset(empty($_POST['Description'])) && isset(empty($_POST['DaysAvailable'])) === false && empty($errors) === true)
            $daysavailable='';
            foreach ($_POST['DaysAvailable'] as $value)
            {

            $daysavailable .=$value." ";
            }

            $Add_Oddjob = array (
                'MemberID'      => $MemberID,
                'OddJobName'    => $_POST['OddJobName'],
                'Description'   => $_POST['Description'],
                'DaysAvailable' => $daysavailable, 

                );

                Add_Oddjob ($Add_Oddjob);

                if(success){
                 header('Location: member.php?username='.$username);
                 exit ();
                }
            } else if (empty($errors) === false){
                //otherwise output errors
                echo output_errors($errors);
            }

But when I log in as a user and enter data i get 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 'WHERE MemberID =' at line 1

The MemberID in the Oddjob table is a primary key in the Member Table. The idea is that each member can have more than one odd job in the odd job table. Do I need to do some sort of join maybe?

Also If i try the insert query in phpmyadmin:

   INSERT INTO `oddjob`(`OddJobName`, `Description`, `DaysAvailable`) VALUES (painter,test,Friday) WHERE `MemberID` = 35

I get this error:

#1064 - 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 'WHERE `MemberID` = 35' at line 1

Any help would be great!

EDIT

So I removed the WHERE clause from my query because I didnt realise I couldnt have that in an insert query. Now if i echo the query i see: INSERT INTOoddjob(DaysAvailable) VALUES (Wednesday ) for some reason its not picking up any of the other fields. Any ideas why?

Lairds
  • 87
  • 4
  • 12

2 Answers2

2

I think you are missing out quotes.. try this

  INSERT INTO `oddjob`(`OddJobName`, `Description`, `DaysAvailable`) VALUES (painter,test,Friday) WHERE `MemberID` = 35

you can use update statement instead of insert .. pls take a look at this link

How to add a where clause in a MySQL Insert statement?

Community
  • 1
  • 1
srinath
  • 2,748
  • 6
  • 35
  • 56
  • I think adding some explanation will improve the beauty. Still good answer. +1 –  Apr 09 '13 at 17:06
  • @srinath thank you, I didn't realize that i couldn't use a where clause in an insert query. – Lairds Apr 09 '13 at 18:00
0

I couldn't understand what is use of where clause in Insert statement. Are you trying to insert record after member Id 35? You cannot use conditional statement with Insert query.

Just remove where clause i guess it should work for u

INSERT INTO `oddjob`(`OddJobName`, `Description`, `DaysAvailable`) VALUES (painter,test,Friday)