Trying to add multiple rows with incrementing field

-1

Im trying to write some code for a raffle, when someone buys a ticket it works well but if someone buys ten tickets. i would like it to put each one on a new row, the last column is the ticket number which is got by another table called count and i want the new count in the last column of each row.

In the actual script there is more than two columns but this is an example o try to let you know what im tryin to do.

As you can see i want the ticket number to increment by one every timesomeone buys tickets. (the ticket number is in a simple table with just id and ticket number)

EXAMPLE someone buys 2 tickes

name | ticket number

John | 1

john | 2

jane | 3

jane | 4

jane | 5

This is what i have. (WORKING EXAMPLE of the code tha doesnt work.) as you can see the ticker numbr stays the same and not increment by one.

$num //is a number between 1 and 10
$tr //is the current count got from database (this needs to count up by one every entry)



include 'includes/connect.php'; 

$num = "3";  // number of tickets someone buys.
$count = "5"; // count of tickets already sold (so this is start count for this transaction).
$id = "1";  // this is the line the counter is on to keep count updatd for the amount of tickets sold.
$name = 'john'; //example name

for($i=0;$i< $num;$i++){
$count="$count+1"; // increments count by 1

$sql123 = "UPDATE count SET count=$count WHERE id='$id'"; //should update database to new count

             $sql = "INSERT INTO test (name, number)
VALUES ('$name', '$count')";

if($result = mysqli_query($con, $sql)){

echo "<br>tickets bought and entered into database,<br>Thank you<br>";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}
}

Not sure what im doing wrong?

Thank you in adavnce

Nook6

The reason the current code doesn’t work is because you are assigning the string “5+1” to $count, not the result of a math operation.

However, this method is not concurrent safe (atomic). What you need to do is run the UPDATE query to add 1 to the current field value, then get the value that was just updated and use that in the insert query. The way to do this is to use the MySql LAST_INSERT_ID(expr) function in the query, then get the last insert id value into your code. In fact, the LAST_INSERT_ID(expr) example in the MySql documentation is for generating sequence numbers.

The update query would be -

UPDATE count SET count=LAST_INSERT_ID(count+1) WHERE id = $id

(I highly recommend that you rename that table to be something other than the same name as a column in that table and don’t put quotes around numbers, the $id value, making them stings.)

After executing this query, you would use the mysqli_insert_id() function to get the value that was just incremented and updated in the count column.

Next, you should be using prepared queries, prepared once, before the start of the looping. To do this, you would want to switch to the much simpler and more consistent PDO extension, since the mysqli extension takes far too many statements to accomplish a prepared query.

Also, you should be storing the user_id, not the user name in the table holding this information and don’t unconditionally output raw database errors onto a web page, use exceptions instead and let php catch the exception where it will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.)

Hi
Sorry for sounding dumb but you lost me with that im not that good lmao not sure how to implement it into my code.
the
$count = “count+1”;
i meant to state the count is coming from a database so in my mind i was adding one then writing one row tto database then doing it all again ( so in effect collecting from database for every ros which means i was adding one each time) hope thats understandble for you its difficult to explain when its the way my brain works but possibly not the way a normal brain works lol

And to change to pdo means 2 things

  1. i would need to learn pdo
  2. i would have to rewrite the whole script lol

Sorry again.

Nook6

What for is that count table? And why don’t you just use the auto increment field of the ticket table?

Have a look at your values

$count="$count+1";
var_dump($count);

Hi
EDIT
I will add i have sorted this problem now by changing the count values of the row counter.
From This
for($i=0;$i< $num;$i++){
TO this
for($i=$ticket_number;$i< $tiket_number_plus_quantity_bought;$i++){
(changed this to try to make it understandable)

The count table is just a simple multi counter that holds the “ticket numbers” fo each raffle the reson i dont use the auto increment is because its like a store everytime i dd a new raffle it starts a row in the count table with the number of the raffle (id) and the current ticket number.
so when somebody buys tickets its gets the current ticket number adds one and thats the first ticket for the buyer if they buy more it increments it for each ticket. then writes it to another table which is where the draw will take place from.

(in my head this all makes sense).

Ty

Nook6

No, that won’t reliably work. Due to the concurrency problem already mentioned, when multiple purchases occur at the same time, this code will get and use the same starting value for each purchase and give the same ticket numbers to different people. You will end up with -
john | 5
john | 6
jane | 5
jane | 6
jane | 7

Your code must take into account what happens on real-time, multi-tasking operating systems when there is more than one request being serviced at the same time.

This really seems like less of how do I do this, and more of how do I do it the way I want.

Considering that you are just randomly drawing a number that is in the database, that number is irrelevant. And auto incrementing id gives you a number. What that number is shouldn’t matter to anyone and can easily be adopted to whatever. Think in terms of the old school tickets that would be ripped in half for a drawing; No one ever used an entire roll for a single event, but they would use the same roll for the next event.

http://sqlfiddle.com/#!9/6667e5/6/0

And if you want to find out who bought multiple tickets or in what order or anything like that you would do something like this…

SET @row_number = 0; 
SELECT 
    (@row_number:=@row_number + 1) AS num, 
    name
FROM
    Raffle_Entires
ORDER BY raffle_id, id, name;

That gives the rows a number based on your criteria.

Hi

can i please explain,
maybe it is because im a noob and dont know php as well as most of you but i do read your answers and if i understand i think about them but from my noob brain i cant always see how a reply you give me can work.

  1. auto increment in my head wont work because the ticket numbers will be all over the place.
  2. putting all sales for all raffles in a table then just picking the entries for a raffle with code in my head would mean that i cant offer a ticket number to the customer ( so they can keep an eye on thier numbers through the whole process).

Im sorry if this sounds stupid to you but i can only explain how i see it in my head and its difficult to implement something that in your head wont work, now im not sure if my two statements above are correct and they wont work for the reasons i have given or not im hoping one of you will tell me.

Also i read somewhere that you can set a table up so that it auto increments for each name:

raffleid         | ticket_number 
teddy            |  1
teddy            | 2
chocolates       | 1
tedy             | 3
chocolates       | 2

im sorry im finding this really difficult to explain so it can be understood.

And i really do appreciate all your input een if i dont understand some of it.
thank you

Why is the increment starting over so important? I still don’t understand the need for that, other than you just want it to be that way.

Hi

The only reason the increment is important to me is because when someone purchases a ticket, i want to be able to give them the ticket number. So as soon as they buy the ticket they have the number/sthat will be put into the draw. and in my mind i cant see any other way of doing that.

also i just think it would be better to start every one from tcket number 1.
I presume you could call it a personal preference

i do appologise for being a noob

Nook6

And how do you know what that number will be now? Do you keep a running tally in your head?

running tally of the current ticket number?

yes. If using what the database gives you isn’t good enough, how are you doing it currently?

im using a table with the raffle_id and ticket_number i just call that to use as the start number then update it again with the new number

So, why not just insert them and it will give the number that it used?

What i gave as a sample, when combined with a form, you could use the same form and add people to multiple raffles from the same form and even sell multiple tickets

i think we are getting back to the origonal problem i had i could not work out how to add multiple rows (if someone bought 5 tickets add 5 rows) and update thier ticket number per row (ticket number 1 to 5 for the tickets if they were the first person to purchase in that raffle)

When some people gamble, they play the odds. When the ticket number indicates how many tickets have been sold at that point for a particular drawing, indicating the probability of winning, said people will be more likely to purchase more tickets (which is what the people running the drawing want to happen.) If the ticket numbers don’t convey this type of information, there will be less tickets bought by said people.

A specific example of purchases dependent on the odds of winning. Our state publishes the prizes that have been claimed in scratch-off lotto games. Once the large prizes have all been claimed for a game, the sales drop off for that game.

So, it boils down to human expectations and getting people to feel like they have a chance at winning so that they will purchase as many tickets as possible.

The 1st reply in this thread will let you maintain a separate number sequence for each drawing and is concurrent safe for multiple purchases made at the same time.

Sponsor our Newsletter | Privacy Policy | Terms of Service