php pdo code to create rows dynamically from user input? - php

column name = receipts (txtbox)
if user enter receipts = 5 then insert 5 rows in database.
data insert in table like this..
receipts
1
2
3
4
5
if user enter again receipts = 3 then again insert 3 rows in database.
6
7
8
like wise....
plz suggest me how to do this....
below is my code to save in php pdo...
i tried below code this is working but not inserting no in sequential order....
if(isset($_POST['save']))
{
$book_no = $_POST['book_no'];
$receipt= $_POST['receipt'];
for($row=1;$row<=$receipts ;$row++)
{
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$receipt));
}
}

First you have to change the following code:
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$receipt));
like this (the receipt number is the $row var, not the $receipt var)
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$row));
If you want that, the next time, it start from the last number inserted (that is, for example, start from 6, if the previous time you have inserted 5 receipts), you have to query the DB to get the the current max value, and then start from it. To get the max, a query like this should work:
SELECT MAX(receipt) FROM scheme_master where book_no = :book_no

Related

Subtract a value from a field on database

i have a problem where i want so subtract 1 value from a field on my table when i add a regestry.
I have 2 tables
Livros
CodLivro
Nome
Total
and
Vendidos
CodVenda
CodLivro
Nome
Lets imagine the total is 20, when i sell a book i want to remove one value from that field.
the current code is
case "vender_l":
{
$stmt = $conn->prepare("INSERT INTO Vendidos (CodLivro, Nome) VALUES (:CodLivro, :Nome)");
$stmt->bindParam(':CodLivro', $CodLivro);
$stmt->bindParam(':Nome', $Nome);
break;
}
Use this code, it should work.
UPDATE Livros SET Total = Total-1 WHERE Livros.CodLivro = :CodLivro;
I think the comment from Masivuye Cokile is right...
Let's put the names of your tables and columns in the example:
update Livros
set total = total - 1
where codLivro = 10 -- 10 is an example of a book id
and total > 0 -- prevents the number of books from being negative

php mysql count() returns 1 ! no counting in DB

I did search for answer before I posted :) !
Now my problem is php mysql script have to count records in mysql database cells I will try to draw my db now
table name: tableurl (table contains two columns url and clicks)
----------------
url clicks
clhgfghfh 6
hgjhgjhgh 0
kjhgjhgjh 0
khgjhgjhg 1
asdasddsg 5
-----------------
Now I want to count all the clicks so output should be 12 ! here is my code
<?php
/* db connection included in head*/
$sql ="SELECT count(clicks) FROM tableurl";
if ($result=mysqli_query($con,$sql))
{
$rowcount=mysqli_num_rows($result);
printf("Clicks together %d \n",$rowcount);
mysqli_free_result($result);
}
?>
P.S. clicks in db is defined as INT
lenght/value is 1
default is NULL
Any input what I'm doing wrong ?
You want to sum the clicks and not count the number of records in your table. Use SUM()
SELECT sum(clicks) as click_sum
FROM tableurl
and then use something like
if($row = $result->fetch_assoc()) {
$sum = $row["click_sum"];
...
}
Try with -
$sql ="SELECT SUM(clicks) as totalClicks FROM tableurl";
if ($result=mysqli_query($con,$sql))
{
$res = mysqli_fetch_array($result);
printf("Clicks together %d \n",$res['totalClicks']);
mysqli_free_result($result);
}

how to get result from array?

I have an array as below :
Array
(
[VA-ecoclsqty2_0_R79_31_9_1Room1]=>2",
[VA-preco2-0_R79_31_9_1Room1]=>50.00",
[VA-amt_preco2-0_R79_31_9_1Room1]=>25.00",
[VA-busclsqty4_0_R79_31_9_1Room1]=>2",
[VA-prbus4-0_R79_31_9_1Room1]=>36.00",
[VA-amt_prbus4-0_R79_31_9_1Room1]=>18.00",
[VA-busclsqty5_1_R79_31_9_1Room2]=>1",
[VA-prbus5-1_R79_31_9_1Room2]=>17.00",
[VA-amt_prbus5-1_R79_31_9_1Room2]=>17.00",
[VA-ecoclsqty6_1_R79_31_9_1Room2]=>3",
[VA-preco6-1_R79_31_9_1Room2]=>28.00",
[VA-amt_preco6-1_R79_31_9_1Room2]=>14.00",
)
I want to insert above data as below rule...
insert data 8 (values as 2+2+1+3) rows where
2 row insert on VA-ecoclsqty.... - amount = 25.00, roomcode = R79_31_9_1, unique_id = 1 for Room1
2 row insert on VA-busclsqty.... - amount = 18.00, roomcode = R79_31_9_1, unique_id = 2 for Room1
1 row insert on VA-busclsqty.... - amount = 17.00, roomcode = R79_31_9_1, unique_id = 3 for Room2
3 row insert on VA-ecoclsqty.... - amount = 14.00, roomcode = R79_31_9_1, unique_id = 4 for Room2
and more for different rooms for multiple rows.
How can i insert it?
And please remember that every key is different from each other.
Please reply soon and try to help.
To use ajax for the form I'd look at jQuery ajax function. To process the form and perform a search in you DB you'll have to setup a URL for the form to post to that does the processing and returns the correct response.

php adding amount in rows

I want to add the rows that is being query in database. There is an amount paid, previous balance, new balance.. I want the previous balance to be added by paid amount.
Num|pay|old|new
1 |100|500|600
2 |120|600|720
3 |200|720|920
4 |300|720|920
5 |350|720|920
6 |500|720|920
The database query has data of amountPaid (pay), previous balance (old), new balance (new), but the data in column(new) is wrong.I want to correct it in forloop.Is there a way to help me in this? to get the column(new) value is
column(pay)100 + column(old)500 = column(new)600.
the column(new) in number two will be put in column(old)600 then added by column(pay)120 is equal to column(new)720 and so on.
Hope you can help me in this...
I suppose you are adding the amounts when user submits it
1) Use mysql UPDATE clause in order to change the value in the coloumns
2) use while loop to get the previous value selecting it with a specific id which I suppose you have in your table
3) add it with a new value and update the new amount coloumn
PHP
<?php
$id=$_POST['id'];
$amount=$_POST['amount'];
include("connection.php");
$sql="SELECT * FROM `tablename` WHERE `user_id` ='$id'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res){
$t2 = $row['previous_amount'];
$t3=$t2+$amount;
$sql="UPDATE `bank`.`tablename` SET `new_amount' = '$t3'";
}
?>

how to fetch data from database dynamically for multiple user in PHP and MySQL

This question is bit complex atleast for me. Well, I am working on a project and need some bit more help..
Actually i have a module , where when 1 user login he get limited data from 1 table and he update its records. That is done !
second thing, the issue : i have 5 user who will login and will work on data.
but no user should get same data, like we have 1000 records.
Then when 1st user login: he get 10 records from 1 - 10.
2nd user login : he get next 10 ; 11- 20.
same so on..
and when next day they login ; they get records from 51.
because 5 user last day worked on first 50 data records.
My issue is how to achieve that 2 goals ?
do i need a framework for this ?
or it can be done using simple php n sql ?
any support will be helpful for me. :)
Ok. This is just a raw answer to give you a better idea. This is how you will insert the login .
Consider having a table containing following fields,
Table Name: Temp_Table
user, assigned_rows_last_no, date_assigned
<?php
$con=mysqli_connect("example.com","hiren","abc123","my_db");
// These things can be included into a single file say config.php and including in every file so that you dont need to specify connection string everytime.
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//This query required to be included into the file, which is exactly after login.php
$sql = mysqli_query($con,"SELECT assigned_rows_last_no FROM Temp_Table ORDER BY assigned_rows_last_no DESC LIMIT 1");
// This is because I want the last row number assigned to the user.
IF ($sql == 0) // Check whether the return answer is NULL or not.
{
// If the result is empty than add new entry of the user with defined row number.
// Suppose that current username can be retrieved from SESSION and stored into a variable.
$insertquery = mysqli_query($con, "INSERT INTO Temp_Table Values ('" . $username . $"', 10, CURDATE()");
mysqli_close($con);
}
else
{
// Select the last entry of row and add 10 to it. Ex. User2 has been assigned to 11-20, table contains 20 in the row of user2, now user3 comes, this query will select the row_no, add 10 and insert again into the table (i.e. value 30 which means 21-30.)
settype($sql, "int");
$sql = $sql + 10;
$insertquery = mysqli_query($con, "INSERT INTO Temp_Table Values ('" . $username . $"', '" . $sql . "', CURDATE()");
mysqli_close($con);
}
mysqli_close($con);
?>
The field Date will help you to recognize the entries of today, so that you can set your logic for "There should be no duplicate entries for the same user on same day"
Now, Make you own page, which check the above mentioned things, and assign the rows to the users.
Note: This code will only be able to clear out the logic for you, I am not sure whether it will work in your code without any changes.
You don't need a extra framework. Simply done with php 'n' sql!
Why you don't save the last edited lines (linenumbers) in a extra SQL-Table? Maybe with the username / userid.

Categories