I am having trouble posting a mysql table to my website.
Basically, the php error says that the row variable that I used was undefined. I tried using a code from a youtube tutorial but to no avail.
I also checked my sql query on phpmyadmin and it seems to work just fine.
Here is my test code for your reference:
<!DOCTYPE html> <html> <head> <title>Table with database</title> <style> table { border-collapse: collapse; width: 100%; color: #588c7e; font-family: monospace; font-size: 25px; text-align: left; } th { background-color: #588c7e; color: white; } tr:nth-child(even) {background-color: #f2f2f2} </style> </head> <body> <table> <tr> <th>Name</th> <th>Transaction</th> <th>Website</th> </tr> <?php $conn = new mysqli('localhost','sasuke', 'sharinganrox', 'uchiha_db'); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT clientData.Client_ID, clientData.Client_Name, transactionData.Transaction_ID, transactionData.Transaction_Date, websiteProduct.Website_ID, websiteProduct.Website_Name, FROM clientData CROSS JOIN transactionData on clientData.Client_ID = transactionData.Client_ID CROSS JOIN websiteProduct on transactionData.Website_ID = websiteProduct.Website_ID WHERE monthname(transaction_date)='January' ORDER BY transaction_date ASC;"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_array($result)) { echo "<tr><td>" . $row["client_name"]. "</td><td>".$row["transaction_ID"]."</td><td>".$row["transaction_date"]."</td><td>".$row["website_Name"]."</td><td>".$row["website_Price"]."</td></tr>"; } echo "</table>"; } else { echo "0 results"; echo "number of rows: " . $result->num_rows; } $conn->close(); ?> </table> </body>
The error says: Notice: Undefined index: client_name in C:wamp64wwwaugusta_webapptestingtable.php on line 41
Notice: Undefined index: transaction_ID in C:wamp64wwwaugusta_webapptestingtable.php on line 41
Notice: Undefined index: transaction_date in C:wamp64wwwaugusta_webapptestingtable.php on line 41
Notice: Undefined index: website_Name in C:wamp64wwwaugusta_webapptestingtable.php on line 41
Notice: Undefined index: website_Price in C:wamp64wwwaugusta_webapptestingtable.php on line 41
Advertisement
Answer
In PHP arrays, string keys are case-sensitive.
The DB column is Client_Name
(capital C and N) but you’re asking for the array key client_name
(lowercase).
Check the column names, and the corresponding keys that you’re asking for, and you’ll see the mismatch. If you var_dump($row)
you should see the problem clearly.