So, I've helped a friend with a ticket system for a charity event. On this page users can select the amount of tickets they want and then click on "register". There are no payments involved, they can just register tickets. There are two types of tickets: normal and vip tickets.
I've created three databases: customers, tickets and customer_tickets. Below are the dumps of these tables.
Customers:
CREATE TABLE `customers` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`surname` varchar(2056) NOT NULL,
`lastname` varchar(2056) NOT NULL,
`email` varchar(2056) NOT NULL,
`street` varchar(2056) NOT NULL,
`house` int(255) NOT NULL,
`postal` varchar(2056) NOT NULL,
`city` varchar(2056) NOT NULL,
`desired_vip_tickets` int(1) NOT NULL DEFAULT '0',
`desired_normal_tickets` int(1) NOT NULL DEFAULT '0',
`order_id` varchar(2056) NOT NULL,
`status` varchar(2056) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=36 ;
Customer_tickets:
CREATE TABLE `customer_tickets` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`order_id` int(255) NOT NULL,
`ticket_id` int(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Tickets:
CREATE TABLE `tickets` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`ticket_id` varchar(2056) NOT NULL,
`ticket_type` int(255) NOT NULL,
`bought` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
When a user clicks register, the code below is executed. However, only the first query is executed and none of the others. Additionally I'm worried if all these queries make the server suffer...
$sql = "UPDATE customers SET `status`='$status' WHERE `order_id`='$order_id'";
$qry = mysql_query($sql) or die (mysql_error());
$sql = "SELECT `desired_vip_tickets`, `desired_normal_tickets` FROM customers WHERE `order_id`='$order_id'";
$qry = mysql_query($sql) or die (mysql_error());
$array = mysql_fetch_array($qry);
$vip = $array[0];
$normal = $array[1];
$sql = "SELECT `ticket_id` FROM tickets WHERE `ticket_type`='1' AND `bought`='0' LIMIT ".$vip;
$qry = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_assoc($qry)) {
$ticket_id = $row['ticket_id'];
$ins = "INSERT INTO customer_tickets (`order_id`, `ticket_id`) VALUES ('$order_id', '$ticket_id)";
$query = mysql_query($ins) or die (mysql_error());
$upd = "UPDATE tickets SET `bought`='1' WHERE `ticket_id`='$ticket_id";
$query = mysql_query($upd) or die (mysql_error());
}
$sql = "SELECT ticket_id FROM tickets WHERE `ticket_type`='0' AND `bought`='0' LIMIT ".$normal;
$qry = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_assoc($qry)) {
$ticket_id = $row['ticket_id'];
$ins = "INSERT INTO customer_tickets (`order_id`, `ticket_id`) VALUES ('$order_id', '$ticket_id)";
$query = mysql_query($ins) or die (mysql_error());
$upd = "UPDATE tickets SET `bought`='1' WHERE `ticket_id`='$ticket_id";
$query = mysql_query($upd) or die (mysql_error());
}
You'll need to SQL join the different tables. Your sql would have to look something like this:
SELECT * FROM table1.customer_name, table2.amount_spent, recentpurchases
WHERE table2.amount_spent = recentpurchases.spendamount
I have used example tables for this anser. They are table1, table2 and recentpurchases. Apologies if this was not a helpful answer.
Related
I am trying to delete the contents from 3 tables which are associated with a certain User ID and I get the following error:
Error in query: Cannot delete or update a parent row: a foreign key constraint fails (`MyName_4.2c`.`tbl_reservation`, CONSTRAINT `tbl_reservation_ibfk_2` FOREIGN KEY (`propertyId`) REFERENCES `tbl_property` (`propertyId`))
Code:
<?php
session_start();
$userId = $_GET['userId'];
require_once('databaseConn.php');
$query = "DELETE FROM tbl_reservation WHERE userId = '$userId'";
$result = mysqli_query($connection, $query)
or die("Error in query: ". mysqli_error($connection));
$query2 = "DELETE FROM tbl_property WHERE userId = '$userId'";
$result2 = mysqli_query($connection, $query2)
or die("Error in query: ". mysqli_error($connection));
$query3 = "DELETE FROM tbl_users WHERE userId = '$userId'";
$result3 = mysqli_query($connection, $query3)
or die("Error in query: ". mysqli_error($connection));
header('Location: index.php');
?>
My Tables:
CREATE TABLE `tbl_property` (
`propertyId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`capacity` int(11) NOT NULL,
`pricePerNight` double NOT NULL,
`locationId` int(11) NOT NULL,
`image` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `tbl_reservation` (
`reservationId` int(11) NOT NULL,
`propertyId` int(11) NOT NULL,
`date_from` date NOT NULL,
`date_to` date NOT NULL,
`amountPaid` double NOT NULL,
`userId` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `tbl_users` (
`userId` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`surname` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `tbl_location` (
`locationId` int(11) NOT NULL,
`location` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I am trying to build a friend system in php I have the tables, database and the logic in place. I am having trouble getting the friend request receiver's id.
I have registeredusers friends updates table. The registeredusers table looks like this,
CREATE TABLE `registeredusers` (
`id` int(11) NOT NULL,
`FirstName` varchar(50) NOT NULL,
`LastName` varchar(50) NOT NULL,
`UserName` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`Password` varchar(255) NOT NULL,
`ResetPassword` int(7) DEFAULT NULL,
`friends` int(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
friends
CREATE TABLE `friends` (
`friend_one` int(11) NOT NULL,
`friend_two` int(11) NOT NULL,
`status` enum('0','1','2') DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The requester's ID would be INSERTED into friend_two and receiver's ID would get into friend_one. here's my code
<?php
include 'dbh.php';
$sql = "SELECT * FROM registeredusers";
$result = mysqli_query($connection,$sql);
$row = mysqli_fetch_assoc($result);
$username = $row['UserName'];
$requesterU = $_GET['user'];
echo "the requester is ".$requesterU;
while($row=mysqli_fetch_array($result)){
$id = $row[0];
$username = $row[1];
echo "
<form action='list of users.php'>
$id $username<input type='submit' value='send request' name='friendsbanalo'></input></form>";
}
$sql = "SELECT * FROM registeredusers WHERE UserName = '$requesterU'";
$result = mysqli_query($connection,$sql);
$row = mysqli_fetch_assoc($result);
$requester_id = $row['id'];
echo "requester's id ".$requester_id;
if(isset($_POST['friendsbanalo'])){
$sql = "INSERT INTO friends (friend_one,friend_two) VALUES('$requester_id','$reciver_userid')";
$result = mysqli_query($connection, $sql);
}else{
echo "error";
}
?>
I am not able to get the receiver's ID, can anyone tell me how can I get receiver's ID? I tried searching for the solution and the answers were too complicated for me to understand. I tried (on a separate file) INNER JOIN but I couldn't get it to work.
I have two tables. One which holds the user status content and the other holding the users. How do I write the query of the users to obtain the user record for their given streamitem in streamdata? At current times, its only grabbing the user that is at the top of the user table.
$check = "SELECT streamitem_id, streamitem_timestamp, streamitem_content FROM streamdata WHERE streamitem_creator="$user1_id." AND streamitem_id=".$last." AND streamitem_type_id=1 ORDER BY streamitem_timestamp DESC";
$check1 = mysqli_query($mysqli,$check);
$resultArr = mysqli_fetch_array($check1);
$json['streamitem_id'] = $resultArr['streamitem_id'];
$json['streamitem_timestamp'] = Agotime($resultArr['streamitem_timestamp']);
$json['streamitem_content'] = $resultArr['streamitem_content'];
mysqli_free_result($check1);
$check = "SELECT * FROM users";
$check1 = mysqli_query($mysqli,$check);
$resultArr = mysqli_fetch_array($check1);
$json['username'] = $resultArr['username'];
$json['id'] = $resultArr['id'];
$json['first'] = $resultArr['first'];
$json['middle'] = $resultArr['middle'];
$json['last'] = $resultArr['last'];
mysqli_free_result($check1);
-- Table structure for table `streamdata`
--
CREATE TABLE IF NOT EXISTS `streamdata` (
`streamitem_id` int(11) NOT NULL auto_increment,
`streamitem_type_id` int(11) NOT NULL,
`streamitem_creator` int(11) NOT NULL,
`streamitem_target` int(11) NOT NULL,
`streamitem_timestamp` datetime NOT NULL,
`streamitem_content` varchar(5000) NOT NULL,
PRIMARY KEY (`str
eamitem_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1953 ;
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`first` varchar(64) NOT NULL,
`middle` varchar(64) NOT NULL,
`last` varchar(64) NOT NULL,
`username` varchar(64) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=150 ;
Query to get user details and stream details for a given stream item:
$check = "
SELECT * FROM users u
INNER JOIN streamdata s ON
(s.streamitem_creator = u.id AND s.streamitem_id = {$last})";
Please note that it is bad practice to insert raw variable values into your queries. Especially if they come from user input. Look into mysqli_real_escape_string or try using prepared statements.
I am developing a cooking recipe-website and i want to create a recipe finder based on the used incredients.
My current finder only works with 3 ingredients right.
The Finder should return the right recipe(s) based on the used incredients (should work with 1-n*)
My Tables:
CREATE TABLE IF NOT EXISTS `INGREDIENTS` (
`ingredients_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
CREATE TABLE IF NOT EXISTS `INGREDIENTS_POS` (
`ingredients_pos_id` int(11) NOT NULL AUTO_INCREMENT,
`ingredients_id` int(11) NOT NULL,
`ingredients_unit` varchar(20) NOT NULL,
PRIMARY KEY (`ingredients_pos_id`),
KEY `ingredients_detail_fk` (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
CREATE TABLE IF NOT EXISTS `RECIPES` (
`recipes_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) COLLATE utf8_bin NOT NULL,
`text` varchar(2000) COLLATE utf8_bin NOT NULL,
`count_persons` int(11) NOT NULL,
`duration` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`date` datetime NOT NULL,
`accepted` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`recipes_id`),
KEY `recipes_user_fk` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=88 ;
CREATE TABLE IF NOT EXISTS `RECIPES_POS` (
`recipes_pos_id` int(11) NOT NULL AUTO_INCREMENT,
`recipes_id` int(11) NOT NULL,
`ingredients_id` int(11) NOT NULL,
`ingredients_value` int(11) NOT NULL,
PRIMARY KEY (`recipes_pos_id`),
KEY `recipe_pos_rec_id` (`recipes_id`),
KEY `recipes_pos_ingredient_fk` (`ingredients_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=58 ;
My buggy Solution (doesn't support count from 1-n):
<?php
include 'db_connect.php';
$q = urldecode(mysql_real_escape_string($_GET['q']));
$parameter = explode ('$',$q);
$var = 0;
//print_r($parameter);
foreach($parameter as $ing)
{
//echo $ing;
$sql = "SELECT ingredients_id FROM INGREDIENTS WHERE name='".$ing."'";
$result = mysql_query($sql,$db) or exit('{"Data":null,"Message":null,"Code":500}');
$row = mysql_fetch_array($result);
$arr_id[$var] = $row['ingredients_id'];
$var++;
}
//print_r($arr_id);
$sql = "SELECT r.recipes_id FROM RECIPES r, RECIPES_POS rp WHERE r.recipes_id = rp.recipes_id ";
foreach($arr_id as $id)
{
$sql .= "AND rp.ingredients_id =".$id . " ";
}
//echo $sql;
$result = mysql_query($sql,$db) or exit('{"Data":null,"Message":null,"Code":500}');
mysql_close($db);
$rec;
while($row = mysql_fetch_array($result))
{
//echo "test";
$_GET['id'] = $row['recipes_id'];
$rec= include('get_recipe_byID.php');
}
//print_r(mysql_fetch_array($result));
if (count($arr_id) == 0)
{
echo '{"Data":null,"Message":null,"Code":404}';
die();
}
?>
I need a better solution for that chase.
Maybe SQL itself will help me to find the right recipes
thx
That query helped me a lot:
select r.recipes_id
from RECIPES r
inner join RECIPES_POS rp on r.recipes_id = rp.recipes_id
where rp.ingredients_id in (4, 6)
group by r.recipes_id
having count(distinct rp.ingredients_id) = 2
If I register a user using this table:
CREATE TABLE IF NOT EXISTS `users`
(
`id` INT(11) NOT NULL AUTO_INCREMENT,
`md5_id` VARCHAR(200) NOT NULL,
`full_name` TINYTEXT CHARACTER SET latin1 COLLATE latin1_general_ci
NOT NULL,
`user_name` VARCHAR(10) NOT NULL,
`user_email` VARCHAR(30) NOT NULL,
`user_level` TINYINT(4) NOT NULL DEFAULT '1',
`pwd` VARCHAR(220) NOT NULL,
`nationality` VARCHAR(30) NOT NULL,
`department` VARCHAR(20) NOT NULL,
`birthday` DATE NOT NULL,
`date` DATE NOT NULL DEFAULT '0000-00-00',
`users_ip` VARCHAR(200) NOT NULL,
`activation_code` INT(10) NOT NULL DEFAULT '0',
`banned` INT(1) NOT NULL,
`ckey` VARCHAR(200) NOT NULL,
`ctime` VARCHAR(220) NOT NULL,
`approved` INT(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
)
ENGINE=INNODB
DEFAULT CHARSET=latin1
AUTO_INCREMENT=3;
and then once logged in to 'myaccount.php' use this code to enter values into another table, the language table:
if (empty($_SESSION['$user_id'])) { // user not logged in; redirect to somewhere else }
if (!empty($_POST['doLanguage']) && $_POST['doLanguage'] == 'Submit') {
$result = mysql_query("SELECT `id` FROM users WHERE `banned` = '0' order by id desc");
list($id) = mysql_fetch_row($result);
session_start();
$_SESSION['user_id'] = $id;
foreach ($_POST as $key => $value) if (empty($err)) {
for ($i = 0;$i < count($_POST["other"]);$i++) {
$native = mysql_real_escape_string($_POST['native'][$i]);
$other = mysql_real_escape_string($_POST['other'][$i]);
$other_list = mysql_real_escape_string($_POST['other_list'][$i]);
$other_read = mysql_real_escape_string($_POST['other_read'][$i]);
$other_spokint = mysql_real_escape_string($_POST['other_spokint'][$i]);
$other_spokprod = mysql_real_escape_string($_POST['other_spokprod'][$i]);
$other_writ = mysql_real_escape_string($_POST['other_writ'][$i]);
$sql_insert = "INSERT into `language`
(`user_id`,`native`,`other`,`other_list`,`other_read`, `other_spokint`
,`other_spokprod`,`other_writ` )
VALUES
('$id','$native','$other','$other_list','$other_read','$other_spokint',
'$other_spokprod','$other_writ') ";
mysql_query($sql_insert, $link) or die("Insertion Failed:" . mysql_error());
}
header("Location: myaccount.php?id=' . $_SESSION[user_id] .'");
exit();
}
}
}
All is fine until , for example I register id=3 (in users table) and then log back into id=1 and change their details in the language table, then their user_id in the language table (which is foreign key to id in users table) is 3 when it should be 1. To make things simple, the id in users table should be same as the user_id in the language table. But when going back and changing data in the languages table the user_id stays the same as the last id that registered!
Please help!
This query you have:
$result = mysql_query("SELECT `id` FROM users WHERE `banned` = '0' order by id desc");
What is the purpose of it? You are assigning to $id the first value it finds, yet the query doesn't look for user name or anything else. You probably want to user $_SESSION['$user_id'] instead of $id as your user's ID.