Enter data to a MySql database with PHP - php

I have a MySql database which has users and each user gets a grade see below:
ID NAME GRADE
1 Sean 10
2 Sarah 15
3 Seo 12
4 Ste 32
Basically, I want to be able to dynamically print out whats stored in the database and add a text input box which enables me to enter in grades which are stored for each individual person. I then want this grade to be stored in the database for the user. Is this possible?
I have code which prints out the users but doesn't include a text box
<table style="width:50%" style = "background-color: transparent;">
<tr>
<th>ID</th>
<th>NAME</th>
<th>GRADE</th>
</tr>
<?php
$sql = "SELECT id, name, grade FROM students";
$result = $conn -> query ($sql);
if($result -> num_rows >0){
//output data of each row
while($row = $result -> fetch_assoc()){
?>
<tr>
<td> <?php echo $row["id"] ?> </td>
<td> <?php echo $row["name"] ?> </td>
<td> <?php echo $row["grade"] ?> </td>
</tr>
?>
</table>

Research a bit on HTML forms here
HTML Forms
You can either use post or get methods for the form to send the values to PHP.
After you have set up your form you can simply use the variables $_GET[formvariable] if you are using get or
$_POST[formvariable] if you are using post.
You can then use mysql to update values in the database when the form is submitted.

Related

Issue with using php and sql to add data to datables

So I'm trying to use php and mysql to put data in my datatables on my website. I wrote some code at the top of my file to confirm that I am accessing my database correctly. The table is displayed correctly when i manually entered some data, but with my php code, the table says "No data available in table". Any thoughts on whats wrong with my php?
$q = "SELECT tickets.ticket_id, tickets.section, tickets.row, tickets.price, users.first_name as first_name, users.last_name as last_name
FROM tickets
LEFT JOIN users
ON tickets.seller_id = users.umid
Where(tickets.game_id = '$g')";
$r = #mysqli_query ($dbc, $q);
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Section</th>
<th>Row</th>
<th>Price</th>
<th>Seller</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '
<tr>
<td>'.$row["section"].'</td>
<td>'.$row["row"].'</td>
<td>'.$row["price"].'</td>
<td>'.$row["first_name"]. " ". $row["last_name"]. '</td>
</tr>
';
}
?>
</tbody>
</table>
This is what the table looks like
Link to the webpage
Figured out the problem. I looped through my row earlier in my code, so when i wanted to put the info in the table, it was at the end of the array.

Passing a database value from html table hyperlink to SQL query on next page

I need help passing a value from a database. I've done this successfully with drop down boxes, and getting the selection then posting with a submit button where it would fill the tables in Display.php successfully. However, I'm now using an html table that fills with values as a list and uses the serial number as a hyperlink.
When the user selects the link and it opens Display.php, I want to grab the serial number associated with that link/row and use it in the SQL query on the display.php page so it can match that serial number with a stageID in my staging table and select all values for that row.
In the following code, if I debug the $_GET array, it shows the correctly chosen serial number. If I debug/print $_GET and $result1, I get:
Array ( [id] => 70066665 ) mysqli_result Object ( [current_field] => 0
[field_count] => 230 [lengths] => [num_rows] => 0 [type] => 0 )
The fact that these are showing the correct serial number and row lengths, I know that the dashboard page and hyperlink code are working, as well as my SQL connection. I feel like there may be an issue in the way I've created the query on Display.php where it just isn't grabbing or matching correctly.
Here is the code:
Dashboard.php
<?php
$query1 = "SELECT * FROM staging;";
$result1 = mysqli_query($connect,$query1);
while($row = mysqli_fetch_array($result1)){
?>
<tr>
<td><? echo $row['workOrderPacket'];?> </td>
<td><? echo $row['workOrderNum'];?> </td>
<td><? echo $row['date'];?> </td>
<td><? echo $row['utility'];?> </td>
<td><? echo $row['serviceName'];?> </td>
<td><? echo $row['address'];?> </td>
<td><? echo $row['serialNumber'];?> </td>
<td><?php echo ''.$row['serialNumber'].''; ?> </td>
</tr>
<?}?>
</table>
Display.php
<?php
//if(isset($_POST['submit']))
if(isset($_GET['id']))
{
$query1 = "SELECT * FROM staging WHERE stageID = ".$_REQUEST['id'].";";
$result1 = mysqli_query($connect,$query1);
while($row = mysqli_fetch_array($result1)){
?>
/////20 HTML tables filled with values from database
/////
In your hyperlink, you set the id using serialNumber but in your display.php, you are querying the result using stageID.

PHP/mySQL Data not being displayed in table

I am having an issue with MySQL data not being displayed via PHP
Below is the code which the data should be outputted into:
THIS IS individual_item_page.php
<?php
if (isset($_GET['suburb']))
{
$_SESSION["dog_park"] = $_GET['suburb'];
}
elseif (isset($_GET['keyword']))
{
$_SESSION["dog_park"] = $_GET['keyword'];
}
else
{
$_SESSION["dog_park"] = $_GET['rating'] ;
}
?>
<h1><?php echo $_SESSION["dog_park"]; ?></h1>
<table border="1" cellspacing="5" cellpadding="5" width="100%">
<thead>
<tr>
<th>Park Name</th>
<th>Street</th>
<th>Suburb</th>
<th>Dog Park Area (m2)</th>
</tr>
</thead>
<tbody>
<?php
$result = $conn->prepare("SELECT * FROM dog_parks.items where suburb = ?");
$result->execute(array($_SESSION['dog_park']));
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><label><?php echo $row['Park_Name']; ?></label></td>
<td><label><?php echo $row['Street']; ?></label></td>
<td><label><?php echo $row['Suburb']; ?></label></td>
<td><label><?php echo $row['Dog_Park_Area_(m2)']; ?></label></td>
</tr>
<?php } ?>
</tbody>
</table>
Below is the output page after the code has been executed:
(No data)
A basic overview of how the page is meant to work is,
I have 3 search types via
keyword
suburb
rating
If I want to search for a dog park by suburb I would select a suburb from the drop down box. (Code at bottom of page)
A table would then display the dog parks in that suburb/area which I would then click on one of those parks displayed (Hyperlinked),
which will take me to the page I am having issues with, 'individual_item_page.php'
Below is the code for the suburb search page which then has a hyperlink to the 'individual_item_page.php' where the issue is..
THIS IS SUBURB SEARCH PAGE
<table class="center"> <!-- Creating a table with the class of 'center' -->
<!-- DROP DOWN BOX -->
<?php
$SUBURB = $_POST['suburb'];
$stmt = $conn->prepare("SELECT dog_park_name from items where suburb='$SUBURB'");
$stmt->execute();
for($i=0; $row = $stmt->fetch(); ){
$_SESSION["dog_park".$i] = $row[0];
?>
<!-- DISPLAY RESULTS -->
<tr> <!-- Adding the first table row -->
<th>Dog Park</th> <!-- Adding the second table header -->
</tr>
<tr> <!-- Adding the second table row -->
<td><a href="individual_item_page.php?suburb='<?php echo $row[$i] ?>' " ><?php echo $row[$i] ?></a></td> <!-- Add the second cell on the second row -->
</tr>
<?php }
?>
</table>
This issue has baffled me for many hours now, any help would be appreciated.
On individual_item_page.php, you have a couple of things going on:
You test for the presence of the first 2 GET variables you may be setting $_SESSION["dog_park"] to, but you fail to test for the third GET variable, $_GET['rating'].
Your SQL statement on that page is searching for a suburb, assuming that $_SESSION["dog_park"] was set to a suburb, despite the fact it may be set to $_GET['keyword'] or $_GET['rating']. Also, I'm not sure why you are binding $_SESSION['dog park'] as an array parameter in your $result->execute() statement.
On the suburb search page you are searching the table items but on the individual search page you are searching dog_parks.items. Was this intentional?
Important Note: On your suburb search page, you use a prepared statement but manually add a user entered variable instead of binding it, which defeats the protection supplied by binding parameters, which is to prevent user-entered data from being directly added to SQL statements.

Viewing data from a certain linked field from mysql table in PHP

I'm stuck in a place where I want to view the other fields of a mysql table when I click onto a linked field.
Here is the code which is used to create a field into a link
<td><a href="fieldview.php?id=' .$row[0]. '"><b>' .$row[1]. '</b></td>
Here $row[0] is the field named "id" in mysql table(exampletable) & $row[1] is the field named "title".
Now, at the "fieldview.php" page I used this code to view other fields:
$id=$_GET['id'];
$sql="SELECT * FROM exampletable WHERE id= '$id'";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
echo '<table id="customers">';
echo '<tr><th>Field 1</th> <th>Field 2</th> <th>Field 3</th></tr>';
echo '<tr class="alt"><td><b>' .$row[1]. '</b></td><td><b>' .$row[2]. '</b></td><td><b>Today!</b></td></tr>';
echo '</table>';
}
But I get nothing at that page. Please can anyone tell me what's the solution?
Try jakenoble's suggestion. if you dont see the problem maybe its due to how you construct your link in the first place.
try modifying to to something like this:
<td><b> <?php echo $row[1] ?></b></td>
Did you connected your database ?
FIRST CONNECT DB!!!!!!!!!
Try Echo:
echo $_GET['id'];
in your file.
But I get nothing at that page. Please can anyone tell me what's the solution?
Did you got the HTML table with nothing?
TRY the following:
<?php
// Create connection
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo $id=$_GET['id'];
$sql="SELECT * FROM exampletable WHERE id= '$id'";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result))
{
?>
<table id="customers">
<tr><th>Field 1</th> <th>Field 2</th> <th>Field 3</th></tr>
<tr class="alt"><td><b><?php echo $row[0]; ?></b></td><td><b><?php echo $row[1]; ?></b></td><td><b>Today!</b></td></tr>
</table>
<?
}
?>
If you get no data in your HTML Tables then run the MySQL query in your Query browser with the ID to find you have data for the ID.
Do specify what you have tried?

Insert/Update Multiple Rows in MySQL with PHP and Jquery

I have a table that is populated by a MS Sql query from one database that gives me values from patient visits and the revenue generated from those visits. I have a checkbox that populates the corresponding text input box showing that visit was paid for. My question is how can I add/update multiple rows using MySQL and PHP. There are two different databases (MySQL and MS SQL).
The HTML table.
<table>
<thead>
<tr>
<th>First Name / Last Name</th>
<th>Alias</th>
<th>Status</th>
<th class="amountDue">$00,000.00</th>
<th colspan="2" class="appliedAmount">$00,000.00<?th>
<th class="variance">$00,000.00</th>
<th>Complete</th>
</tr>
<tr>
<td><td>
<td><td>
<td><?php echo $patRow['VisitNum']; ?></td>
<td><?php echo $patRow['VisitName']; ?></td>
<td><?php echo $patRow['AmountDue']; ?></td>
<td><input type="checkbox"></td>
<td><input type="text" name="Amount[]"></td>
<!-- Invoice Populated from Database --!>
<td><select class="invoiceNumber">
<option>4565</option>
</select>
</tr>
</tr>
</table>
Now the PHP.
<?php
require('../assets/dbconnect.php');
$size_array = count($_POST['Amount']);
for ($i=0; $i<$size_array; $i++){
$query = 'INSERT INTO webportal.test (id, PSID, SysPatVisitID, AmountDue, Amount, InvoiceNum)'.
" VALUES ('', '".mysql_real_escape_string($_POST['PSID'][$i])."',
'".mysql_real_escape_string($_POST['SysPatVisitID'][$i])."'',
'".mysql_real_escape_string($_POST['AmountDue'][$i])."'',
'".mysql_real_escape_string($_POST['Amount'][$i])."'',
'".mysql_real_escape_string($_POST['InvoiceNum'][$i])."')
ON DUPLICATE KEY UPDATE content=VALUES(
'".mysql_real_escape_string($_POST['AmountDue'][$i])."'',
'".mysql_real_escape_string($_POST['Amount'][$i])."'',
'".mysql_real_escape_string($_POST['InvoiceNum'][$i])."''
";
$result = mysql_query($query) or die (mysql_error());
}
So this is what I have so far and when I try to insert into my database it only inserts the first record and I get an error. Eventually I want to pass these variables with Jquery, but I just need to get the PHP working first.
$query = 'INSERT INTO webportal.test (PSID, SysPatVisitID, AmountDue, Amount, InvoiceNum)'.
" VALUES ('".mysql_real_escape_string($_POST['PSID'][$i])."',
'".mysql_real_escape_string($_POST['SysPatVisitID'][$i])."',
'".mysql_real_escape_string($_POST['AmountDue'][$i])."',
'".mysql_real_escape_string($_POST['Amount'][$i])."',
'".mysql_real_escape_string($_POST['InvoiceNum'][$i]).")
ON DUPLICATE KEY UPDATE content=VALUES(
'".mysql_real_escape_string($_POST['AmountDue'][$i])."',
'".mysql_real_escape_string($_POST['Amount'][$i])."',
'".mysql_real_escape_string($_POST['InvoiceNum'][$i])."'
";
You're not matching your 's correctly. '". <stuff> ."''. You only need one closing tick.
You forgot your closing bracket for VALUES.
If you wish for your id to be auto incremented by the DBMS, don't include it in your query at all.
Also, error messages exist for a reason. Learning to understanding them will greatly increase your debugging capabilities. If you don't understand the error, just look it up on MySQL.com.

Categories