0

How do I get this to work? Trying to use the explode function and insert into my DB table, but the values are not being sent to my DB table.

HTML:

<select id="customer_name" name="customer_name" required>
    <option value="">Select customer...</option>
    <?php
    $sqli = "SELECT * FROM customer order by customer_name";
    $result = mysqli_query($conn, $sqli);
    while ($row = mysqli_fetch_array($result)) {
    echo '<option>'.$row['customer_name'].'|'.$row['customer_ID'].'</option>';
    }
    ?>
    </select>

PHP:

<?php
include_once 'database.php';

$stmt1 = $conn->prepare("INSERT INTO persons(person_ID, customer_name, customer_ID) VALUES (?,?,?)");

$result = $_POST['customer_name'];
$result_explode = explode('|', $result);

$person_ID = $_REQUEST['person_ID'];
$customer_name= $_REQUEST['$result_explode[0]'];
$customer_ID= $_REQUEST['$result_explode[1]'];

$stmt1->bind_param("sss", $person_ID, $customer_name, $customer_ID);

$stmt1->execute();

echo "<strong>Record saved....</strong>"; echo 'returning to add another person. If no more persons to add please close this window.';

mysqli_close($conn);
?>

The above returns the errors: Undefined index: $result_explode[0] Undefined index: $result_explode[1] However I'm not sure how to define these (I'm self learning on a step curve)

1 Answer 1

2

Refer here for solution: https://www.stechies.com/undefined-index-error-php/

While working in PHP, you will come across two methods called $_POST and $_GET. These methods are used for obtaining values from the user through a form. When using them, you might encounter an error called “Notice: Undefined Index”.

This error means that within your code, there is a variable or constant that has no value assigned to it. But you may be trying to use the values obtained through the user form in your PHP code.

The error can be avoided by using the isset() function. This function will check whether the index variables are assigned a value or not, before using them.

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry I'm not too concerned about removing the notice, the issue is the fields are not going into my DB table.
@Ashman what? how can you not be concerned about the error notice? that's like level one READ what the error notice says, then you will understand what your issue is, this notice is not a warning its an error its telling you why it's not working and you should know that even before your started coding, this answer is correct as it solves your problem just read what the link says and correlate that with your error.
Did you even read to the bottom of the article? Removing the notice is only a small part of that article. Read to the bottom to fully understand what is going on, why you are getting the error, why it's not doing what it is supposed to do and how to fix it. $_POST['customer_name'] is not being set ergo no data, nothing to explode, nothing to insert to the DB.
Yes read it. I'll try again as I'm not understanding. Very new to this.
@Ashman thats good that you solved your issue and i understand your are new so please next time i recommend you look at the error notice copy it then paste it into google you will most probably find an answer to your error Number one rule is read your errors fully so that you can grasp the extent of the error ~ have a great day :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.