Jump to content

Upload Image and Data Error


raggy99

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/258151-upload-image-and-data-error/
Share on other sites

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:

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

 

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

 

 

 

 

 

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

//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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.