undefined variables

Hi, for the “insert into” part of the code, where i list the values, I’m getting a bunch of “undefined variable” notices. The thing that is weird to me though, is the fact that the first value “student ID” doesn’t bring up this notice, and yesterday (for some reason it doesn’t do this today, it just comes up with the ‘ERROR’) it would say that the studentID row had been updated.
[php]<?php

$studentID = isset($_POST[‘studentID’])?strip_tags($_POST[‘studentID’]):null;
$firstName = isset($_POST[‘fullName’])?strip_tags($_POST[‘fullName’]):null;
if(isset($_POST[‘position’]) )
{
$varposition = $_POST[‘position’];
$errorMessage = “”;
}
if(isset($_POST[‘experience’]));
if(empty($aexperience))
{
echo(“You did not check experience”);
}
else
{
$N = count($aexperience);
}
if(isset($_POST[‘willDemonstrate’]));
if(empty($awillDemonstrate))
{
echo(“You did not choose any modules to demonstrate”);
}
else
{
$N = count($awillDemonstrate);
}
if(isset($_POST[‘willNotDemonstrate’]));
if(empty($awillNotDemonstrate))
{
echo(“You did not choose any modules to not demonstrate”);
}
else
{
$N = count($awillNotDemonstrate);
}
$firstName = isset($_POST[‘maxHoursAvailable’])?strip_tags($_POST[‘maxHoursAvailable’]):null;
if(isset($_POST[‘trainingNeeded’]) )
{
$varposition = $_POST[‘trainingNeeded’];
$errorMessage = “”;
}

$db = new mysqli(‘localhost’, ‘root’, ‘’, ‘modules’);
if (mysqli_connect_errno()) {
echo ‘Error: Could not connect to database. Please try again later.’;
exit;
}
// Insert data into mysql
$query="INSERT INTO tutors (studentID, fullName, position, experience, willDemonstrate, willNotDemonstrate, maxHoursAvailable, trainingNeeded)
VALUES (’$studentID’, ‘$fullName’, ‘$position’, ‘$experience’, ‘$willDemonstrate’, ‘$willNotDemonstrate’, ‘$maxHoursAvailable’, ‘$trainingNeeded’) ";
if($result){
echo $db->affected_rows.“information has been updated.”;
} else {
echo “ERROR”;
}
//close mysql
$db->close();
?> [/php]
I know it’s kind of a mess right now and I could put all those issets into a loop probably but I would just like it to work before I make it neat!
If it’s relevant then ‘position’ and ‘trainingNeeded’ are ENUMS, and ‘experience’, ‘willDemonstrate’ and ‘willNotDemonstrate’ are sets.

Because all of those variables are undefined and $studentID is defined on the first line. It should still execute the query but with empty values for all of the undefined variables.

Hi Matt, I’m confused, I thought I had defined them all, in the same way as I have defined studentID? With the isset? How would I go about defining them in the proper way if my way didn’t work?

None of these variables are defined like $studentID

[php]
‘$fullName’, ‘$position’, ‘$experience’, ‘$willDemonstrate’, ‘$willNotDemonstrate’, ‘$maxHoursAvailable’, ‘$trainingNeeded’[/php]

I’m guessing this variable is supposed to be $fullName instead of $firstName

[php]$firstName = isset($_POST[‘fullName’])?strip_tags($_POST[‘fullName’]):null;[/php]

As for the others,

[php]
if (isset($_POST[‘position’])) {
$varposition = $_POST[‘position’];
$errorMessage = “”;
}
[/php]

Why are you calling it $varposition? Shouldn’t it be $position?

[php]
if(isset($_POST[‘willDemonstrate’]));
if(empty($awillDemonstrate)) {
echo(“You did not choose any modules to demonstrate”);
}
else {
$N = count($awillDemonstrate);
}
[/php]

I’m not sure what you are trying to do here. The semi-colon after if(isset()) is invalid syntax and $awillDemonstrate is always going to be empty() since it is never defined.

Thank you for your helpful response! I’ve got rid of the silly syntax and naming mistakes (I suppose when you spend so long looking at a piece of code you miss things like that, sometimes you need an outsider to have a look at it) and changed some other things around, which brings me to no PHP notices when I enter data from the website, it just says that I apparently haven’t entered anything and comes up with “ERROR” to say none of my rows have been affected :frowning:
Also, when I don’t fill out the form and go straight to the php it still says my variables are undefined! Even though when I was working with the notices on the php page leading from the form it got rid of them all.
Could you please have another look at my new code and tell me how to define them in the correct way, I realise it says I haven’t entered anything because they are apparently still undefined. I just don’t understand how it’s different from studentID!
[php]<?php

$studentID = isset($_POST[‘studentID’])?strip_tags($_POST[‘studentID’]):null;
$fullName = isset($_POST[‘fullName’])?strip_tags($_POST[‘fullName’]):null;
if(isset($_POST[‘position’]) )
{
$position = $_POST[‘position’];
$errorMessage = “”;
}
if(isset($_POST[‘experience’]))
{
$experience = $_POST[‘experience’];
if(empty($aexperience))
{
echo(“You did not check experience”);
}
else
{
$N = count($aexperience);
} }
if(isset($_POST[‘willDemonstrate’]))
{
$willDemonstrate = $_POST[‘willDemonstrate’];
if(empty($awillDemonstrate))
{
echo(“You did not choose any modules to demonstrate”);
}
else
{
$N = count($awillDemonstrate);
} }
if(isset($_POST[‘willNotDemonstrate’]))
{
$willNotDemonstrate = $_POST[‘willNotDemonstrate’];
if(empty($awillNotDemonstrate))
{
echo(“You did not choose any modules to not demonstrate”);
}
else
{
$N = count($awillNotDemonstrate);
} }
$maxHoursAvailable = isset($_POST[‘maxHoursAvailable’])?strip_tags($_POST[‘maxHoursAvailable’]):null;
if(isset($_POST[‘trainingNeeded’]) )
{
$trainingNeeded = $_POST[‘trainingNeeded’];
$errorMessage = “”;
}

$db = new mysqli(‘localhost’, ‘root’, ‘’, ‘allocations’);
if (mysqli_connect_errno()) {
echo ‘Error: Could not connect to database. Please try again later.’;
exit;
}
// Insert data into mysql
$sql=“INSERT INTO tutors (studentID, fullName, position, experience, willDemonstrate, willNotDemonstrate, maxHoursAvailable, trainingNeeded)
VALUES (’$studentID’, ‘$fullName’, ‘$position’, ‘$experience’, ‘$willDemonstrate’, ‘$willNotDemonstrate’, ‘$maxHoursAvailable’, ‘$trainingNeeded’, now())”;
$result = mysql_query($sql);
if($result){
echo $db->affected_rows.“information has been updated.”;
} else {
echo “ERROR”;
}
//close mysql
$db->close();
?> [/php]

Also, fullName and maxHours now doesn’t bring up a notice. It’s just the sets and enums.

You are mixing mysql with mysqli. Change this:

[php]
$result = mysql_query($sql);
if($result){
echo $db->affected_rows.“information has been updated.”;
} else {
echo “ERROR”;
}
[/php]

To this:

[php]
if ($result = $db->query($sql)) {
echo $db->affected_rows.“information has been updated.”;
}
else {
printf(“Error: %s\n”, $db->error);
}
[/php]

And you should now see that your insert query has an error. The number of columns does not match the number of values.

Ah thank you. With a little editing the ENUMs and text fields now successfully enter into the database, but still absolutely no idea how to get the checkboxes (Experience, willDemonstrate, willNotDemonstrate) to enter :frowning: I’ve searched google, there are many articles on it but none of them seem relevant to my code. Bare in mind I want them to be able to check as many boxes as they can (but willDemonstrate and willNotDemonstrate should not conflict…although that isn’t my main concern right now). Have you got any idea of a way to achieve this?

Check the variable name for willdemonstrate, in some areas, you have awilldemonstrate and in some you have willdemonstrate.

I’ve actually scraped those bits of code completely because it just wasn’t working for me. Right now I just have them defined like studentID

Okay, I’m getting there! My problem, and hopefully my last one, is that I want the user to be able to check multiple values for each of the checkbox fields. With the code I have, only the first value I click gets inputted into the database. Any idea how to make it so the user can select more than 1 value?
Currently I have this code for each of the checkbox values (Experience, willDemonstrate, willNotDemonstrate)
[php]if (isset($_POST[‘Experience’]) && is_array($_POST[‘Experience’])) {
$experienceCB = join (’, ', $_POST[‘Experience’]);
}
else $experienceCB = ‘’;[/php]

Can you show your form?

[php]


Student ID



Full Name



Position

Select... Staff Student

Experience

101 103 109 110 201
207 211 213 219 304
305 323 327 329 396
516 517 518 519 521
522 523 557

[/php]

That’s the start of it, willDemonstrate and willNotDemonstrate are exactly the same values as Experience above

So what is the problem exactly? With this code you are creating a string of values

[php]join(’, ', $_POST[‘Experience’]) [/php]

Are you storing this as a string in your table? What type are you using for this column?

It’s stored as a SET.
My problem is that if I select more than 1 value, it only shows one value, I want it to enter all values checked.

So you have a column that is SET(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)?

There is either a problem with the query or the table. You will just have to echo the query and try to debug it.

Are you sure you want to use a SET type here? It doesn’t seem to work well using numbers as strings.

For example:
CREATE TABLE myset (col SET(‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘10’));
INSERT INTO myset (col) VALUES (‘1,2,3,4,5,6’), (‘2,3,4’), (‘7,8,9’), (‘10’), (‘11’);

Resulting data:
“1,2,3,4,5,6”
“2,3,4”
“7,8,9”
“10”
“1,2,4”

Note that since ‘11’ is not defined in the SET column it is converted to ‘1,2,4’

You might be better off just storing the string as a VARCHAR

I’ve changed it to a varchar, now it does take in multiple values but puts them in a different row. Is there anyway to store the values in the same row, separated by commas?

Sorry I’m not clear on what the problem is now. It puts them in a different row?

Hi, just wanted to say I left this for a couple of weeks and came back to it and solved it. I’m sure not sure how but now when the users selects mutliple checkboxes it shows up in the database as “option1, option2, option3” seperated by commas as desired. Thank you for the help.

Sponsor our Newsletter | Privacy Policy | Terms of Service