Dynamically Collecting Data from 2 tables to add to another - php

So I have the following code to collect information for two sets of information, customers and products :
if(isset($_POST['q'])) {
$q = $_POST['q'];
$select = $db->query("SELECT * FROM products WHERE Model LIKE '%$q%' OR Description LIKE '%$q%' ORDER BY id DESC");
} else {
$select = $db->query("SELECT * FROM products ORDER BY id DESC");
}
<form method="post">
<input type="text" name="q" /> <input type="submit" name="search" />
</form>
The code is duplicated across both products and customers. I would like to create another page that will let me type in a form field that auto-populates with each database table (customers/products) and, after information is selected, allow the update of a third table (invoices).
In the same page, I would like to allow the addition of as many "products" as the user would like, via an icon "+" sign next to each row of products.
What would be the best way to approach this?

Related

How to auto-fill a form after doing a search

For a games reservation system that I have created, I have made a search bar to allow for easier navigation of the games available. However, to make the system quicker to make a reservation, I would like to create a link to a form that would automatically post the details of the game into the reservation form, but I'm not sure how.
Here is the code I used to create the search bar, if needed.
$sql= "select * from games ";
if (isset($_POST['search_box'])) {
$search= mysql_real_escape_string( $_POST['search_box']);
$sql .= "WHERE GameName LIKE '%{$search}%' ";
$sql .= " OR GameDescription LIKE '%{$search}%'";
$sql .= " OR GameID LIKE '%{$search}%'";
}
$result = mysql_query($sql) or die(mysql_error());
After the PHP code, I used HTML, to create a search form:
<form name="search_form" method="POST" action="index.php">
Search: <input type="text" name="search_box" value="">
<input type="submit" name = "search" value = "Search">
</form>
You could pass the parameters in the url. For example, build a link like so:
Link to reservation form
Then on your reservation form page, use php to check the query string for those parameters using $_GET['game'] (would return "Zelda") and $_GET['sky'] (would return "blue").

SESSION variable in while loop not being sent correctly to second .php file

I'm trying to create a simple topic and posting system and I have two tables in a MySQL database; a topics table and a posts table. In my posts table I have a post_parentID field that links to a topic_id field in my topics table.
Here is how I start my query selecting (and displaying) all the topics that are in the topic table. I then save the topic_id field into a variable:
<?php
$sql = "SELECT * FROM topics INNER JOIN members ON topics.topic_by = members.id";
$topics = $mysqli->query($sql);
if ($topics->num_rows > 0) {
// output data of each topic
while($row = $topics->fetch_assoc()) {
//Saving TopicID into variable
$topic_id = $row["topic_id"];
$_SESSION['topicid'] = $topic_id;?>
Right after I display the html for each topic, I start a new while loop to display each post within each topic:
<?php
$sql = "SELECT * FROM posts INNER JOIN members ON posts.post_by = members.id WHERE post_parentID = $topic_id";
$posts = $mysqli->query($sql);
if ($posts->num_rows > 0) {
// output data of each post
while($row = $posts->fetch_assoc()) { ?>
Right after this, I display the html for each post and then I display the form-control to enter a post, which is directed to a send_post.php file:
<div class="chat-message-form">
<div class="form-group">
<form action="includes/send_post.php" method="post">
<textarea class="form-control message-input" name="post_content" placeholder="Enter message text"></textarea>
<input type="submit">
</form>
</div>
</div>
When I call $topic_id in my send_post.php file like this:
$topic_id = $_SESSION['topicid'];
It only returns the very last topic_id in my topics table instead of the current topic_id in the while loop. Is my logic about this right or should I be doing something different?
By doing this in your loop:
$_SESSION['topicid'] = $topic_id;
you're overwriting the value of topicid everytime you loop. I don't know how exactly you want your topicid to look like but a possible way to avoid this would be:
$_SESSION['topicid'] = array();
while($row = $topics->fetch_assoc()) {
$_SESSION['topicid'][] = $topic_id;
...
Another easy way to the this is:
">to your next page // this will attach specific ids to each link during loop
Afterwards, go to your next page and use the $_GET method to get the id send via url...then apply it to ur query there
$topic_id= 0 //set this before the loop
while(){...
.....
$topic_id++; //increment id by one for each loop
echo $topic_id // if you want to print the values of the id
to your next page // concatenate specific id to selected link
}// close while loop
in your nextpage.php
$variable_name=$_GET['$topic_id'];// this way you get the specific id concatenated to the particular link clicked

Subtract number from column and add to another

What I want to do is have a button allows that when clicked takes away 1 from the column 'tickets' in the table 'event' and adds it to the column 'ticket' in the table 'user'.
Code for button
form action="sendellie.php" method="post">
<input type="submit" value="Buy tickets"/>
</form>
Snippet of Code for processing page (sendellie.php)
<?php
$sql = "UPDATE event SET tickets = tickets-1 WHERE name='Ellie Goulding'";
$sql = "UPDATE user SET ticket = ticket+1 WHERE name = '$uname'";
?>
The problem with the code above is that it does not work. It does not take away or add anything.
Any suggestions or tips would be much appreciated. Thanks

PHP/MySql - Creating navigation buttons

I'm totally stuck! I need to create navigation buttons in order to go through records that match a specific Id. So far I have only been able to retrieve 1 record (the first one). I am using the following code to do so:
$query = "SELECT t1.requester_name, t1.requester_email, t1.client_name, t1.client_country, t1.machine_quantity, t1.severity, t1.sales_connect, t1.request_status, t2.serial_type, t2.serial_model, t2.serial_number, t2.machine_status, t2.machine_config, t2.brand, t2.tier, t2.request_id
FROM requests t1
LEFT JOIN serialnumbers t2
ON t1.request_id = t2.request_id
WHERE t1.request_id = {$id} ";
$results = mysql_query($query, $connection);
$row = mysql_fetch_assoc($results);
The records are coming from 2 distinct tables and while I am having no trouble with the Request Table (Always one row), I cannot manage to figure out how to get every row in the Serial Numbers table (one to many relationship).
I am using a html form to display these values... Here is a sample of how I am doing so:
<input type="text" value="<?php echo $row['serial_type']; ?>" id="machineType" name="machineType[]" />
Any ideas on how I can achieve this objective?
you are going to want to make your $row = mysql_fetch_assoc a while loop.
while ($row = mysql_fetch_assoc($results)){
echo '<input type="text" value="'.$row['serial-type']." id="machineType" name="machineType[]" />';
}
something along those lines.

PHP/MySQL Like Button

I've made a 'like' button for my product pages with this code:
<?php
if('POST' == $_SERVER['REQUEST_METHOD']) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" value = "like"/>
</form>
Works like a charm excpet for one minor problem being that every visit to the page registers a 'like'.
Could someone help explain what i need to chnage/add in order that new 'likes' are only registered when the actual form is submitted?
Thanks
Dan
A better solution rather than submitting the page and the whole page reloading would be to make an AJAX request, this is how Facebook 'likes' work.
This can be achieved using the jQuery JavaScript library.
The general outline would be:-
1) Click button
2) Send AJAX request
3) Update HTML to show button has been clicked and prevent reclicking of button.
<?php
if($_POST['like']) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" value = "like" name='like'/>
</form>
This should work ;-)
<?php
if ($_POST['like']){
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" name="like" value = "like"/>
</form>
First of all - in your sql you have:
`product_id` = '1'
do not use id value as a string:
`product_id` = 1
About your problem:
Add another condition:
if ('POST' == $_SERVER['REQUEST_METHOD']) {
if ( !empty($_POST['submitType']) && ( $_POST['submitType'] == 'like' ) ) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
}
and in html:
<input type = "submit" name="submitType" value = "like"/>
Sounds like some kind of old question, but I wonder why noone has said, that op's approach doesn't sound quite right. You try to just count likes (set likes=likes+1). It has many disadvantages:
You miss information, who gave the like. Thus you won't be able to reconstruct the whole picture
Users won't be able to "undo" likes (as you don't record who liked the post)
In case of many concurrent likes I feel like you'd get some kind of data race or a long delays, because MySQL would need to process every request on a single field in order.
Much better idea is to create separate table in the DB named "product_likes" with columns like product_id, user_id, date. Of course, product id and user id should be unique together.
Thus you'll always know the full picture and will be able to see who liked the product. Even if accidentally you'll issue the second like from the same user about the same product, it won't be stored due to db constraints.
Also it will be possible to extend it to i.e. emotions-reactions, just by adding new column like "like_type" and updating the constraint correspondingly.

Categories