Skip to main content

Posts

Showing posts from June, 2023

Prepared statements, form submission, form validation and displaying errors in php

PHP Code. Insert statement Query. INSERT INTO `users`(`user_id`, `user_first`, `user_last`, `user_email`, `user_uid`, `user_pwd`) VALUES (1,'Adnan','Zaib','text@.com','Admin',1234) What are Prepared statements and how to use them //Created a sql prepare template $sql = "SELECT * FROM users WHERE user_uid=?;"; //than send to database with certain values left(unspecified parameters). //Create a prepared statement $stmt = mysqli_stmt_init($conn);   //prepare the prepared statement if(!mysqli_stmt_prepare($stmt, $sql)){ echo "sql statement failed"; }else{ //bind values to parameters and further to placeholder mysqli_stmt_bind_param(#stmt, "s",); // ‘s’ stands for datatype of parameter that you are going to pass. }   while($row = mysqli_fetch_assoc($result)){ echo $row['user_uid'] . "<br>"; } PHP form Validation   <?php // define variables and set to empty values $name = $email = $gender = $comment = ...

Relational queries in laravel

 1. Suppose you want to query parent model and also apply condition on child relation model and you also want to get specifies data, not all data from child table in hasMany relationship. $collection_products = Product::on('business');             // query()             if($selectedColor != null){                 $collection_products = $collection_products->with(['variants' => function($query) use ($selectedColor){                 $query->where('color','=',$selectedColor);             }]);             $collection_products = $collection_products->whereHas('variants',function(Builder $query) use ($selectedColor){                 $query->where('color','=',$selectedColor);             });     ...