I am a beginner when it comes to PHP and I'm having some issues getting a data entry form to work right. I'm building a site for a local high school wrestling team and they want to hold an online raffle. I have that up and running fine, but they want an online form they can add someone, with the # of tickets they purchased, so they can sell tickets offline as well.
I have the form set up and it adds the new person correctly to the database, but I can't get it to add the tickets at all. It needs to be able to add anywhere from 1 ticket and up, and have each ticket be it's own record in the database and be associated with the correct person. I have it set up to go through it with a while loop, but it apparently isn't working and I can't figure it out.
Any help would be greatly appreciated. Below is the code, everything seems to be working up until close to the end.
I have the form set up and it adds the new person correctly to the database, but I can't get it to add the tickets at all. It needs to be able to add anywhere from 1 ticket and up, and have each ticket be it's own record in the database and be associated with the correct person. I have it set up to go through it with a while loop, but it apparently isn't working and I can't figure it out.
Any help would be greatly appreciated. Below is the code, everything seems to be working up until close to the end.
PHP:
<?php
// create short variable names
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$amount=$_POST['amount'];
if (!$fname || !$lname || !$phone || !$email || !$amount) {
echo "You have not entered all the required details.<br />"
."Please go back and try again.";
exit;
}
if (!get_magic_quotes_gpc()) {
$fname = addslashes($fname);
$lname = addslashes($lname);
$phone = addslashes($phone);
$email = doubleval($email);
}
@ $db = new mysqli('localhost', 'root', 'root', 'raffle');
if (mysqli_connect_errno()) {
echo "Error: Could not connect to database. Please try again later.";
exit;
}
$query = "insert into raf_members (email,password,first,last,phone,active,ipaddress,since,expires) values
('".$email."', 'password', '".$fname."', '".$lname."', '".$phone."', '1', 'none', '".THEDATE." at ".THETIME."', 'none')";
$result_member = $db->query($query);
$query = "select * from raf_members where email='".$email."'";
$result = $db->query($query);
$i = $result->fetch_array($result);
$ab = 0;
while ($ab < $amount) {
$ticket_id = '';
$ticketid = md5(rand(1000000,9999999));
$abc = 0; while ($abc < 20) { $ticket_id .= $ticketid[$abc]; $abc++; }
$query = "select * from raf_tickets where ticket_id='".$ticket_id."'";
$result = $db->query($query);
$dup = $result->num_rows; if (!$dup) { $test_time = time() - 172800; GetTime();
$query = "insert into raf_tickets (ticket_id,owner_mnum,thedate,thetime,active,`time_stamp`) values ('".$ticket_id."','".$i[mnum]."','".THEDATE."','".THETIME."','1','".time()."')";
$result = $db-query($query);
if ($result) { $ab++; }
}
}
if ($result_member) {
echo $db->affected_rows." member inserted into database.";
} else {
echo "An error has occurred. The member was not added.";
}
$db->close();
?>
Last edited by a moderator: