Jump to content

Recommended Posts

I always get the following warnings and i have no idea why.

    Warning: Undefined array key "username" in C:\xampp\htdocs\Kulinarik\save_order.php on line 2

    Warning: Undefined array key "item" in C:\xampp\htdocs\Kulinarik\save_order.php on line 3

    Warning: Undefined array key "quantity" in C:\xampp\htdocs\Kulinarik\save_order.php on line 4

    Fatal error: Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\Kulinarik\save_order.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Kulinarik\save_order.php on line 13

Here comes the code that belongs to the errors

Html

                  <table class="u-table-entity" src="jquery.js" action="save_order.php" method="POST">

                    <script scr="calculation.js"></script>
                    
                    <colgroup>
                      <col width="20%">
                      <col width="2.1%">
                      <col width="22%">
                      <col width="21.7%">
                      <col width="34.2%">
                    </colgroup>
                    <tbody class="u-table-alt-grey-5 u-table-body">
                      <tr style="height: 55px;">
                        <b>
                        <th class="u-table-cell u-table-cell-1"><b>Produkt</b><span style="font-weight: 700;"></span>
                        </th>
                        <th class="u-table-cell"></th>
                        <th class="u-table-cell u-table-cell-3"><b>Einzelpreis</b></th>
                        <th class="u-table-cell u-table-cell-4"><b>Menge</b></th>
                        <th class="u-table-cell u-table-cell-5"><b>Gesamtpreis</b></th>
                        </b>
                      </tr>

                      <tr style="height: 55px;">
                        <td class="u-table-cell">
                          <input name="item" value="Kornspitz"></input>
                        </td>
                        <td class="u-table-cell"></td>
                        <td class="u-table-cell">
                          <input type="text" class="price" value="11.39" readonly/>
                        </td>
                        <td class="u-table-cell">
                          <input type="text" class="quantity"/> 
                        </td>
                        <td class="u-table-cell">
                          <input type="text" class="total" readonly/>
                        </td>
                      </tr>







        <a href="save_order.php" class="u-border-none u-btn u-button-style u-custom-color-1 u-btn-2" type="submit">Bestellen<br>

php

<?php
	$username = $_POST['username'];
	$item = $_POST['item'];
	$quantity = $_POST['quantity'];

	// Database connection
	$conn = new mysqli('localhost','root','123456','orders');
	if($conn->connect_error){
		echo "$conn->connect_error";
		die("Connection Failed : ". $conn->connect_error);
	} else {
		$stmt = $conn->prepare("insert into orders(username, item, quantity) values(?, ?, ?)");
		$stmt->bind_param("sssssi", $username, $item, $quantity);
		$execval = $stmt->execute();
		echo $execval;
		echo "Bestellung Erfolgreich";
		$stmt->close();
		$conn->close();
	}
?>

 

Edited by Setzi138

Your only input that has a name is "item", so the others won't appear in the POST array.

You also need to check that data has been posted before attempting to access them

if ($_SERVER['REQUEST_METHOD'= == 'POST) {
    // process posted data
}

 

You are probably trying to use those variables on the first output of the page. They will not be filled in until the user does it and the page is submitted. Your HTML shows not form or submit action. See this article on how to create an interactive page.

1 minute ago, Setzi138 said:

you mean like this ?

That's a start. But it will leave you trying to insert nothing into your table.

3 minutes ago, ginerjm said:

Can one really put the method and action clauses into a table tag?

Good spot. I didn't even notice he doesn't have a form to submit.

5 minutes ago, ginerjm said:

Because you didn't read Barand's post completely.  As well as my comment about where you placed the method and action clauses.

Is this your first script using an html form?

Yes 
Is it that obvious ? 😅

Quite.  Perhaps you need to go back to reading up on how to write html?

For the heck of it here's a sample of what a form could look like.  Note the things that are different from what you produced.

//  producing an html form from php
//  this form will produce 3 POST entries: fld1, fld2 and btn
$code=<<<heredocs
	<form method='POST' action='(this script's name)' autocomplete=off>
	<label>Enter this data item:
	<input type='text' name='fld1' class='sz_10' value='$fld1'>
	</label>
	<br>
	<label>Enter this data also:
	<input type='text' name='fld2' class='sz_6' value='$fld2'>
	</label>
	<br>
	<input type='submit' name='btn' value='Submit'>
	</form>
heredocs;
	echo $code;

This utilizes PHP's heredocs verb to produce easy-to-write and easy-to-read html while in php mode.  It also references a couple of css classes to help set the size of the input fields.  Read up on css and avoid the antiquated use of styles on your input tags.  Note the use of label tags.  And note the use of var names in the value clauses of the inputs.  These can be used to send back the user's input when your script needs to report an error and you don't want the user to have to re-type their inputs when you send the form back to them.

A lot to learn which will benefit you immensely if you simply try and follow it.  Along with a reference manual.

Edited by ginerjm
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.