Jump to content

INSERT INTO problem


WSparrow

Recommended Posts

Hey there night owls, been fighting with this one all night.

 

I'm getting an unexpected T_STRING error on line 28 of the following

<?php 

error_reporting(E_ALL);
include 'mysql-connect.php';

{



if (!empty($_POST['upload']))
{
//Loop thru Post Array
foreach($_POST as $key => $value) {
$key = $value;
}


if(isset($_POST[’upload’]) && $_FILES[’SpecialFile’][’size’] > 0) {
$fileName = $_FILES[’SpecialFile’][’name’];
$tmpName = $_FILES[’SpecialFile’][’tmp_name’];
$fileSize = $_FILES[’SpecialFile’][’size’];
$fileType = $_FILES[’SpecialFile’][’type’];
$fp= fopen($tmpName, ‘r’);
$content = addslashes($tmpName);
fclose($fp);
}
if(!get_magic_quotes_gpc()) {
$fileName = addslashes($fileName);
}
$keyword = mysql_real_escape_string($_POST['keyword']);

$description = mysql_real_escape_string($_POST['description']);

$query = “INSERT INTO UploadedFiles(name, size, type, content, keyword, description)
VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$keyword' '$description')”; or die(mysql_error());

$result = mysql_query($query);
if (!$result) {
dberror (mysql_error(), $_SERVER[’PHP_SELF’] );
echo mysql_error();
}			

//Display Confirmation
$message = urlencode(”Your file has been uploaded.”);
header( “Location: confirmed.php?m=$message” );
exit;
}
}
?>

 

I've checked it again and again for missing quotes, brackets, etc.  I can't find the problem.  Anyone see what I'm missing?

 

Thanks

Link to comment
Share on other sites

I think the problem might be to do with your single and double quotes

 

this ’ should be this '

and this ” should be "

 

also you are setting every $_POST to the variable $key and then changing it every time it loops so you would only have 1 $key variable and the value would be that of the last $_POST in the array

Link to comment
Share on other sites

Well I thought I had this figured out.  But now the page loads blank with "your+file+has+been+uploaded" appended to the URL.  It's not giving me any errors though.  Here's the revised code:

 

<?php 

error_reporting(E_ALL);

include 'mysql-connect.php';
$uploaddir = './temp';


if (!empty($_FILES['file']))

{

foreach ($_FILES['fileName']['error'] as $key => $error) {

$key = $error;
if ($error == UPLOAD_ERR_OK) {
}




$fileName = $_FILES['file']['name'][$key];

$tmpName = $_FILES['file']['tmp_name'][$key];

$fileSize = $_FILES['file']['size'];

$fileType = $_FILES['file']['type'];

$fp= fopen($tmpName, 'r');

$content = addslashes($tmpName);

fclose($fp);


if(!get_magic_quotes_gpc()) {

$fileName = addslashes($fileName);

}

$keyword = mysql_real_escape_string($_POST['keyword']);

$description = mysql_real_escape_string($_POST['description']);




$query = "INSERT INTO UploadedFiles (name, size, type, content, keyword, description)

VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$keyword' '$description')" or die(mysql_error());



$result = mysql_query($query);

if (!$result) {

dberror (mysql_error(), $_SERVER['PHP_SELF'] );

echo mysql_error();

}
}
}			

//Display Confirmation

$message = urlencode("Your file has been uploaded.");

header( "Location: confirm.php?m=$message" );

exit;





?>

<html>

<head>

<title>File Upload Form</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">



<link href="styles2.css" rel="stylesheet" type="text/css">

<style type="text/css">

<!--

body {

background-repeat: repeat-y;

background-color: #000;

margin-left: 9.5%;

}

-->

</style></head>



<body>

<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">

  <tr> 

    <td height="126" colspan="3"> </td>

  </tr>

  <tr> 

    <td width="160" valign="top">

    <p> </p>

    <p> </p>

    <div class="myaccount">

  <p><strong>My Account</strong></p>

  <a href="myaccount.php">My Account</a><br>

  <a href="mysettings.php">My Settings</a><br>

    <a href="Fresh.php">Site Map</a><br>

    <a href="art.php">Art</a><br>

    <a href="genres.php">Music</a><br>

    <a href="think.php">Think Tank</a><br>

    <a href="writing.php">Writing</a><br>

    <a href="help.php">Help</a><br>

    <a href="logout.php">Logout </a>



  <p>The Best thing since Kleenex</p></div>

    <td width="732" valign="top"><p><p> </p>

</p>

      <h3 class="titlehdr">File Uploads</h3>

      <p>Share your work here! Please note that fields marked <span class="required">*</span> 

        are required.</p>





  <br>

      <form action="upload.php" method="post" name="upload" enctype="multipart/form-data" >

        <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">

          <tr> 
<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />

            <td colspan="2"><label for="name">Filename:</label>

<input type="file" name="file" id="file" /></td>

          </tr>

          <tr> 

            <td colspan="2"> </td>

          </tr>

          <tr> 

            <td colspan="2">Description<span class="required"><font color="#CC0000">*</font></span><br> 

              <textarea name="description" cols="40" rows="4" id="description" class="required"></textarea> 

              <span class="example"></span> </td>

          </tr>

          <tr> 

            <td>Category <font color="#CC0000">*</font></span></td>

            <td><select name="keyword" class="required" id="select5">

                <option value="" selected></option>

                <option value="" disabled="disabled"> ---- Art ----</option>

                <option value="Drawing">Drawing</option>

                <option value="Film">Film</option>

                <option value="Painting">Paint</option>

                <option value="Photography">Photography</option>

                <option value="Sculpture">Sculpture</option>

                <option value="" disabled="disabled"> ---- Music ----</option>

                <option value="Blues">Blues</option>

                <option value="Classical">Classical</option>

                <option value="Country">Country</option>

                <option value="Electronic">Electronic</option>

                <option value="International">International</option>

                <option value="Jazz">Jazz</option>

                <option value="Latin">Latin</option>

                <option value="RB">R and B</option>

                <option value="Raggae">Raggae</option>

                <option value="Rock">Rock</option>

                <option value="" disabled="disabled"> ---- Writing ----</option>

                <option value="NonFiction">Non-Fiction</option>

                <option value="Novellas">Novellas</option>

                <option value="Novels">Novels</option>

                <option value="Shorts">Short Stories</option>

                <option value="Rants">Rants</option>

               </select></td>

          </tr>

        </table>

        <p align="center">

          <input type="submit" id="uploadForm" value="Submit">

        </p>

      </form>

      

   

    </td>

    <td width="196" valign="top"> </td>

  </tr>

  <tr> 

    <td colspan="3"> </td>

  </tr>

</table>



</body>

</html>

 

Any bright ideas guys?

And again, when I copy and paste there's way more empty lines.  Encoding issue?

Link to comment
Share on other sites

Well I'm trying to get it to run a file upload form (the HTML at the bottom).  But instead it's just skipping to the 'confirm.php' at the end of the php portion.  That's not supposed to happen until the file has been uploaded.

 

Earlier, I had this section

if (!empty($_FILES['file']))

{

foreach ($_FILES['fileName']['error'] as $key => $error) {

$key = $error;
if ($error == UPLOAD_ERR_OK) {
}

left out and the page was loading properly, but it was just reloading the page when you hit 'submit'.  Since this is all I've modified, I kind of suspect that's where the problem is, but I could be wrong.

Link to comment
Share on other sites

I should also point out that I don't know much at all about arrays.  I don't know if I need to actually define an array for this (I haven't in my includes) or how to go about doing that.

 

I moved the three curly braces that were just before the 'display confirmation' part, to just after it, and now the page loads properly, but it still just reloads the page when you click 'submit'

Link to comment
Share on other sites

The confirmation dialog and the header() redirect are in the code in a manner that they run unconditionally. You'd need to establish under what specific conditions that part of the code should execute, and use that information to formulate your if() { conditional for it.

 

Oh, and please don't bump threads after only half an hour . . .

Link to comment
Share on other sites

Okay I changed the confirmation to a simple echo.  I've attempted to define an array.  I'm still getting no errors but 'submit' just reloads the page without even attempting to upload.

My revised code:

<?php 

error_reporting(E_ALL);
include 'dbc.php';
page_protect();

include 'mysql-connect.php';

$uploaddir = './temp';

$_FILES = array("name", "size", "type", "content", "keyword", "description");



if (!empty($_FILES['file']))

{

foreach ($_FILES['fileName']['error'] as $key => $error) {

$key = $error;

if ($error == UPLOAD_ERR_OK) {

}





$fileName = $_FILES['file']['name'][$key];

$tmpName = $_FILES['file']['tmp_name'][$key];

$fileSize = $_FILES['file']['size'];

$fileType = $_FILES['file']['type'];

$fp= fopen($tmpName, 'r');

$content = addslashes($tmpName);

fclose($fp);



if(!get_magic_quotes_gpc()) {

$fileName = addslashes($fileName);

}

$keyword = mysql_real_escape_string($_POST['keyword']);

$description = mysql_real_escape_string($_POST['description']);





$query = "INSERT INTO UploadedFiles (name, size, type, content, keyword, description)

VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$keyword' '$description')" or die(mysql_error());



$result = mysql_query($query);

if (!$result) {

dberror (mysql_error(), $_SERVER['PHP_SELF'] );

echo mysql_error();



//Display Confirmation
if (mysql_affected_rows($result) >0) 

echo "<h1>File sucessfully uploaded</h1>";
exit; 
}

}

}



?>

<html>

<head>

<title>File Upload Form</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">



<link href="styles2.css" rel="stylesheet" type="text/css">

<style type="text/css">

<!--

body {

background-repeat: repeat-y;

background-color: #000;

margin-left: 9.5%;

}

-->

</style></head>



<body>

<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">

  <tr> 

    <td height="126" colspan="3"> </td>

  </tr>

  <tr> 

    <td width="160" valign="top">

    <p> </p>

    <p> </p>

    <div class="myaccount">

  <p><strong>My Account</strong></p>

  <a href="myaccount.php">My Account</a><br>

  <a href="mysettings.php">My Settings</a><br>

    <a href="Fresh.php">Site Map</a><br>

    <a href="art.php">Art</a><br>

    <a href="genres.php">Music</a><br>

    <a href="think.php">Think Tank</a><br>

    <a href="writing.php">Writing</a><br>

    <a href="help.php">Help</a><br>

    <a href="logout.php">Logout </a>



  <p>The Best thing since Kleenex</p></div>

    <td width="732" valign="top"><p><p> </p>

</p>

      <h3 class="titlehdr">File Uploads</h3>

      <p>Share your work here! Please note that fields marked <span class="required">*</span> 

        are required.</p>





  <br>

      <form action="upload.php" method="post" name="upload" enctype="multipart/form-data" >

        <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">

          <tr> 

<input type="hidden" name="MAX_FILE_SIZE" value="300000000" />

            <td colspan="2"><label for="name">Filename:</label>

<input type="file" name="file" id="file" /></td>

          </tr>

          <tr> 

            <td colspan="2"> </td>

          </tr>

          <tr> 

            <td colspan="2">Description<span class="required"><font color="#CC0000">*</font></span><br> 

              <textarea name="description" cols="40" rows="4" id="description" class="required"></textarea> 

              <span class="example"></span> </td>

          </tr>

          <tr> 

            <td>Category <font color="#CC0000">*</font></span></td>

            <td><select name="keyword" class="required" id="select5">

                <option value="" selected></option>

                <option value="" disabled="disabled"> ---- Art ----</option>

                <option value="Drawing">Drawing</option>

                <option value="Film">Film</option>

                <option value="Painting">Paint</option>

                <option value="Photography">Photography</option>

                <option value="Sculpture">Sculpture</option>

                <option value="" disabled="disabled"> ---- Music ----</option>

                <option value="Blues">Blues</option>

                <option value="Classical">Classical</option>

                <option value="Country">Country</option>

                <option value="Electronic">Electronic</option>

                <option value="International">International</option>

                <option value="Jazz">Jazz</option>

                <option value="Latin">Latin</option>

                <option value="RB">R and B</option>

                <option value="Raggae">Raggae</option>

                <option value="Rock">Rock</option>

                <option value="" disabled="disabled"> ---- Writing ----</option>

                <option value="NonFiction">Non-Fiction</option>

                <option value="Novellas">Novellas</option>

                <option value="Novels">Novels</option>

                <option value="Shorts">Short Stories</option>

                <option value="Rants">Rants</option>

               </select></td>

          </tr>

        </table>

        <p align="center">

          <input type="submit" id="uploadForm" value="Submit">

        </p>

      </form>

      

   

    </td>

    <td width="196" valign="top"> </td>

  </tr>

  <tr> 

    <td colspan="3"> </td>

  </tr>

</table>



</body>

</html>

 

And yes, the temp folder it's pointing to does exist.  I'm at a loss.

Link to comment
Share on other sites

Well I've tried changing the array setup to the following:

 

$uploaddir = './temp';
$uploaddir = $uploaddir . basename ($_POST['file']['name']);

$_FILES = array(

"name" => ($_POST['file']['name']),
"size" => ($_POST['file']['size']),
"type" => ($_POST['file']['type']),
"content" => ($_POST['file']),
"keyword" => ($_POST['keyword']),
"description" => ($_POST['description']));

 

I'm still not getting any errors, but I'm getting 'undefined index' warns on all six of the last lines in the segment.  But don't those lines define the variables?

Link to comment
Share on other sites

The errors I'm getting are:

 

Undefined index: file in /upload.php on line 8,

  Undefined index: file in /upload.php on line 11,

Undefined index: file in /upload.php on line 12,

Undefined index: file in /upload.php on line 13,

Undefined index: file in /upload.php on line 14,

 

These lines work out to line 8 being

$uploaddir = $uploaddir . basename ($_POST['file']['name']);

 

and so on.  But how do I define "file" when it's being posted from the form? Something like: 'file' = ($_POST(content)) maybe?

 

Link to comment
Share on other sites

Hey,

 

For your first problem with the sql INSERT INTO, I'm afraid I found a missing comma. I hope this thing must have been fixed in your actual php page. But thought I should point this out..

 

$query = "INSERT INTO UploadedFiles (name, size, type, content, keyword, description)

VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$keyword' '$description')" or die(mysql_error());

 

There is a MISSING COMMA in between '$keyword' '$description'

 

Check it out.

I'll look into the second problem of yours, I'm a beginner, so I'm not sure I can catch it.

Cheers!

Link to comment
Share on other sites

Get rid of this:

$_FILES = array("name", "size", "type", "content", "keyword", "description");

 

If this is still there, get rid of it also:

$_FILES = array(

"name" => ($_POST['file']['name']),
"size" => ($_POST['file']['size']),
"type" => ($_POST['file']['type']),
"content" => ($_POST['file']),
"keyword" => ($_POST['keyword']),
"description" => ($_POST['description']));

 

Immediately after error_reporting(E_ALL); add this, then post the output it generates after the form has been submitted:

echo '$_POST array contains:<br><pre>';
print_r($_POST);
echo '</pre><br>$_FILES array contains:<br><pre>';
print_r($_FILES);
echo '</pre>';

Link to comment
Share on other sites

Okay, I've made all the changes suggested and the print_r output is

 

$_POST array contains:

 

Array

(

)

 

 

$_FILES array contains:

 

Array

(

)

Notice: Undefined index: file in /var/www/html/www.domain.com/upload.php on line 14

 

The undefined index notice refers to:

$uploaddir = $uploaddir . basename ($_POST['file']['name']);

 

I don't get why they'd be empty, my form uses:

<form method="POST" name="file" enctype="multipart/form-data" action=<?php echo $_SERVER['PHP_SELF'] ?> 

I'm about ready to start from scratch on this one.  Anybody have a good upload form/script?

Link to comment
Share on other sites

Assuming that what you posted was AFTER you attempted to upload a file, the $_FILES array will be empty for the following reasons -

 

1) Uploads are not enabled on your server. What does a phpinfo(); statement show for the file_uploads setting?

 

2) The HTML of your form is invalid and the browser is not bothering to actually upload the file. What's the current code on the page? Also, Firefox won't upload a zero length file.

 

3) You are exceeding the post_max_size setting. What size file are you attempting to upload and what does a phpinfo(); statement show for the post_max_size setting?

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.