why pressing a button a php form the table displayed disappear? - php

I have a form with a list box which report some id client recorded in a MySql database. I select one of this id client I click on a button - named selected-record, I get data from the db and these are displayed on a php table. This is fine. Then I click on a second button called delete-record with the goal to get the id of the client - variable $num_client - and to run the sql delete instruction to delete the record. 2 points I was not able so far to fix.
Why pressing "delete_record" button the table displayed pressind selected_record is immediately cancelled on a form?
Why when I press delete_record the instruction echonum_client;` gives an empty value?
if ( isset($_POST['selected_record'])){
// visualiazzazione tabella
echo "<table border='1'>
<tr>
<th>Id cliente</th>
<th>Ragione Sociale</th>
<th>address</th>
<th>village</th>
<th>Provincia</th>
<th>Contatto</th>
<th>Dipartimento</th>
</tr>";
echo "<tr>";
echo "<td>" . $num_client. "</td>";
echo "<td>" . $rag_soc. "</td>";
echo "<td>" . $address. "</td>";
echo "<td>" . $village. "</td>";
echo "<td>" . $provincia. "</td>";
echo "<td>" . $contact_client. "</td>";
echo "<td>" . $dipart. "</td>";
echo "</tr>";
echo "</table>";
$result=mysqli_query($con,"SELECT id_cliente , ragione_sociale FROM clienti");
}
if( isset($_POST['delete_record'])) {
// Here If I make echo $num_client this gives no value ??
//$sql_del = "DELETE FROM `db_ordini_clienti`.`clienti` WHERE `id_cliente` = $num_client";
}

Related

Filtering MySQL Database by field result

I am pulling stock data off of a trading API through a loop based on an aray of stock symbols. This spits out xml data which I then parse and input into a MySQL database for analysis later. This is an example of the fields I'm inserting into the database (I can add more):
$sql = "INSERT into securities (symbol, ask, ask_time, asksz)
VALUES ('$symbol', '$ask', '$ask_time','$asksz')";
My current query pulls all of the data and lists it in rows, entry after entry into a table:
$sql = "SELECT id, symbol, ask, ask_time, asksz FROM securities";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>symbol</th>";
echo "<th>ask</th>";
echo "<th>ask_time</th>";
echo "<th>asksz</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['symbol'] . "</td>";
echo "<td>" . $row['ask'] . "</td>";
echo "<td>" . $row['ask_time'] . "</td>";
echo "<td>" . $row['asksz'] . "</td>";
echo "</tr>";
}
echo "</table>";
Is there a way to compare two separate stocks side by side? For instance, structure my query in order to pull those 4 fields only for 'AAPL', then in the same row pull the same data for 'GOOG'?

Read More Button

I have the below table in my site...
and what I want is that when i click on the read more link to redirect me to the page view_announcement.php and in this page to display me the whole data for this specific row.
For example, if we click on the link in the second row I want in the view_announcement.php to load all the data for this specific row.
My code in order to display this table is this...
<?php
$q = ($_GET['q']);
$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"project");
$sql="SELECT author,category,subject,content FROM announcements WHERE category = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Author</th>
<th>Category</th>
<th>Subject</th>
<th>Content</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . 'Read More' . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
The view_announcement.php file doesn't contain any code yet because i dont know what to write.
One way to do it is to append a query variable to the "Read More" links. You'll probably need a unique identifier, such as an ID number, on your announements table. If you don't have one yet, I suggest adding one and setting it up to auto-increment.
You would want to modify your query to include the unique ID number:
$sql="SELECT id,author,category,subject,content FROM announcements WHERE category = '".$q."'";
Then you would modify the loop which prints your table out to include those unique IDs in the URL to view_announcement.php
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['author'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . 'Read More' . "</td>";
echo "</tr>";
}
And in your file view_announcement.php, you would make another SQL query to get the full data for a specific row, like this:
$sql="SELECT * FROM announcements WHERE ID = '".$_GET['id']."'";
If you click any button, that redirects to view_announcement.php file, where you can get the subject values.
Use that subject values in your query to get all the details which relates to that subject.

How to put user input into a SQL Query

I have a database and I want the user to be able to have an input into what comes out. i.e
Select from Table where example = user input from box **(input by the user)**
Im guessing what I need is a variable to hold the value that then goes into the statement. I know how to get the value from the input box with script but can I use it like:
select * From handover WHERE hdate = variable. However I am guessing someone is going to talk to me about security if its even possible.
<html><body>
<input>User input</input> //That needs to go into statement
<?php
include 'config.php';
$result = mysqli_query($con,"SELECT * FROM handover WHERE hdate = **user input**;");
echo "<table border='1'>
<tr>
<th>hdate</th>
<th>Delay</th>
<th>Health and Safety</th>
<th>Non Vsa</th>
<th>VSA</th>
<th>Dar</th>
<th>Other</th>
<th>Hour</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['hdate'] . "</td>";
echo "<td>" . $row['hdelay'] . "</td>";
echo "<td>" . $row['hs'] . "</td>";
echo "<td>" . $row['nv'] . "</td>";
echo "<td>" . $row['vsa'] . "</td>";
echo "<td>" . $row['dar'] . "</td>";
echo "<td>" . $row['other'] . "</td>";
echo "<td>" . $row['hour'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Any help is welcome and advice on the best language to use for this.
Kind Regards
Fintan
first of all, this question has nothing to do with javascript & ajax. so you can delete those tags.
you want to show/search data from mysql.
$result = mysqli_query($con,"SELECT * FROM handover WHERE hdate = '".$_POST['abc']."' ");
this is when you want to check if hdate column have exact data as user input ( $_POST['abc'] ).
and also don't forget to use mysqli_real_escape_string
you can learn common mysql pattern queries from here: http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

Sending MySQL data to page through link in a table

We are currently working on a game shop website and have hit a roadblock regarding the purchase link.
The link displays within a mysql table and each link sends the user to the same page.
This is necessary as we will be adding new games to the database and want to do using only a mysql command to make the site as efficient as possible.
This is the code of the table (ignore the fact that the purchase link displays the 'gameCodes'.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['gameName'] . "</td>";
echo "<td>" . $row['pointsValue'] . "</td>";
echo "<td>" . ''. $row['gameCodes'] .'' . "</td>";
echo "</tr>";
}
echo "</table>";
What I am wanting to do is send the game code of the game that corresponds with the row the link is on to the Purchase.php page to then process the purchase.
Any help is appreciated greatly.
my answer deals with not only passing variables from url to your page....but passing it in clean way
First, make sure that url URL properly encode using "PHP urlencode"
echo "<td>" .
'<a href="Purchase.php?gameCodes='.urlencode($row['gameCodes']) .'">'.
$row['gameCodes'] .
'</a>' .
"</td>";
Then to fetch the data strip_tags from the url variable if any:
echo (strip_tags($_GET['item']));
why is this needed??
Since you are fetching the values from URL, assume i manually change the url to :
Purchase.php?gameCodes=<script>alert("hello")</script>
then without proper handling, gameCodes variable value will be fetched and it would alert "hello" on the page
Have you thought about sending it through the URL like follow:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['gameName'] . "</td>";
echo "<td>" . $row['pointsValue'] . "</td>";
echo "<td>" . ''. $row['gameCodes'] .'' . "</td>";
echo "</tr>";
}
echo "</table>";
and then process to the purchase using the code sent in the URL
let me know if it corresponds to what you need.
I think you can put the gameCodes id directly in the link
echo "<td>" .
'<a href="Purchase.php?gameCodes='. $row['gameCodes'] .'">'.
$row['gameCodes'] .
'</a>' .
"</td>";
Now you can process the code from the purchase page and retrieve it with $_GET
$_GET['gameCodes'];

php mysql echo | How to inherit colors from a submit form?

I'm having some real trouble finding information on this topic and I would be very appreciative for any help. In short I have a form where users select a category from a drop down list, enter some contents, and hit submit which goes to SQL. Each category in the dropdown is color coded:
<option STYLE="color: #00CC66;" value="Option_1">Option_1</option>
<option STYLE="color: #0066CC;" value="Option_2">Option_2</option>
<option STYLE="color: #996633;" value="Option_3">Option_3</option>
etc
Then I have a php that pulls up the stored submitted data (categories and contents) into a table on that same page sorted by date.
<?php
$con=mysqli_connect("localhost","myuser","mypassword","mydb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM mytable order by date DESC");
echo "<table border='1'>
<tr>
<th>Category</th>
<th>Contents</th>
<th>Date/Time</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['contents'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
My question is, when echo places the information in the table, is there a way I can get 'category' showing up in the same colors as the user form? (IE, on the table, Option_1 would show up as #00CC66, Option 2 as 0066CC, etc...)
Basically I want the actual category text on the fetched table to display the same as it is in the drop down form. I don't mind if I need to manually set each one as the categories are limited, I just have no clue where to begin on this one. Appreciate any help in advance!
Yes, but you would need to either change the value of the select box to the colour or manually do it like this:
function getColor($strOption)
{
switch ($strOption)
{
case "Option_1":
return "#00CC66";
case "Option_2":
return "#0066CC";
#etc
}
}
Then in your while loop:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><font color='".getColor($row['category'])."'> " . $row['category'] . "</font></td>";
echo "<td>" . $row['contents'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "</tr>";
}

Categories