Isset() from POST[]

I submit a form and am trying to look at the POST[]

(
    [Name] => 
    [Manufacturer] => 
    [Model] => 
    [Width] => 
}

Im trying an INSERT query but

ERROR: Could not able to execute INSERT INTO unknown_types (name,manufacturer,model,external_width,external_height,external_depth,weight,total_plugs,required_plugs,requires_diverse_power,standard_copper_ports,standard_fiber_optic_ports,unidentified_ports,airflow_type,mounting_direction,Front_Cols,Front_Rows,Back_Cols,Back_Rows,power_consumptionu,network_speed ) VALUES ( ‘’,’’,’’, ,1, ,’’,‘Front’,). SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘,1, ,’’,‘Front’,)’ at line 3

I gather I Need NULL in the place of blank values for the query to work, but

 if(empty($_POST['Width'])) { $Width = NULL; } else { $Width = addslashes($_POST['Width']); }

seems to do nothing to the query and I get the same error.

Try searching for “PHP.net Null coalescing operator“

Php form post variables will not always be available and using the “ Null coalescing operator” sets defaults.

If the defaults are NULL or FALSE then the following test is easier to implement:


$Name = $_POST['Name'] ?? NULL;
$Manufacturer= $_POST['Manufacturer'] ?? NULL;
$Model =  $_POST['Model'] ?? NULL;
$Width = $_POST['Width'] ?? NULL;

$msg = ‘Something is not quite right’;
if ($Name && $Manufacturer && $Model && $Width) 
{
  $msg = ‘Everything is Hunky Dory’;
}
echo $msg

then why this?

$Width = $_POST['Width'] ?? NULL; 		

echo "Width: ".$Width;

results in

Width

why not

Width: NULL:

You cannot echo a null value. Use var_dump() instead.

These form values are not nulls. They are empty strings and using the null coalescing operator will return the empty string in the post variable, since it is not a null.

If you were using a prepared query, which is something you have already done, using implicit binding (supplying an array to the execute call), your current attempt would actually work, because supplying a php null value at query execution time will result in an sql null being used. However, since you are putting the values directly into the sql query statement, you must literally put the characters NULL into the sql statement.

I would personally dynamically build the sql query statement with only those columns in it that you have values for.

1 Like

I cannot actually check because currently using a tablet.

I believe NULL will not be rendered.

Instead of echo try using var_dump(…);

Why are are trying to use echo instead of testing all variables using the if statement?

Try also using echo immediately after the $msg = ‘Everything is Hunky Dory’;

the problem is im getting

ERROR: Could not able to execute 
INSERT INTO unknown_types (name,manufacturer,model,external_width,external_height,external_depth,weight,total_plugs,required_plugs,requires_diverse_power,standard_copper_ports,standard_fiber_optic_ports,unidentified_ports,airflow_type,mounting_direction,Front_Cols,Front_Rows,Back_Cols,Back_Rows,power_consumption,network_speed )
 VALUES 
( '','','', ,,,,,,1,, ,,'','Front',,,,,,).
 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ',,,,,1,, ,,'','Front',,,,,,)' at line 3

am I supposed to replace blank values with NULL?

Depends on the table column variable types and if they will accept a NULL value.

Too little information.

ok, I have the table variables default to NULL

CREATE TABLE unknown_types (
   unknown_type_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
   nlyte_material_id INT,
   name VARCHAR(100) DEFAULT NULL,
   model VARCHAR(50) DEFAULT NULL,
   manufacturer VARCHAR(50) DEFAULT NULL,
   external_width DECIMAL(6,3) DEFAULT NULL,
   external_depth DECIMAL(6,3) DEFAULT NULL,
   external_height DECIMAL(6,3) DEFAULT NULL,
   weight DECIMAL(6,3) DEFAULT NULL,
   total_plugs TINYINT(1) DEFAULT NULL,
   required_plugs TINYINT(1) DEFAULT NULL,
   requires_diverse_power BOOLEAN DEFAULT NULL,
   standard_copper_ports TINYINT UNSIGNED DEFAULT NULL,
   standard_fiber_optic_ports TINYINT UNSIGNED DEFAULT NULL,
   unidentified_ports TINYINT UNSIGNED DEFAULT NULL,
   airflow_type ENUM('Ambient','Front To Back') DEFAULT NULL,
   input_power_type ENUM('One Phase','Two Phase','Three Phase','Direct Current') DEFAULT NULL,
   mounting_direction ENUM('Front','Back') DEFAULT NULL,
   Front_Cols TINYINT UNSIGNED DEFAULT NULL,
   Front_Rows TINYINT UNSIGNED DEFAULT NULL,
   Back_Cols TINYINT UNSIGNED DEFAULT NULL,
   Back_Rows TINYINT UNSIGNED DEFAULT NULL,
   power_consumption DECIMAL(4,2) DEFAULT NULL,
   network_speed TINYINT UNSIGNED DEFAULT NULL,
   PRIMARY KEY ( unknown_type_id )
);

I gather the best way to INSERT a record like

INSERT INTO unknown_types (name,manufacturer,model,external_width,external_height,external_depth,weight,total_plugs,required_plugs,requires_diverse_power,standard_copper_ports,standard_fiber_optic_ports,unidentified_ports,airflow_type,mounting_direction,Front_Cols,Front_Rows,Back_Cols,Back_Rows,power_consumption,network_speed )
 VALUES 
( '','','', ,,,,,,1,, ,,'','Front',,,,,,).

would be

INSERT INTO unknown_types (requires_diverse_power,mounting_direction) VALUES (1,'Front')

But how can I do that with php?

I prefer to use PhpMyAdmin and paste a $sql statement into the query because there is more error information returned:

$sql = '
INSERT INTO unknown_types
(
name,
manufacturer,
model,
external_width,
external_height,
external_depth,
weight,
total_plugs,
required_plugs,
requires_diverse_power,
standard_copper_ports,
standard_fiber_optic_ports,
unidentified_ports,
airflow_type,
mounting_direction,
Front_Cols,
Front_Rows,
Back_Cols,
Back_Rows,
power_consumption,
network_speed
)
 VALUES 
(
 "",,""', ""', ,,,,,,1,, ,,'',"Front",,,,,,
)
';


echo $sql;


Paste the $sql results into PhpMyAdmin SQL and select the Format option followed by Go.

See above - build the query to only contain the columns that you have values for, or use prepared statements.

1 Like