How to fix php/mysqli error below

I have a code below where I am trying to upload files in to a sever and insert data into 2 tables, (one into the 'Image' Table and one in the 'Image_Question' Table)

At the moment it is uploading the files into the server and inserting the data into the 'Image' Table but it is not inserting data into the second table "Image_Question" table.

Now the reason I believe it is not inserting data into the "Image_Question" table is because I am recieving this error after uploading a file:

Notice: Undefined index: numQuestion in /web/stud/u0867587/Mobile_app/imageupload2.php on line 98 Warning: mysqli_stmt::execute(): (23000/1048): Column 'QuestionId' cannot be null in /web/stud/u0000000/Mobile_app/imageupload2.php on line 104 array(0) { }

So my question is that how can the error above be fixed so that it would be able to insert data into the "Image_Question" table?

Below is what the Image_Question table should look like:

ImageId        SessionId      QuestionId
100                  AAA                      3
101                  SDE                      7

Below is the code where it uploads the file and inserts data into the database:

<?php  
    /* check connection */  
    if (mysqli_connect_errno()) {  
        printf("Connect failed: %s\n", mysqli_connect_error());  
        die();  
    }  
 
    $result = 0;  
 
    //UPLOAD IMAGE FILE  
 
    move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);  
 
    $result = 1;  
 
    //INSERT INTO IMAGE DATABASE TABLE  
 
    $imagesql = "INSERT INTO Image (ImageFile) VALUES (?)";  
 
    if (!$insert = $mysqli->prepare($imagesql)) {  
        // Handle errors with prepare operation here  
    }  
 
    //Dont pass data directly to bind_param store it in a variable  
    $insert->bind_param("s", $img);  
 
    //Assign the variable  
    $img = 'ImageFiles/' . $_FILES['fileImage']['name'];  
 
    $insert->execute();  
 
    $insertimagequestion->execute();  
 
    //IF ANY ERROR WHILE INSERTING DATA INTO EITHER OF THE TABLES  
    if ($insert->errno) {  
      // Handle query error here  
    }  
 
    $insert->close();  
    
    $lastID = $mysqli->insert_id;         
 
 $imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId)  
    VALUES (?, ?, ?)"; 
    
     if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) { 
      // Handle errors with prepare operation here 
       echo "Prepare statement err imagequestion"; 
    } 
    
$qnum = $_POST['numQuestion'];

$insertimagequestion->bind_param("isi",$lastID, $sessid, $qnum); 

$sessid =  $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : ''); 
    
    $insertimagequestion->execute(); 
    
                if ($insertimagequestion->errno) { 
          // Handle query error here 
        } 
 
        $insertimagequestion->close(); 
       
        var_dump($_POST);
 
    } 
    ?> 

Open in new window


the $_POST['numQuestion']; refers to a name variable of a hidden input on a previous page
carlbrooks1995Asked:
Who is Participating?
 
Slick812Commented:
greetings : carlbrooks1995, , I see that you have included the PHP error message of -
"Column 'QuestionId' cannot be null" in the execute( ) function

SO, if you look at your bind_param( ) -
$insertimagequestion->bind_param("isi",$lastID, $sessid, $qnum);

you have the $qnum as corresponding to the database QuestionId column, since the error says it's NULL, you have MISSED getting a "valid value" in the $qnum variable, you set this with -
$qnum = $_POST['numQuestion'];

so I would guess that the $_POST['numQuestion'];   is NOT SET or you do not change it as an INTEGER,
you should test  the $_POST  entries to see if they are set, sometimes users do not type anything into a FORM field (although this is a hidden field on yours).

if (!isset($_POST['numQuestion']))  $qnum = 0;
     else
    $qnum = (int) $_POST['numQuestion'];// convert to an integer

for alot of $_POST returns, the safest thing to do is to TEST them to see if their values are within a LIMIT

if (($qnum < 1)|($qnum > 123)) {
    echo 'BAD ERROR , OUT OF RANGE';// your error handling code here
    exit():
    }

When I do development and testing, I usually echo out all the important values, to check them as -

echo 'LastID= ',$lastID,' |sessid= ',$sessid,' |qnum= ',$qnum,'<br />';
$insertimagequestion->execute();
0
 
Slick812Commented:
here is some code I have used on my pages

$amt = (empty($_POST['amt'])) ? 0 : (int) $_POST['amt'];
if (($amt < 56) | ($amt > 719))  $amt = 56;
0
 
carlbrooks1995Author Commented:
@S;ick812, I am getting no errors but I am recieving no echo, what does this mean?
0
 
carlbrooks1995Author Commented:
It is ok Slick812, got it to work. It was the qnum which was the problem. Thank you very much :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.