raggy99 Posted March 2, 2012 Share Posted March 2, 2012 Hi All, I am having trouble I keep getting an error 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 ') VALUES ('Phone Usage', '777', 'OPS Manager', '')' at line 1 What I am wanting to do is Upload data and link of a file (could be a pic or PDF) into my table and place the file in documents folder. My Form <html> <body> <?php include ('header.php'); ?> <?php include ('menu.php'); ?> <form action="actionaddprocedure.php" method="post" enctype="multipart/form-data" /> <p>Name<input type="text" name="Name"/></p> <p>Procedure Number<input type="text" name="Procedure_number" /></p> <p>Created By <input type="text" name="Created_by" /></p> <p>File Location<input type="file" name="Uploadfile" /> </p><input type="submit" value="Add" /> </form> <?php include ('footer.php'); ?> </body> </html> my action script page. <?php define('DB_NAME', 'raggsweb_oltusers'); define('DB_USER', 'raggsweb_raggs'); define('DB_PASSWORD', 'XXXXXXXX'); define('DB_HOST', 'localhost'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('çould not connect: '. mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die('could not use ' . DB_NAME . ': ' . mysql_error()); } $Name=$_POST['Name']; $Procedure_number=$_POST['Procedure_number']; $Created_by=$_POST['Created_by']; $file=($_FILES['Uploadfile']['Name']); $sql= "INSERT INTO procedures (Name, Procedure_number, Created_by, Uploadedfile,) VALUES ('$_POST[Name]', '$_POST[Procedure_number]', '$_POST[Created_by]', '{$_FILES['Uploadfile']['Name']}')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } //This is the directory where images will be saved $target = "documents/"; $targetx = $target . basename( $_FILES['Uploadfile']['Name']); //Writes the photo to the server if(move_uploaded_file($_FILES['Uploadfile']['tmp_name'], $targetx)) { //Tells you if its all ok } else { //Gives and error if its not die('Error: ' . mysql_error()); } mysql_close(); ?> I am a total Noob at this, I did get this from another website and have modified it to my needs. Thanks Inadvance Quote Link to comment Share on other sites More sharing options...
batwimp Posted March 2, 2012 Share Posted March 2, 2012 What do you get when you echo out your $sql variable? Quote Link to comment Share on other sites More sharing options...
Anon-e-mouse Posted March 3, 2012 Share Posted March 3, 2012 <?php ... $sql= "INSERT INTO procedures (Name, Procedure_number, Created_by, Uploadedfile,) VALUES ('$_POST[Name]', '$_POST[Procedure_number]',.."; ... ?> Point. You have an extra comma after 'Uploadedfile' which should not be there. Quote Link to comment Share on other sites More sharing options...
raggy99 Posted March 3, 2012 Author Share Posted March 3, 2012 Is this the correct way to echo the $sql variable? <?php define('DB_NAME', 'raggsweb_oltusers'); define('DB_USER', 'raggsweb_raggs'); define('DB_PASSWORD', 'XXXXXXX'); define('DB_HOST', 'localhost'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('çould not connect: '. mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die('could not use ' . DB_NAME . ': ' . mysql_error()); } echo($sql); $Name=$_POST['Name']; $Procedure_number=$_POST['Procedure_number']; $Created_by=$_POST['Created_by']; $file=($_FILES['Uploadfile']['Name']); $sql= "INSERT INTO procedures (Name, Procedure_number, Created_by, Uploadedfile) VALUES ('$_POST[Name]', '$_POST[Procedure_number]', '$_POST[Created_by]', '{$_FILES['Uploadfile']['Name']}')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } //This is the directory where images will be saved $target = "documents/"; $targetx = $target . basename( $_FILES['Uploadfile']['Name']); //Writes the photo to the server if(move_uploaded_file($_FILES['Uploadfile']['tmp_name'], $targetx)) { //Tells you if its all ok } else { //Gives and error if its not die('Error: ' . mysql_error()); } mysql_close(); ?> My Error now reads Error: Quote Link to comment Share on other sites More sharing options...
raggy99 Posted March 3, 2012 Author Share Posted March 3, 2012 Further Information. Fields Name, Procedure_number & Created_by are being now added to the database. however the link is not being added to the Uploadedfile field. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 To address the comment about it giving the error just saying "Error:", obviously it's not a mysql error, otherwise it would say something after "Error:", what it looks like the problem is, is that the "move_uploaded_file" is failing, probably because your $targetx is incorrect. I would suggest changing your else statement to say else { //Gives and error if its not die('Error: target='.$targetx.', tmp_name='.$_FILES['Uploadfile']['tmp_name']); } This way you can see exactly what file is attempting to be moved to what folder Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 Further Information. Fields Name, Procedure_number & Created_by are being now added to the database. however the link is not being added to the Uploadedfile field. Change the curly braces where you have '{$_FILES['Uploadfile']['Name']}'. Not sure if that's causing the problem, but a cleaner way to do that is: $uploadedfile_name = $_FILES['Uploadfile']['Name']; $sql= "INSERT INTO procedures (Name, Procedure_number, Created_by, Uploadedfile) VALUES ('$_POST[Name]', '$_POST[Procedure_number]', '$_POST[Created_by]', '$uploadedfile_name')"; that should solve your problem Quote Link to comment Share on other sites More sharing options...
raggy99 Posted March 3, 2012 Author Share Posted March 3, 2012 Thanks DannyB, I am getting closer.I have a better error now. Error: target=documents/, tmp_name=/var/tmp/phpdv7Xk9 Data has saved in the database except the file location. File did not find it's way to the folder. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 Thanks DannyB, I am getting closer.I have a better error now. Error: target=documents/, tmp_name=/var/tmp/phpdv7Xk9 Data has saved in the database except the file location. File did not find it's way to the folder. I may be incorrect, but I don't believe you need the slash at the end of 'documents'. Try removing it. Also, this may be crazy, but make sure that folder actually exists. A last thing to check is that 'documents' is a folder in the directory that this php script is located Quote Link to comment Share on other sites More sharing options...
raggy99 Posted March 3, 2012 Author Share Posted March 3, 2012 //This is the directory where images will be saved $target = "documents"; $targetx = $target . basename( $_FILES['Uploadfile']['Name']); Still getting the error Error: target=documents, tmp_name=/var/tmp/phptA3mSx the folder documents does exist. I have even copied and pasted it from the ftp "filezilla" to double check. Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted March 3, 2012 Share Posted March 3, 2012 I figured it out. You have $_FILES['Uploadfile']['Name'], but the names of the values in the array of the $_FILES variable are all lowercase and you have "Name". Change it to "name" and you should be good. You have $_POST[Name] which is correct because is corresponds to the input fields you specified Quote Link to comment Share on other sites More sharing options...
raggy99 Posted March 8, 2012 Author Share Posted March 8, 2012 You are a legend. Thank you for all your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.