Jump to content

Upload Problem


SkyRanger

Recommended Posts

Hey Guys, can somebody please have alook at my code, when I don't have the mysql stuff in it works not problem, when I add the mysql it does not want to upload the file.

 

Thanks

 

 


<?php

// Where the file is going to be placed 
$target_path = "../uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

{
$con = mysql_connect("localhost","******", "*******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("******", $con);

mysql_query("INSERT INTO table (sname, smonth, sday, syear, sdload)
VALUES ('$sname', '$smonth', '$sday', '$syear', '$fileupload')");

mysql_close($con);

echo "Successful<BR/>";
echo "<a href=index2.php>Return to admin section</a>";
}
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

Link to comment
Share on other sites

No the data is not even inserting into the database.  lol, no I guess I just got carried away removing all the private info out the code.  Seems it is bypassing everything and going right to the error: 

 

} else{
    echo "There was an error uploading the file, please try again!";
}

Link to comment
Share on other sites

Then it's likely your query is failing, but there's no logic in place to test for that. You should at least make sure the query executed without error, and ideally verify that a record was actually inserted into the table.

 

pseudo-code

$query = "INSERT INTO `table` (field1, field2) VALUES ('value1', 'value2')";
if( !$result = mysql_query($query) ) {
     echo '<br>A database error has occurred.'; // UNCOMMENT *NEXT* LINE IN DEVELOPMENT ENVIRONMENT
     //echo "<br>Query string: $query<br>Produced error: " . mysql_error() . '<br>';
} else {
     if( mysql_num_rows($result) < 1 ) {
          echo '<br>Query executed successfully, but no record was inserted.<br>';
     }
}

Link to comment
Share on other sites

Solved part of the problem, got the data to insert but messed up on the image part,  trying to figure out how to get the name of the image to insert.

 

Tried:

 

$fileupload = $_FILES['uploadedfile']['name'];

and

$fileupload = basename( $_FILES['uploadedfile']['name']);

 

wondering if that might be causing the upload problem also.

 

 

Link to comment
Share on other sites

Anybody have any idea why this is not working....

 

Here is the form

 


						<form action="addedfile.php" method="post">

Title of PDF:<br />
<input type="text" name="sname" size="50" />
<br /><br />
Date:<br />

<select name="smonth" size="1">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="11">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select><select name="sday" size="1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select><select name="syear" size="1">
<option value="11">2011</option>
<option value="12">2012</option>
<option value="13">2013</option>
<option value="14">2014</option>
</select>
<br/><br />
Choose File:<br />
<input type="file" name="uploadedfile" /><br />
 <br />
<input class="button" name="Submit1" type="submit" value="submit" /><input class="button" name="Reset1" type="reset" value="reset" /></form>


Link to comment
Share on other sites

In order to work, your <form> tag will need to contain enctype="multipart/form-data. You could also simplify the <select> fields by generating them on the fly.

 

<?php
// MONTHS
$months = array(1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$sel_month = "<select name=\"smonth\">\n";
foreach( $months as $k => $v ) {
$sel_month .= "<option value=\"$k\">$v</option>\n";
}
$sel_month .= "</select>\n";

// DAYS
$days = range(1, 31);
$sel_day = "<select name=\"sday\">\n";
foreach( $days as $v ) {
$sel_day .= "<option value=\"$v\">$v</option>\n";
}
$sel_day .= "</select>\n";

// YEARS
$start_yr = date('Y');
$end_yr = $start_yr + 3;
$years = range( $start_yr, $end_yr );
$sel_year = "<select name=\"syear\">\n";
foreach ($years as $v) {
$sel_year .= "<option value=\"$v\">$v</option>\n";
}
$sel_year .= "</select>\n";
?>

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

Title of PDF:<br />
<input type="text" name="sname" size="50" />
<br /><br />
Date:<br />

<?php
echo $sel_month;
echo $sel_day;
echo $sel_year;
?>

<br/><br />
Choose File:<br />
<input type="file" name="uploadedfile" /><br />
 <br />
<input class="button" name="Submit1" type="submit" value="submit" />
<input class="button" name="Reset1" type="reset" value="reset" />
</form>

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.