Print specified page, update DB MYSQL and PHP - php

I have the following code:
<iframe onscroll ="caise4.php" name="frame4"></iframe>
<input type= "submit" onclick ="frames['frame4'].print()" value = "WATER/ELECTRICITY" id = "id4"></a>
This would allow me to print a different page when the button/frame is clicked.
And i have the following PHP code:
<?php
if(isset($_POST['frame4'])){
$SQL_1 = "INSERT INTO `tick4` (`ticket_id`, `ticket_date`) VALUES (NULL, CURRENT_TIMESTAMP)";
$result_1 = mysqli_query($db, $SQL_1);
$SQL_2 = "UPDATE v_attente_service AS vas
JOIN (SELECT COUNT(ticket_id) AS cnt FROM tick4) AS ti
SET vas.NOMBATTE = ti.cnt
WHERE vas.CODESERV=4";
$result_2 = mysqli_query($db, $SQL_2);
}
?>
When the user clicks, a new row needs to be inserted in the table "tick4". Then "NOMBATTE" in "v_attente_service" is updated.
Basically, when the user prints a ticket, the number of tickets is updated, and the number of people waiting in the queue is also updated.
The problem I have: I get redirected to the page preview, but nothing is displayed there. Also, nothing gets updated in my tables.
I am using phpMyAdmin by the way.
Any help would be greatly appreciated. Thank you.

Related

Button to post value from fetched database data

I'm a beginner in PHP and PDO.
I'm trying to fetch database data from a database table, display it on a list and add a button that when clicked it adds the itemid value to another table with the name_id of the user.
The issue is even though the list is displaying the items in order and correctly,the button is not working 100%, when pressed on any item from the list it always posts the first itemid value from the database column.
Any help is appreciated.
echo ''.$itemid.'';
echo'<form action="" method="post"><button name="get" type="submit">CLICK ME</button></form>' ;
if(isset($_POST['get'])){
$query = "update table set fetched_item_id='".$row['item_id']."' where name_id='".$_SESSION['id']."' ";
if($query)
{
$query = $conn->prepare($query);
$query->bindParam('1', $itemid);
$query->execute();
echo "Success!";
exit;
}
I tried everything I could according to my knowledge.

How to submit the values of form before update and after update

I have a form that retrieves data from a database and displays it in respective fields. The user then has the ability to make changes to the form and update those values. I'm able to correctly display the data from the database(SELECT) by clicking a submit button and update it as well(UPDATE) by clicking another submit button. What I want to do is store all the data before update in a separate table, sort of as an audit log that will tell me what the previous fields were and what the new update is like. I want to have both the Update and Insert query on the same submit button as well. How do I go about inserting the 'former' values in my table.
Here's my working update statement:-
if(isset($_POST['submit'])){
// Updating the dealer info using sfid
$sql = "UPDATE tbl_dealer_info ";
$sql .= "SET phone = '".$phone."', email = '".$email."', SFID = '".$sfid."', account_name = '".$account_name."', parent_account = '".$parent_account."', awi_code = '".$awi_code."', sales_portal_id = '".$sales_portal_id."', iae = '".$iae."', rsm_val = '".$rsm_val."', door_type_val = '".$door_type_val."', payment_method_val = '".$payment_method_val."', region_val = '".$region_val."', street_address = '".$street_address."', city = '".$city."', state = '".$state."', zip = '".$zip."', area = '".$area."', market = '".$market."', boost_app = '".$boost_app."', virgin_app = '".$virgin_app."', virgin_mob_app = '".$virgin_mob_app."', start_date = '".$start_date."', bank_name = '".$bank_name."', bank_acc_number = '".$bank_acc_number."', routing_number = '".$routing_number."' WHERE SFID = '".$sfid."' ";
$sql .= "LIMIT 1";
$result = mysqli_query($conn, $sql);
One easy way to do this is using hidden values example
<form>
<input type="text" name="phoneNumberNew">
<input type="hidden" name="phoneNumberOld">
</form>
With this what you can do is submit the form to wherever your table is held and populate the table where the textbox is the new data and the hidden one will be your old data. From there you can submit it onward using more hidden values or whatever means you see fit. This is just one option for you based upon technology everyone has available! :)
When the user submits, the form data will be sent to the action="" page you defined.
In that page, run the select query again, which will give you the current values.
Then run the update query.
In your log you can then output the current values (from the select you did before), and the new values (from the update fields you got from the form).
--- Added this after the OP`s comment:
Let`s say you have a page like profile.php to update a user.
User A goes to profile.php?username=A
User B goes to profile.php?username=B
you can then build your select and update queries and specify which user you want to update.
Another method, which is better and more secure is to setup a session cookie. You then read that cookie to know which session, and hence which user is using the page.
Obviously you want something to ensure the person modifying users is allowed to do so, but that is outside your question (users - roles - security management).
There will then be no chance of mixing up users.

Fetching rows of data from a table that isn't in another table

It's been long that I've been encountering this problem and I'll be glad if you guys can help me out on what to do.
This is the illustration.
Take for instance, you have a table called matrics and the table has a column called 'matric_num'.
Now, you create a registration page for users where one of the field is to select any matric num and after the user might have selected a matric number, filled the rest of the form and click on the submit button, it should insert the details into database.
If another user want to register, the matric number that the previous user selected should not be listed in the option menu.
HERE is what I did.
$query = mysqli_query($con, "SELECT *FROM `users`");
$rows = mysqli_num_rows($query);
echo "<select>";
for($i=1;$i<=$rows;$i++)
{
$fetch_user_matric = mysqli_fetch_array($query);
$matric_num = $fetch_user_matric['matric_num'];
$matric_query = mysqli_query($con, "SELECT *FROM `matrics` WHERE `matric_no` <> '$matric_num'");
$matric_rows = mysqli_num_rows($matric_query);
for($j=1;$j<=$matric_rows;$j++)
{
$fetch_matric_num = mysqli_fetch_array($matric_query);
$matric_no = $fetch_matric_num['matric_no'];
echo "<option value='$matric_no'>$matric_no</option>";
}
}
echo "</select>";
It displayed only the first matric number from the database whereas all matric numbers that hasn't been used by any user should be displayed.
Please what do you think is wrong with the sql query.
THANKS.
Just try this query:
SELECT * FROM `matrics` WHERE `matric_no` not in (select `matric_num` from `users`)

How to update record got from session?

I dont know what to do with this. I want to update a record from database. I have header were there, the fullname of the user is displayed once login, and has also a dropdown. One of this dropdown is Account Setting were a user can update his Information. But I dont know what to do since Its my first time working with php. below is my code in displaying the record.
<?php
$session_useraccount_id=$_SESSION['useraccount_id'];
$qry= "SELECT * FROM tblusersaccount where useraccount_id = '$session_useraccount_id'";
$result=mysql_query($qry) or die(mysql_error());
{
$useraccount_id=mysql_result($result,$i,'useraccount_id');
$fname=mysql_result($result,$i,'fname');
$lname=mysql_result($result,$i,'lname');
$email=mysql_result($result,$i,'email');
$password=mysql_result($result,$i,'password');
$useraccountname=mysql_result($result,$i,'useraccountname');
?>
<?php echo "<a style='color: white' href='updateusers.php?update = $useraccount_id'>$fname $lname &nbsp&nbsp";?>
It gives me the id, and I can display it to other page.
$qry= "SELECT useraccount_id, fname, lname, useraccountname, email, password FROM tblusersaccount";
$result=mysql_query($qry) or die(mysql_error());
{
$useraccount_id=mysql_result($result,$i,'useraccount_id');
$fname=mysql_result($result,$i,'fname');
$lname=mysql_result($result,$i,'lname');
$useraccountname=mysql_result($result,$i,'useraccountname');
$password=mysql_result($result,$i,'password');
}
and below I put this
First Name" required>
and so on.
How can I update my record??
Question not clear, by the way you can provide a button that onCLick, once you collect the account data, executes an update query on server side.

Get unique id of row just inserted into database

I have a form that submits super basic information, just a new name (the name gets assigned a unique id). What I am trying to make happen is when the user submits the name it gets submitted into the database and they get redirected to a new page card.php where they can add more specific information. However the unique id associated with the row that was just submitted needs to follow in the URL (id=$id)
$query = "INSERT INTO name VALUES(NULL, '$name')";
mysqli_query($conn, $query);
$query2 = "SELECT * FROM name ORDER BY id DESC LIMIT 1";
$result = mysqli_query($conn, $query2);
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
}
header("Location: http://localhost/card.php?id=$id");
Process
addName.php -> user submits new name -> add to database -> redirect to card.php WITH unique id value of name that is just submitted
1.) Is there a way to retain the id of the row just submitted? In the fluke chance 2 or more people submit at the same time the second query of getting the last row inserted into the database will return the wrong row id
2). Having a while loop return 1 piece of info seems like a shitty way to do things, this might be the most basic of shit but i cant seem to return the 1 piece of data without doing this
Try using mysqli_insert_id which returns the AUTO_INCREMENT ID generated from the previous INSERT operation.
$query = "INSERT INTO name VALUES(NULL, '$name')";
mysqli_query($conn, $query);
$id = mysqli_insert_id();
header("Location: http://localhost/card.php?id=$id");
Reference: http://us1.php.net/manual/en/function.mysql-insert-id.php
You could use the mysqli function to retrieve the last inserted id (check mysqli_insert_id as suggested in a comment), but I wouldn't recommend you using the user id in the URL, people can try to get in, using other ids
also you can use the php function to generate a custom is check out: uniqid

Categories