0

I have this code in my form:

<form id="addForm" action="php/add-article.php" method="POST" enctype="multipart/form-data">
            <table>
                <tr>    
                    <td class="tableLeft">Article Photo:</td>
                    <td class="tableRight"><input id="formPhoto" class="addTextInput" name="photo" type="file" /></td>
                    <td id="validatePhoto"></td>
                </tr>
                <tr>    
                    <td class="tableLeft">Article Photo Alt:</td>
                    <td class="tableRight"><input id="formAlt" class="addTextInput" name="alt" type="text" /></td>
                    <td id="validateAlt"></td>
                </tr>
                <tr>    
                    <td class="tableLeft">Article Title:</td>
                    <td class="tableRight"><input id="formTitle" class="addTextInput" name="title" type="text" /></td>
                    <td id="validateTitle"></td>
                </tr>
                <tr>
                    <td class="tableLeft">Article Body:</td>
                    <td class="tableRight"><textarea id="formArticle" class="addTextInput" rows="6" name="article"></textarea></td>
                    <td id="validateArticle"></td>
                </tr>
                <tr>
                    <td class="tableLeft"></td>
                    <td id="validateSending" class="tableRight"></td>
                </tr>
                <tr>
                    <td class="tableLeft"></td>
                    <td class="tableRight"><input id="formSubmit" class="addSubmitInput" type="submit" value="Add This" /></td>
                </tr>
            </table>
        </form>

And this in a php file (add-article.php):

<?php
$time = time();
$id= time().''.mt_rand(1000, 9999);
$year = date("Y");
$path = "../images/$year/";

$title = ucwords($_POST['title']);
$article = $_POST['article'];
$alt = $_POST['alt'];

$extension = end(explode(".", $_FILES["photo"]["name"]));

$added = date("Y-m-d H:i:s");
$views = "0";
?>
<?php
$insert_post_sql = "INSERT INTO articles (id, photo, alt, title, article, added, views) VALUES('$id', '.$extension', '$alt', '$title', '$article', '$added', '$views')";
$insert_post_res = mysqli_query($con, $insert_post_sql);
if(mysqli_affected_rows($con)>0){
move_uploaded_file($_FILES["photo"]["tmp_name"],"$path" . $id . "." . $extension);
header("Location: ../article.php?id=$id");
exit();
}
else{
echo "0";
};
?>

When I run this on my localhost, everything works compltely fine yet when I do it on my live site it echo's 0 and says that photo, alt, title and article are uindefined.

Does anyone know what the reason for this might be?

user3177012
  • 663
  • 2
  • 8
  • 19
  • What do you mean, undefined values in DB or PHP variables? – Domain Nov 15 '14 at 10:49
  • Inside the server error log it says: `PHP Notice: Undefined index: title in ...` – user3177012 Nov 15 '14 at 10:52
  • You need to check if the form submitted or not? Local server ignore this kind of error but live server dont. Give a name to your button and check if the button isset . Let me know if you need details. – William Francis Gomes Nov 15 '14 at 10:58
  • At the first line write print_r( $_POST ) to see posted data. And it is always better to check that variable or index in array is present by isset() before use. – Domain Nov 15 '14 at 11:00
  • @WisdmLabs Thanks but that doesn't print anything - Does the form look alright to you? Meaning does it look like a form that should work correctly? I'm using exactly the same form & PHP script on another website on the same server and that one works completely fine – user3177012 Nov 15 '14 at 11:05

3 Answers3

0

The main reason should be the move_uploaded_file permission to write in the specified path on the production server.

George
  • 310
  • 1
  • 9
  • `move_uploaded_file` happens only after the row is affected & as nothing is being added to the database this can't be the reason – user3177012 Nov 15 '14 at 10:53
  • Yes you are right, also check this: http://stackoverflow.com/a/16054754/2289750 if i's any help because it seems that _POST vars does not arrive to your add-article.php. Try using _GET to see if it works first – George Nov 15 '14 at 11:08
  • Well I'm using pretty much exaclty the same scripting on another website, hosted on the same server and that uploads the form data fine so I don't think it's an error with _POST – user3177012 Nov 15 '14 at 11:14
0

The problem is from your sql statement... it couldnt find the photo, alt etc... on the table header that is why it returned 0 records, hence, echoing 0 as your program demands....

  • Yeah I know that but why is it not finding them? They have been posted through the form correctly - This works on my localhost, just not on the server when I upload it – user3177012 Nov 15 '14 at 10:54
  • ok then, try to escape the data received from the form using mysql_real_escape_string or addslashes before you insert them onto the database –  Nov 15 '14 at 10:57
  • and make your that you have selected a database for the query after the connection –  Nov 15 '14 at 10:59
0

I edited the last <tr> of your form

<tr>
   <td class="tableLeft"></td>
   <td class="tableRight"><button id="formSubmit" class="addSubmitInput" type="submit" name="submit">Add This </button></td>
</tr>

Now try below PHP code:

<?php
$time = time();
$id = time() . '' . mt_rand(1000, 9999);
$year = date("Y");
$path = "../images/$year/";

if (isset($_POST['submit'])) {
    $title = ucwords($_POST['title']);
    $article = $_POST['article'];
    $alt = $_POST['alt'];

    $extension = end(explode(".", $_FILES["photo"]["name"]));

    $added = date("Y-m-d H:i:s");
    $views = "0";

    $insert_post_sql = "INSERT INTO articles (id, photo, alt, title, article, added, views) VALUES('$id', '.$extension', '$alt', '$title', '$article', '$added', '$views')";
    $insert_post_res = mysqli_query($con, $insert_post_sql);
    if (mysqli_affected_rows($con) > 0) {
        move_uploaded_file($_FILES["photo"]["tmp_name"], "$path" . $id . "." . $extension);
        header("Location: ../article.php?id=$id");
        exit();
    } else {
        echo "0";
    };
}
?>

Let me know if its working for you now. Regards.