Jump to content

Can't Detect isset() for text box (only 1 box)


jahicki

Recommended Posts

Hi guys, I have this code:

<?php

// This Code Works

if (isset($_POST{"PantryID"})) {

Create_Pantry($_POST{"PantryID"});

}

// This Code Isn't Working, Keeps Going To 'Else'.

if (isset($_POST{"ItemName"})){

Add_Item($_POST{"ItemName"}, $_POST{"ItemWeight"}, $_POST{"ItemPrice"}, $_POST{"ItemDate"});

}else{

echo "Nothing To do";

}

?>

 

here is the html

<label for="PantryID">Create Pantry: </label>

<input type="text" id="PantryID" name="PantryID" disabled="true" />

<label for="ItemName">Item Name: </label>

<input type="text" id="ItemName" name="ItemName" disabled="true" />

Please help, I can't find any syntax errors.  - At the point the PHP is activated it is no longer disabled. the PantryID  is set up the same way.

Edited by jahicki
Correction
Link to comment
Share on other sites

16 hours ago, jahicki said:

You:    ... if (isset($_POST{"ItemName"})) ... 

Gizmola:  ...  var_dump($_POST)  ... 

You:   ... I got this from the Var_Dump: array(1) { ["Submit"]=> string(6) "Submit" } Array

Conclusion?  The ItemName Form field is not being passed across. 

Check the HTML that should be sending it. 

Regards, 
   Phill  W.

Link to comment
Share on other sites

You can start by using square brackets in your POST variables instead of curly braces like the rest of us. The curly braces can fail in at least one case.

Example:

$myArray = [1,2];
$index = 1;

echo "value at index $index is $myArray[$index]"; // outputs "value at index 1 is 2"

echo "value at index $index is $myArray{$index}"; // will throw "Notice: Array to string conversion"

var_dump($myArray{$index});   // outputs "int(2)"

https://wiki.php.net/rfc/deprecate_curly_braces_array_access

Edited by benanamen
  • Like 1
Link to comment
Share on other sites

So you have 2 items you can work on.  The first was brought up by benanamen, which is really a syntax issue.  You are not referencing arrays correctly:

 

// Use this

if (isset($_POST['itemName']) {

// Not this
if (isset($_POST{'itemName'}) {

 

That is not the source of your current problem however.  Your current problem is that whatever javascript you have that you assume is making your form valid, is not valid, so your text field is not being passed when the form is posted.  Another good debugging tool is to use something like chrome dev tools, and look at the network tab when you submit the form.  You can then see the contents of the post variables as they are sent when you submit the form.

In conclusion, right now your problem is your html/javascript code and has nothing to do with PHP.  The code is working as you intended, as $_POST['itemName'] is not set/does not exist.

  • Like 1
Link to comment
Share on other sites

Hi guys, thanks for the responses.

I removed the JS completely for now, as it isn't required.

I changed the Curley Brackets for Square. (Can't remember where I saw the Curley Brackets.)

Now I get this:

array(4) { ["Item"]=> string(5) "Bacon" ["Weight"]=> string(3) "500" ["Price"]=> string(4) "3.25" ["Date"]=> string(10) "12/12/2020" } ArrayCollecting Data -- Establishing Connection To Database -- Building Query -- Bacon5003.2512/12/2020 -- Inserting Data -- Error creating table: Unknown column 'Bacon' in 'field list

$sql = "INSERT INTO Pantry (ItemName, ItemWeight, ItemPrice, ItemDate)
VALUES (" . $Name . ", " . $Weight . ", " . $Price . ", " . $Date . ")";

 

Edited by jahicki
Link to comment
Share on other sites

9 minutes ago, Barand said:

I hope you are not storing dates in your db table in that "12/12/2020" format.

What is the structure of the table you are trying to update?

I won't be once I get it updating. its a basic SQL database table.

the table was created with this code:

$sql = "CREATE TABLE " . $Name . "(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
ItemName VARCHAR(30) NOT NULL,
ItemWeight VARCHAR(30) NOT NULL,
ItemPrice VARCHAR(5),
ItemDate DATE
)";

 

Edited by jahicki
Link to comment
Share on other sites

2 hours ago, jahicki said:

 


$sql = "INSERT INTO Pantry (ItemName, ItemWeight, ItemPrice, ItemDate)
VALUES (" . $Name . ", " . $Weight . ", " . $Price . ", " . $Date . ")";

Never, never trust data values submitted by the User! 

You are wide open to SQL Injection Attacks. 
Obligatory XKCD Reference: Little Bobby Tables

Read up on Prepared Statements or, at the very least, encode the values that you are using to build your "SQL" - a String variable that just happens to contains something that your DBMS can understand. 

And Barand is absolutely right - be extra careful with Date values.  When is 03/06/09?  Spring, Summer or Autumn? 

Regards, 
   Phill  W.

 

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.