note 128595 added to mysqli.real-escape-string

From: Date: Thu, 08 Jun 2023 04:41:49 +0000
Subject: note 128595 added to mysqli.real-escape-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-220552@lists.php.net to get a copy of this message
example: <?php // Step 1: Establish a connection to the MySQL database $servername = "localhost"; $username = "xxxx"; $password = "xxxx"; $dbname = "xxxx"; $conn = mysqli_connect($servername, $username, $password, $dbname); // Check if the connection was successful if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Step 2: Retrieve user input from the form $customerID = $_POST['customerNum']; $shippedStatus = $_POST['shipped']; // Step 3: Sanitize user input $customerID = mysqli_real_escape_string($conn, $customerID); $shippedStatus = mysqli_real_escape_string($conn, $shippedStatus); // Step 4: Construct the SQL query $sql = "SELECT p.orderID, p.orderDate, p.shippingDate, c.customerID, c.firstName, c.lastName, c.postcode FROM purchase p INNER JOIN customer c ON p.customerID = c.customerID WHERE p.customerID = '$customerID' AND p.shipped = '$shippedStatus' ORDER BY p.orderDate ASC"; // Step 5: Execute the query and fetch results $result = mysqli_query($conn, $sql); // Step 6: Check if any orders were found if (mysqli_num_rows($result) > 0) { // Display orders in a table echo "<table>"; echo "<tr><th>Order ID</th><th>Order Date</th><th>Shipping Date</th><th>Customer ID</th><th>First Name</th><th>Last Name</th><th>Postcode</th></tr>"; while ($row = mysqli_fetch_assoc($result)) { echo "<tr>"; echo "<td>" . $row['orderID'] . "</td>"; echo "<td>" . $row['orderDate'] . "</td>"; echo "<td>" . $row['shippingDate'] . "</td>"; echo "<td>" . $row['customerID'] . "</td>"; echo "<td>" . $row['firstName'] . "</td>"; echo "<td>" . $row['lastName'] . "</td>"; echo "<td>" . $row['postcode'] . "</td>"; echo "</tr>"; } echo "</table>"; } else { // No matching orders found echo "No orders were found that match your criteria."; } // Step 7: Close the database connection mysqli_close($conn); ?> function validateForm(theForm) { var valid = true; //assume the form is valid to start with // Go through each form input and check it has a value if (!theForm.fname.value.length) { valid = false; //form is not valid document.getElementById('fname-error').style.display = "inline-block"; //show the error message theForm.fname.style.border = "1px solid #ff0000"; //with a red border around the text box } else { document.getElementById('fname-error').style.display = "none"; //hide the error message theForm.fname.style.border = "1px solid #ccc"; //set the border back to normal } if (!theForm.lname.value.length) { valid = false; document.getElementById('lname-error').style.display = "inline-block"; theForm.lname.style.border = "1px solid #ff0000"; } else { document.getElementById('lname-error').style.display = "none"; theForm.lname.style.border = "1px solid #ccc"; } if (!theForm.pet.value.length) { valid = false; document.getElementById('pet-label').style.color = '#ff0000'; } else { document.getElementById('pet-label').style.color = "#000000"; } if(isNoCheckboxSelected(theForm.sports)) { valid = false; document.getElementById('sports-label').style.color = '#ff0000'; } else { document.getElementById('sports-label').style.color = '#000000'; } return valid; //return the state of the form } function isNoCheckboxSelected(checkboxGroup) { for (var i = 0; i < checkboxGroup.length; i++) { if (checkboxGroup[i].checked) { return false; } } return true; } <body> <form action="#" method="get" onsubmit="return validateForm(this);"> <div class="form-element"> <label for="fname">First Name</label> <input type="text" name="fname" id="fname"> <span class="error-messages" id="fname-error">first name req </span> </div> <div class="form-element"> <label for="name2">Name2</label>... ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.130 (proxied: 137.154.29.175) ---- Manual Page -- https://php.net/manual/en/mysqli.real-escape-string.php Edit -- https://main.php.net/note/edit/128595 Del: integrated -- https://main.php.net/note/delete/128595/integrated Del: useless -- https://main.php.net/note/delete/128595/useless Del: bad code -- https://main.php.net/note/delete/128595/bad+code Del: spam -- https://main.php.net/note/delete/128595/spam Del: non-english -- https://main.php.net/note/delete/128595/non-english Del: in docs -- https://main.php.net/note/delete/128595/in+docs Del: other reasons-- https://main.php.net/note/delete/128595 Reject -- https://main.php.net/note/reject/128595 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220552) next »