query data from more than one table - php

I have a search function that currently grabs data from one table, and I'd like to grab data from an additional table, as well.
$query = $this->db->get('tbl_customer');
$this->db->select('in_customer_id, st_company_name, in_customer_type, st_customer_account, st_customer_state_id, flg_customer_account_type, in_status, dt_added_date, st_tag');
if(trim($action['searchtxt'])!='')
$this->db->like('st_company_name', html_entity_decode($action['searchtxt']));
The view:
<div class="floatl" style="width:250px;">
<form name="frm" action="<?php echo $index_url; ?>customers/search/" method="post">
<div class="floatl">
<input name="Search" type="text" class="textboxsearch" id="Search" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" value="<?php if($searchtxt!=''){ echo $searchtxt; } else{ echo 'Search'; }?>" maxlength="50" />
</div>
<div class="floatl searchicon">
<input type="image" src="<?=$admin_base_url?>images/textbox_search.gif" alt="" width="22" height="22" />
</div>
<br />
<br />
<font class="txt9">(i.e. Company, Account name)</font>
</form>
</div>
The table I want to additionally search is called tbl_admin_user. Any ideas on how I can accomplish this?

You might want to brush up on your SQL a little, especially take a look at SQL Joins.
With that said, after re-reading your question it appears that you are attempting to search particular columns for fairly specific data within multiple tables. First you might want to look into using LIKE instead of WHERE.
Second, depending on how you're displaying the results you you'll probably either write two separate queries and then loop through each of the separate results and display them. A Union or Join might be possible but also might be difficult to display the results accurately if the table structures are really different.

It seems that you are using an ORM/framework to access data, which one are you using.
Anyway, you are probably looking for join or union.

Related

how to enter div value into database

Below is my form- which value i like to update into database.
<form action="#" class="form">
<div class="form__slider">
<div class="form__slider-rating" id="slider__rating"></div>
<div class="form__slider-value" id="form__slider-value"></div>
<!-- <input type="number" class="form__slider-value" id="form__slider-value" value=""> -->
</div>
<input type="hidden" name="movie_id" id="movie_id" value="<?php echo $post_id; ?>" />
<input type="hidden" name="name" id="name" value="<?php echo $userName; ?>" />
<textarea class="form__textarea" name="remark" id="remark" placeholder="Review"></textarea>
<button type="button" class="form__btn">Send</button>
</form>
and in site it was look like this
This blue mark value I need to update into database. How to do that?
Regarding PHP, you need to handle a form.
Firstly, write a code that would handle the form itself, you can do that, by putting some php on top of your page;
if(isset($_POST['form-slider-value'] && $_POST['form-slider-value'] !== null) {
$value = $_POST['form-slider-value'];
//Database handle
}
Of course don't forget to add name 'form-slider-value' to the input.
Ideally there would be classes or similar stuff for that, I'd advise you to use some kind of framework.
Basically. PHP and FORMS works with handling them, once you submit your form with a button submit, it goes to a place where you address it Form action=. Then everything that is written within names will be at $_POST data.
It is not recommended to keep standalone code in one file, with HTML,css,PHP all together.
Hope that answered your question.
As well. This thing is very common to do or use. I advise you to learn about PHP Forms.
https://www.w3schools.com/php/php_forms.asp
And read about constructing a class. Id say do it the proper way.
Learn about frameworks as well, they are modern day time saviors once you learn it.

Add array with unknown size into database

I need to build a page where the user can enter the name of the categories of products of an webstore. I created a page with dynamic input fields so the user will be able to add all the categories (which i don't know the ammount). I have to get the name of those categories and put into a database. I got a script online and made a few changes but i dont know how to proceed further on. I need help with the php script, how to get the array and insert into the database.
This is my form script:
<form method="post" action="setup3.php">
<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div>
<input type="text" name="mytext[]">
</div>
</div>
<div>
<input type="submit" value="Next" />
</div>
</form>
I think this would point you in the right direction:
<?php
if (! empty($_POST['mytext'])) {
foreach ($_POST['mytext'] as $entry) {
// perform your insert here.
}
}
I would strongly recommend using an ORM like Doctrine (http://www.doctrine-project.org/) since it already tackles a lot of security issues for you.
If you wish to stay pretty close to low-level database interactions, writing plain SQL queries, I'd recommend using PDO (http://php.net/manual/en/book.pdo.php)

PHP MySQL Linking Posts to Users

I decided to open a new question for this topic, as I have been having issues with it. My blog has a mysql database with the table posts complete with a post_user field. Each user has a form and can submit a post to a public blog. Once they submit this form, complete with a title and a body, when it is posted on the blog it should say by Username. My idea is that I can save the username to the post_user in the database upon form submission using $_SESSION info, but I am not exactly sure how to do this. Any help would be greatly appreciated.
The form, with the old UNSECURE way of entering the session author, note I am only including session data in the form to display what I am trying to show on a blog post, I don't know how to do it any other way hence my question:
<form action="" method="post">
<p>
<input type="hidden" name="user" id="user" value="<?php echo $_SESSION['user']['username'] ?>" readonly />
</p>
<p>
<input type="hidden" name="userid" id="userid" value="<?php echo $_SESSION['user']['id'] ?>" readonly />
</p>
<p>
<label for="title">Title: </label>
<input type="text" name="title" id="title" />
</p>
<p>
<textarea name="body" rows="20" cols="60"></textarea>
</p>
<p>
<input type="submit" value="Add Post" />
</p>
</form>
Code that posts the entry:
$posts = get_posts();
foreach($posts as $post)
{
?>
<h2><?php echo $post['title']; ?></h2>
<h4>By <font color="#FF6347"><?php echo $post['user']; ?></a></font> on <?php echo $post['date']; ?></h4>
<h4><?php echo $post['total_comments']; ?> comments, last comment <?php echo $post['last_comment']; ?>
<hr />
<p><?php echo $post['preview']; ?></p>
<?php
}
?>
You shouldn't save the username on the posts table. You should have two separate tables: one for the posts and one for the users, which are linked via a user_id field, a.k.a foreign key.
This is the way relational databases, such as MySQL, operate. This serves a few purposes:
You avoid repetition in case of one to many relations, which is exactly your case: one user can post many posts.
You use a unique identifier.
The information is organised better instead of mixing data together.
The way to tie the tables together when retrieving data is by using a JOIN statement, which links the two tables via the common user_id field. It is highly recommended to make this field an index, which would significantly speed up data retrieval. (Think about the difference between finding a certain word or sentence in a book by scanning through each and every page, and finding a chapter using the, well, index)
To sum up, you should have an id key in your $_SESSION super-global array, which you then insert to the user_id field upon post submission.
As for your question, you don't need to send the session data with a hidden input field, because it will be available even after submission - after all this is what session is all about. So why convert it to $_POST and expose it publicly?

HTML Forms to query multiple pages

i am trying to build a meta search engine.
I currently have the following code.
<form method="POST" action="google_basic.php">
<label for="service_op">Service Operation</label><br/>
<input name="service_op" type="radio" value="Web" CHECKED />
Web <input name="service_op" type="radio" value="Image" />
Image <br/> <label for="query">Query</label><br/>
<input name="query" type="text" size="60" maxlength="60"
value="" /><br /><br /> <input name="bt_search" type="submit"
value="Search" /> </form> <h2>Results</h1>
{RESULTS}
I need the form to have more than one action= ""(I realise a form can only have one action, i need the equivalent of 3 actions ="" ). The form needs to access 3 search engines and display the results. What is the best way to do this?? I know that javascript may an option but is not a solution for me as it may be switched off in the clients browser.
Any ideas on the best way to go about this??
TIA
You need to perform the 3 "actions" on the server (in or from the google_basic.php file). After POSTing to the server, you can perform an arbitrary number of "actions" from there.
See also: http://us2.php.net/manual/en/intro.curl.php
it only needs one action, then the action is what will display all 3 searches. so instead of having google_search.php, bing_search.php, and yahoo_search.php, combine them all into a generic search page that will display all 3

PHP Automatically Generate new pages

I'm making a social network type site, where users can upload their items to be rated. However, I'm trying to improve the way the site is laid out, so want to automatically generate pages once the user inserts a new item. The add.php page has the following form:
<form action="add.php" method="post" autocomplete="on" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" id="pic">
<p> <label for="jname" class="iconic user"> Name of Jam <span class="required">*</span></label> <input type="text" name="jname" id="jname" value="<?php if (isset($_POST['jname'])) echo $_POST['jname']; ?>" required="required" placeholder="Input your Name of Jam here" /> </p>
<p> <select name="jtype" id="jtype" value="<?php if (isset($_POST['jtype'])) echo $_POST['jtype']; ?>" required="required">
<option value="jam">Jam</option>
<option value="jelly">Jelly</option>
<option value="marmalade">Marmalade</option>
<option value="preserve">Preserve</option>
</select> </p>
<p> <label for="producer" class="iconic user"> Jam Producer <span class="required">*</span></label> <input type="text" name="producer" id="producer" value="<?php if (isset($_POST['producer'])) echo $_POST['producer']; ?>" required="required" placeholder="Input the producer of the Jam here" /> </p>
Upload a picture of your jam: </br> </br> <input name="userfile" type="file" /> </br>
<input type="submit" name="submit" value="Register" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
When the form is submitted, I then want it to generate a new page for that new user created item. Is there a fairly simple way of doing this?
Cheers
You don't have to actually 'create' new html page for each item.
You could save this information to database (mysql for example).
Then you could create another php file, say 'item.php' and access different entries from mysql database like so:
item.php?id=1
This generally isn't the way such sites are created. (i.e.: You don't generate the physical pages themselves at the point of form submission.) Instead, you'd usually store the form data in a database and then retrieve/display it based on the URL - either by decoding the URL itself via a "controller" or by using a query string variable such as ?producerid=x. This data would then be used to populate a template.
To be honest, I'd really recommend getting hold of a recent PHP book (as far as database access is concerned, you should be using PDO or MySQLi) or following some online tutorials - whilst it might initially seem like this won't be a meaningful form of progress, its likely to pay substantial dividends in the long run.
The best and efficient way to do it is to store the data in a database and retrieve it whenever user clicks on specific item. One thing though, if part of your plan is to make your site accessible by google search engine, you have to generate the individual web pages... because, google is only a web crawler...it cant get into mysql or other databases.
Usually there's no new page generation for things like that. You should create a template and load dynamic informations from other sources (such an XML file or a database) to it so that it seems a completely new page.
Just:
See what each item page has in common
Define a template which contains the common code
Retrieve dynamic informations (item infos for example) from a database
Use PHP embedded in HTML to load dynamic HTML
An example:
Facebook does not create a new page per each user registration. There's an HTML template which defines the position of the profile photo, the position and style of the posts, the position and style of the friend list and stuff common to any profile page, and then just load different informations when you call such a page for Mark, Frank, Jeff and so on.

Categories