Jump to content

PHP Null Variables & MYSQL


Y2Neildotcom

Recommended Posts

Hi all,

 

I have a web page that allows the user to insert rows into a MySQL database. THe page is for my football team and it inserts the name of the player and also a time for if they were substituted during the game.

 

The MySQL database has two fields for these times, both are set to BIGINT(20) and both have a default of NULL. These are called 'on' and 'off'.

 

My PHP form contains a dropdown box for the time's on and off that lists the numbers 0-90 with values of 0-90 as well as a blank (-) with a value of blank ("").

 

<label><select name="off[1]" id="off1">
     <option value="" selected="selected">-</option>
      <option value="1">01</option>
      <option value="2">02</option>
      <option value="3">03</option>
      <option value="4">04</option>
      <option value="5">05</option>

 

Because you could have up to 16 rows inserting at once (but no fewer than 11), the insert code from the form to the database is as such:

 

$players = (isset($_POST['players'])) ? $_POST['players'] : null;
$players = (is_array($players)) ? $players : array();

$r = 0; // Count records inserted

// For each player submitted on form...
foreach($players as $p) {
// Get player input data
$player_id = (isset($_POST['player_id'][$p])) ? $_POST['player_id'][$p] : ''; // Player ID
        $on = (isset($_POST['on'][$p])) ? (int) $_POST['on'][$p] : NULL; // Player On time
$off = (isset($_POST['off'][$p])) ? (int) $_POST['off'][$p] : NULL; // Player Off time
$player_pos_type = (isset($_POST['player_pos_type'][$p])) ? $_POST['player_pos_type'][$p] : ''; // Player Pos Type
$sub_type_id = (isset($_POST['sub_type_id'][$p])) ? $_POST['sub_type_id'][$p] : ''; // Player Sub Type ID
$sql="INSERT INTO `apps` (`player_id`, `match_id`, `team_id`, `on`, `off`, `player_pos_id`, `sub_type_id`) 
VALUES
('$player_id','$_POST[match_id]','$_POST[team_id]',$on,$off,'$player_pos_type','$sub_type_id')";
if (!empty($player_pos_type)) {
	if (!mysql_query($sql,$con)) {
		die('Error: ' . mysql_error());
	} else {
		$r++;
	}
}
}

 

When I submit a form the data that enters my database when I select a blank on or off time always goes in as a value of '0' when it is expected to be 'NULL'.

 

I've tried several variations on the tenary operator, changing the condition is false from 'NULL' to null, to "NULL" etc but everytime it enters as '0'.

 

I even went to debug by setting $off = 'NULL' and ignoring whatever was entered via the form but this still entered every row as '0'.

 

This leads me to beleive I have an issue with the data being submitted. Do I need to change the PHP form itself?

 

I've been told that when a PHP variable with a value of null is inserted into a string, it just becomes an empty string, but I'm not sure on how to fix this.

 

Can anyone offer a solution to this?

 

Many thanks.

Link to comment
Share on other sites

Thanks,

 

My table is set thusly:

 

Field Type                 Null Default

    on bigint(20) Yes NULL

            off bigint(20) Yes NULL

 

Echoing the results for on and off came up:

 

on: ''

off: ''

 

So it is going through as blank, so are you suggesting that because the column is an integer it'll always insert blank as '0' even though the defaul is null?

Link to comment
Share on other sites

another thing you can try is putting quotes around the NULL value

 

$on = (isset($_POST['on'][$p])) ? (int) $_POST['on'][$p] : "NULL"; // Player On time

If the isset() returns FALSE, that would make the $on a string with the value of "NULL", not a NULL value. There's a big difference between NULL and "NULL"

 

The problem is that you're using isset() to check against a field that has <option value=""> selected. The isset() will return TRUE and set the value to an empty string, which is what the value actually is.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.