11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 30 • USING <strong>PHP</strong> WITH MYSQLwww.it-ebooks.infoThe process of binding parameters is best explained with an example. Returning to theaforementioned scenario involving a web form that accepts 20 URLs, the code used to insert thisinformation into the <strong>MySQL</strong> database might look like the code found in Listing 30-1.Listing 30-1. Binding Parameters with the mysqli Extension// Create the query and corresponding placeholders$query = "INSERT INTO products SET id=NULL, sku=?,name=?, price=?";// Create a statement object$stmt = $mysqli->stmt_init();// Prepare the statement for execution$stmt->prepare($query);// Bind the parameters$stmt->bind_param('ssd', $sku, $name, $price);// Assign the posted sku array$skuarray = $_POST['sku'];// Assign the posted name array$namearray = $_POST['name'];// Assign the posted price array$pricearray = $_POST['price'];// Initialize the counter$x = 0;// Cycle through the array, and iteratively execute the querywhile ($x < sizeof($skuarray)) {$sku = $skuarray[$x];$name = $namearray[$x];$price = $pricearray[$x];$stmt->execute();}// Recuperate the statement resources$stmt->close();// Close the connection$mysqli->close();602

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!