-2

I had this query using php in inserting images after submitting the form It says "Requirements submitted succesfully" but there is no data inserted in database.

This is my code so far:

if(isset($_POST['sumit'])){
    $count = count($_FILES);
    $query = "SELECT * FROM dummyclients_tbl WHERE user_id = '".$_SESSION['user']."'";
    if (!$result = mysql_query($query)) {
        exit(mysql_error());
    }
    if(mysql_num_rows($result)){
        $row = mysql_fetch_assoc($result);
        $sid = $row['user_id'];
        $coll =$row['college'];
        $stat = "Pending";

        $query = "INSERT INTO request_tbl (user_id,document_id,imgreq1,imgreq2,imgreq3,imgreq4,imgreq5,imgreq6,imgreq7,request_status,college) VALUES ('$sid','$passed_id'";
        for($i = 1; $i <= $count; ++$i){
            if(is_uploaded_file($_FILES['imgreq'.$i]['tmp_name']) && $_FILES['imgreq'.$i]['size']){
                $query .= ",'" . base64_encode(file_get_contents(addslashes($_FILES['imgreq'.$i]['tmp_name']))) . "'";
            }else{
                $query .= ",NULL";
            }
        }
        $query .= ",'$stat','$coll')";
        ?>
            <script>alert('Requirements Successfully Submitted!');</script>
        <?php
       // saveimage($query);
    }
    else{

        ?>
            <script>alert('Error while submitting form!');</script>
        <?php

    }
}      

I dont know where did I go wrong so please if anyone can help I appreciate it. Thanks.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Rae Kun
  • 13
  • 5
  • 2
    Nowhere do you actually *run* your query. You need to execute `$query` (right before your "Requirements Successfully Submitted" alert) – random_user_name Jan 30 '17 at 16:12
  • Also, see the thousands of posts that tell you NOT to use `mysql_`, instead use PDO or mysqli_: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – random_user_name Jan 30 '17 at 16:13
  • Thanks @cale_b I am well aware of that but we are required to use mysql for the mean time while learning. – Rae Kun Jan 30 '17 at 16:15
  • Your code failed; use error reporting http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Jan 30 '17 at 16:33
  • and `$query = "INSERT INTO request_tbl` was never queried with `mysql_query()`. You're using the same variable for both queries here, being `$query`. – Funk Forty Niner Jan 30 '17 at 16:33

2 Answers2

0

So it is true that I did not execute the query and forgot to put mysql_query($query); after $query .= ",'$stat','$coll')"; . And that lead me to solving another problem wherein I did not set the fields in the database to receive NULL values which is the cause of the error.

Rae Kun
  • 13
  • 5
-1

after:

$query .= ",'$stat','$coll')";

add

mysql_query($query)
Grzegorz
  • 121
  • 5
  • It still throws the message "Requirements successfully submitted" but still no data is inserted in the database. – Rae Kun Jan 30 '17 at 16:28
  • quite possible parameters number now is not valid. before mysql_query($query) add "echo $query" and try to submit it into phpmyadmin - you will find error than. ouch, and mysql_ is quite old and you should move to mysqli_... – Grzegorz Jan 30 '17 at 16:33
  • It is now working fine. I just forgot to set the variables to accept `NULL` values. Anyways thanks @ Small Web Dev you helped a lot. – Rae Kun Jan 30 '17 at 17:03