Issue with using php and sql to add data to datables - php

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.

Related

PHP MySql Result as Table in html

Im trying to list the result from MySQl results as a Table in my HTML.
My PHP file:
$stmt = $this->get('database_connection')
->executeQuery(
'SELECT * FROM members'
);
while (false !== ($objMember = $stmt->fetch(\PDO::FETCH_OBJ)))
$template->listTable= implode(', ', $objMember['firstname']);
return $template->getResponse();
And in HTML:
block('content'); ?>
<p><?= $this->listTable ?></p>
<?php $this->endblock(); ?>
But I dont get any reults in the html file. What is the correct way to list all results from SQL to a Table?
I don't know if it is the right way or not but it works as this :
I create some html table
<table class="table">
<thead class=" text-primary">
<th>table header</th>
<th>table header 2</th>
</thead>
<tbody>
In here my PHP begins
<?php
$query = mysqli_query($mysql_connection, "SELECT * FROM tbale WHERE conditions");
while ($array = mysqli_fetch_array($query)) {
$k_exmaple_1 = $array['column1'];
$k_exmaple_2 = $array['column2'];
echo "
<tr>
<td>$k_exmaple_1</td>
<td>$k_exmaple_2</td>
</tr>
";
}
?>
PHP Ends
I connected the table in MySQL, and selected my rows then print it with echo in html format.
</tbody>
</table>
Table ends

HTML Table size too big after mysql_fetch_assoc

After I put the database values into the HTML table, the table gets to big. Any CSS code doesnt help. Shall I change something in the database value type? Or any other suggestion?
Below is the code:
<?php
$connector = mysql_connect('localhost','root','')
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("user_registration", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM login ORDER BY 1 DESC ");
?>
<table class="table1" border="2" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surename</th>
<th>Email</th>
<th>Gender</th>
<th>Username</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr >
<td >{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['surename']}</td>
<td>{$row['email']}</td>
<td>{$row['gender']}</td>
<td>{$row['username']}</td>
<td>{$row['password']}</td>
</tr>\n";
}
?>
</tbody>
</table>
<?php mysql_close($connector); ?>
You need to select a subset of your records, otherwise the page will grow as fast as the database table... use the LIMIT statement to get back a page at a time...
SELECT id, name, etc
FROM MyTable
ORDER BY id
LIMIT (0, 20)
The next page is
SELECT id, name, etc
FROM MyTable
ORDER BY id
LIMIT (20, 20)
And so on.

Enter data to a MySql database with 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.

Working with two tables and grouping in mysql and php

I have two tables like this.
books
=====
b_id a_id book_name
1 1 ABC
2 1 DEF
3 2 GHI
authors
=======
a_id author_name
1 A1
2 A2
I need a table like this.
S.No BookName
-----------------
A1
==
1 ABC
2 DEF
A2
==
1 GHI
What i'm planning to do is
1. Do a while loop and get the author name first and print it
2. Use the author id inside the first loop and do another one iteration to get the list of book list for each author.
A sample code is like this:
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>S.No</th>
<th>Book Name</th>
</tr>
</thead>
<?php
$mysql_query1 = "my sql query to get the author name list first";
$results = mysql_query ( $mysql_query1 ) or die ( mysql_error () );
while($row = mysql_fetch_array($results)){
?>
<tbody>
<tr>
<td style="background:none;"><u><?php echo $row['author_name']; ?></u></td>
</tr>
<?php
$result = mysql_query ( "mysql query to get the list of books for each author with a where condition like this where a_id=$row['a_id']" ) or die ( mysql_error () );
$book_count = mysql_num_rows($result);
while($row1 = mysql_fetch_array($result)){
?>
<tr>
<?php for($i=1;$i<=$book_count;$i++){ ?>
<td><?php echo $i; ?> </td>
<td><?php echo $row1['book_name']; ?> </td>
<?php } ?>
</tr>
<?php
}
?>
</tbody>
<?php } ?>
</table>
My friends insisted me that the above method is a old one, and there is something to do with just few line codes. Is it?
If yes, could someone redefine my code and give me.
Thanks,
Kimz
Сan this be so?
$mysql_query1 = "select * from authors"
$result = mysql_query ( "select * from books where a_id=".$row['a_id']) or die ( mysql_error () );
The PHP method would be something like:
//First let's get all the values we need and store them in array format
// so that we don't need to make expensive calls to the database
<?php
$result = mysql_query("SELECT author_name, book_name FROM books b, authors a WHERE b.a_id=a.a_id");
$arr = array();
while($row=mysql_fetch_assoc($query)){
//storing books by author
$arr[$row['author_name']][]=$row['book_name'];
}
//so now we have an array like ['A1'=>'ABC','DEF'],['A2'=>'GHI']
?>
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>S.No</th>
<th>Book Name</th>
</tr>
</thead>
<tbody>
<?php
//let's print out the contents of our array in table format
foreach($arr as $author=>$val){
echo "<tr>
<td style='background:none;'><u>".$author."</u></td>
<td></td>
</tr>";
foreach($val as $ind=>$book_name)
{
echo "<tr>
<td>".$ind."</td>
<td>".$book_name."</td>
</tr>";
}
}
?>
</tbody>
</table>
Note:
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

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