How to fix php/mysqli error below

Posted on 2012-09-19
Last Modified: 2012-09-19
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:

    /* check connection */  
    if (mysqli_connect_errno()) {  
        printf("Connect failed: %s\n", mysqli_connect_error());  
    $result = 0;  
    move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);  
    $result = 1;  
    $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'];  
    if ($insert->errno) {  
      // Handle query error here  
    $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'] : ''); 
                if ($insertimagequestion->errno) { 
          // Handle query error here 

Open in new window

the $_POST['numQuestion']; refers to a name variable of a hidden input on a previous page
Question by:carlbrooks1995
    LVL 33

    Accepted Solution

    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;
        $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

    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 />';
    LVL 33

    Expert Comment

    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;

    Author Comment

    @S;ick812, I am getting no errors but I am recieving no echo, what does this mean?

    Author Closing Comment

    It is ok Slick812, got it to work. It was the qnum which was the problem. Thank you very much :)

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now