Jump to content

Connection between HTML table and MySql database


Setzi138
 Share

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ? 😅

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

 Share

×
×
  • 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.