Setzi138 Posted October 14, 2021 Share Posted October 14, 2021 (edited) 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 October 14, 2021 by Setzi138 Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/ Share on other sites More sharing options...
Barand Posted October 14, 2021 Share Posted October 14, 2021 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 } Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591033 Share on other sites More sharing options...
gw1500se Posted October 14, 2021 Share Posted October 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591034 Share on other sites More sharing options...
ginerjm Posted October 14, 2021 Share Posted October 14, 2021 Can one really put the method and action clauses into a table tag? Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591035 Share on other sites More sharing options...
Setzi138 Posted October 14, 2021 Author Share Posted October 14, 2021 you mean like this ? if ($_SERVER['REQUEST_METHOD' == 'POST') { $username = $_POST['username']; $item = $_POST['item']; $quantity = $_POST['quantity']; } Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591036 Share on other sites More sharing options...
Barand Posted October 14, 2021 Share Posted October 14, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591037 Share on other sites More sharing options...
Setzi138 Posted October 14, 2021 Author Share Posted October 14, 2021 @gw1500se i have a button to submit it i aded it at the end When i click the button the mentioned warnings arise Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591038 Share on other sites More sharing options...
ginerjm Posted October 14, 2021 Share Posted October 14, 2021 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? Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591039 Share on other sites More sharing options...
Setzi138 Posted October 14, 2021 Author Share Posted October 14, 2021 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 ? 😅 Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591040 Share on other sites More sharing options...
ginerjm Posted October 14, 2021 Share Posted October 14, 2021 (edited) 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 October 14, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591041 Share on other sites More sharing options...
Barand Posted October 14, 2021 Share Posted October 14, 2021 If your form's action is "this script", just leave out the action attribute altogether. <form method='POST' > . . . </form> Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591042 Share on other sites More sharing options...
gw1500se Posted October 14, 2021 Share Posted October 14, 2021 Remember Barand's first post. You need to check that the form was submitted before using the variables. P.S. That first '=' is a typo. Barand meant to close the brackets using ']'. Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591044 Share on other sites More sharing options...
ginerjm Posted October 14, 2021 Share Posted October 14, 2021 To be clear the Barand line should look like if ($_SERVER['REQUEST_METHOD'] == 'POST') { (process post items here } Note that the ] was missing as well as a closing quote Quote Link to comment https://forums.phpfreaks.com/topic/313992-connection-between-html-table-and-mysql-database/#findComment-1591045 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.