Categories: php

Optimizing query in PHP

1 answer

I have probably a simple problem, but I'm a newbie in PHP

I have a select button:

<select name="ads_plan[]">     <option value="">Choose plan</option>     <option value="Free">Free</option>     <option value="Basic">Basic</option>     <option value="Premium">Premium</option>     <option value="Delux">Delux</option> </select> 

And I grab it with this PHP code:

<?php $ads_plan = $_POST['ads_plan'];  foreach ($ads_plan as $item) {     if ($item == 'Free') {         echo 'Free';     }      elseif ($item == 'Basic') {         echo 'Basic';     }      // etc ?> 

Now, I want to add selected value to the query. And I did that in this way:

UPDATED Also, note that I have a method choosePlan() where every plan collects his date and add a specific number of the day in the ads_end_date column. So for example: if the user chooses the Premium plan, the method will add 7 days on the current date, and on that day will delete that row from the database. $function->choosePlan(7, 'Premium')

<?php foreach ($ads_plan as $item) {     $users_id = $_SESSION['id'];      if ($item == 'Free')          $add_free_ads = $db->query("INSERT INTO users_ads (user_id, ads_name, ads_description, ads_category, ads_image, ads_price, ads_location, ads_phone, ads_plan, ads_end_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", $users_id, $ads_name, $ads_description, $ads_category, $ads_image, $ads_price, $ads_location, $ads_phone, $ads_plan, $function->choosePlan(3, 'Free'));      elseif ($item == 'Basic')          $add_basic_ads = $db->query("INSERT INTO users_ads (user_id, ads_name, ads_description, ads_category, ads_image, ads_price, ads_location, ads_phone, ads_plan, ads_end_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", $users_id, $ads_name, $ads_description, $ads_category, $ads_image, $ads_price, $ads_location, $ads_phone, $ads_plan, $function->choosePlan(5, 'Basic'));      elseif ($item == 'Premium')         $add_premium_ads = $db->query("INSERT INTO users_ads (user_id, ads_name, ads_description, ads_category, ads_image, ads_price, ads_location, ads_phone, ads_plan, ads_end_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", $users_id, $ads_name, $ads_description, $ads_category, $ads_image, $ads_price, $ads_location, $ads_phone, $ads_plan, $function->choosePlan(7, 'Premium'));      else         $add_delux_ads = $db->query("INSERT INTO users_ads (user_id, ads_name, ads_description, ads_category, ads_image, ads_price, ads_location, ads_phone, ads_plan, ads_end_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", $users_id, $ads_name, $ads_description, $ads_category, $ads_image, $ads_price, $ads_location, $ads_phone, $ads_plan, $function->choosePlan(10, 'Delux')); } ?> 

This above works, but I know that this is not the right approach. So my question is how can I optimize all this code. Is there any way how can I do this with only one query?

Thanks

All answers to this question, which has the identifier 61239104

The best answer:

DON'T TRUST USER INPUT validate the $ads_plan as shown below with the $valid array of valid strings.

Assuming you're using PDO you could reuse the prepared statement, now your SQL only has to be changed in one place and doesn't violate the DRY principle.

Here is a quick rewrite

$sth = $db->prepare('INSERT INTO users_ads (user_id, ads_name, ads_description, ads_category, ads_image, ads_price, ads_location, ads_phone, ads_plan, ads_end_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?');  $valid = ['Free', 'Basic', 'Premium', 'Delux']; $add_types = [];  foreach ($ads_plan as $item) {     if (in_array($item, $valid))     {         $sth->execute($users_id, $ads_name, $ads_description, $ads_category, $ads_image, $ads_price, $ads_location, $ads_phone, $ads_plan, $item);         $add_types[$item] = $sth->rowCount();     } } 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data