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.
Related
I have this leave form where employees can apply for a leave. Everything works fine, the only problem is I can't get the pending status to show up. I've already defined the default value of status on my table.
Here's what it looks like when I view the leaves:
view-leave
and here's my table structure:
Table Structure
I'm not sure if you guys need my view-leave code but here it is:
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM leaves order by id DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['status']; ?></td>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
The DEFAULT key word determines behaviour during an INSERT, not during a SELECT. Whatever values are in the table are what you will see, regardless of what the current DEFAULT is.
If you want something similar to a default during a SELECT, try the following...
SELECT
id,
full_name,
phone,
email,
fromdate,
todate,
reason,
COALESCE(status, 'Pending') AS status
FROM
leaves
ORDER BY
id DESC
This will replace all NULL values in status with 'Pending' while you are selecting the data. It won't change anything in the table, just in your results.
If you want to change what is in the table to get rid of those "blank" values, the you need to do an UPDATE.
UPDATE
leaves
SET
status = 'Pending'
WHERE
status IS NULL
OR status = ''
If you want to stop blanks values getting in to the table in the future, you need to specifically mention all the fields except for the the ones that you want to have a default applied.
INSERT INTO
leaves (
full_name,
phone,
email,
fromdate,
todate,
reason
)
VALUES (
'Joe Bloggs',
'555 555 555',
'joe#bloggs.com',
'2017-04-01',
'2018-12-25',
'Just Because'
)
That list of field names at the start is critical.
If you skip it, you're telling the database you want to set the values for every column, and so you never want the default used. The same is true if you include status in the list : Even if you give the value NULL or '', you're telling the database that's the value you want, regardless of what the default might be.
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.
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.
I'm currently working on a table that contains 'Name', 'Info', 'Price' and 'Duration'. However the PHP-Script is connected to a database.
I want to autofill the tablecells: 'Name', 'Price', 'Duration' via the Database by using PHP and SQL and that works perfectly fine for me. Though, I want to customize the content that's in the individual Info cells (e.g. Readmore jQuery and redirect to other pages).
I tried a little bit with using tags inside the Database and other weird stuff which, obviously, didn't work.
Is there a more elegant way of solving my problem than setting up a complete normal table without PHP/SQL, where I'd have to put in every bit of data about Name,Price and Duration manually?
<table class="table table-bordered table-hover tablesorter row sortable">
<thead>
<tr>
<th class="header">Name</th>
<th class="hidden-xs">Info</th>
<th>Price (in Euro)</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
//Connect to Database
$db=mysql_connect ("xxxx", "xxxx", "xxxx") or die ('Oops! Da hat wohl' . mysqli_error(). 'Mist gebaut');
//Choose Database
$mydb=mysql_select_db("massageke_de");
//Query Database
$sql="SELECT * FROM Angebote";
//-run the query against the mysql query function
$result=mysql_query($sql);
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$row['Info'];?<</td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Thanks in advance for helping.
Don't bother asking me additional questions.
P.S.: I hope you can help, without needing the CSS of Bootstrap, that I used.
P.P.S.:I know that my PHP-Script is not protected against PHP-Injection
(but I want to and will learn how to secure it)
Edit: As Jester asked for it. I made it quickly with photoshop because I think an image can express much better, what I want to achieve, than my poorly coding-skills.
Get to the image
Seems to me like the easiest way would be to just edit the info column in the database for each? If you want to do it in php i'd suggest making an array using the names (ids would be better but it seems you don't have access to those?) as keys:
$info['Aroma Teilkoerper'] = "Text about aroma teilkoerper";
$info['Aroma Ganzkoerper'] = "Text about aroma ganzkoerper";
and so on until you have all. Then in the loop:
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$info[$row['Name']];?></td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
Hoping that is a workable solution for you? (also you had a syntax error in your closing php tag.
echo $Name =$row['Info'];?< // <----- should be ?> of course
I will have to mention first that I have searched for a Google and stackoverflow and anywhere else, as well as tried to use scripts given in forums and write my own ones, but nothing worked for me. I am completely stuck.
So, all I try to do is to write a script that will delete checked rows from MySQL table. Here is my HTML written inside of a PHP file:
<tr class="noP">
<td class="check"><input class="checkbox" name="checkbox[]" type="checkbox" value="'.$row["PID"].'"/></td>
<td class="id">'.$row['PID'].'</th>
<td>'.$row["name"].'</th>
<td>'.$row["surname"].'</th>
<td>'.$row["pcode"].'</th>
<td class="address">'.$row["address"].'</th>
<td class="email">'.$row["email"].'</th>
<td>'.$row["phone"].'</th>
<td class="education">'.$row["education"].'</th>
<td class="remarks">'.$row["remarks"].'</th>
</tr>
for here $row = mysql_fetch_assoc($qParts);, so this array is just a collector of field values from MySQL DB.
Basically, all I try to do is just a table with all the participants listed with ability to delete selected ones.
I would highly appreciate any help provided. Thank you!
This should help you:
foreach($_REQUEST['checkbox'] as $val)
$delIds = intval($val);
$delSql = implode($delIds, ",");
mysql_query("DELETE FROM table WHERE PID IN ($delSql)");
So, that takes your input array from $_GET/$_POST, sanitises it (a little), then implodes it to get a list of IDs (e.g. 5, 7, 9, 13). It then feeds that into an SQL statement, matching on those IDs using the IN operator.
Note that you should do this using prepared statements or similar. It's been a while though, so I can't write them off-hand, but this should give you the gist of it.
To do this using PDO, have a look here. It's a bit more complex, since you need to dynamically create the placeholders, but it should then work the same.
Reference - frequently asked questions about PDO
I think I can help you out. I had the same issue during my semester project. The problem can be solved using HTML and PHP alone.
I am assuming that PID is the primary key in your table. The trick here is to put the entire table in a form so that it looks like this:
<form action="/*NAME OF THIS PAGE HERE*/.php" method="POST">
<?php
if(isset($_POST['delete'])) //THE NAME OF THE BUTTON IS delete.
{
foreach ($_POST["checkbox"] as $id){
$de1 = "DELETE FROM //table-name WHERE PID='$id'";
if(mysqli_query($conn, $de1))
echo "<b>Deletion Successful. </b>";
else
echo "ERROR: Could not execute";
}
}
if(isset($_POST['delete'])){
echo"<b>Please make a selection to complete this operation.</b>";
}
?>
</br>
<!-- below you will see that I had placed an image as the delete button and stated its styles -->
<button type="submit" name="delete" value="delete" style="float:right;border:none; background:none;">
<img style="width:55px; height:55px;margin-left:10px; margin-bottom:6px; float:left;" src="images/del.png">
</button>
<table>
<thead>
<tr>
<th>#</th>
<th>PID</th>
<th>NAME</th>
<th>SURNAME</th>
<!--REST OF THE TABLE HEADINGS HERE -->
</tr>
<?php $fqn="SELECT * FROM //table-name here;
$fqn_run=mysqli_query($conn,$fqn);
while($row=mysqli_fetch_array($fqn_run)):?>
</thead>
<tbody>
<tr class="noP">
<td class="check"><input class="checkbox" name="checkbox[]" type="checkbox" value="<?php echo $row["PID"];?>"></td>
<td><?php echo $row['PID'];?></td>
<td><?php echo $row["name"];?></td>
<td><?php echo $row["surname"];?></td>
<!-- REST OF THE ROWS HERE -->
</tr><?php endwhile;?>
</tbody>
</table>
</div>
</div>
</form>
Hope this helps you.