?
Solved

How to fix php/mysqli error below

Posted on 2012-09-19
4
Medium Priority
?
933 Views
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:

<?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
0
Comment
Question by:carlbrooks1995
  • 2
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Slick812 earned 2000 total points
ID: 38415248
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
 
LVL 35

Expert Comment

by:Slick812
ID: 38415339
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
 

Author Comment

by:carlbrooks1995
ID: 38415372
@S;ick812, I am getting no errors but I am recieving no echo, what does this mean?
0
 

Author Closing Comment

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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

621 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