So, I have question on whether the SQL statement for inserting a customer borrowing a particular CD is valid or not.
Here's the form I want the customer to fill out.
<form action="proj3.php" method="post">
<h4>Enter regular customer information.</h4>
Customer Name: <input type="text" name="cust_name"><br>
Customer SSN: <input type="text" name="cust_ssn"> (No spaces or special characters)<br>
Customer Telephone number: <input type="text" name="cust_tel"> (No spaces or special characters) <br>
<h4>Enter existing CD information.</h4>
CD Title: <input type="text" name="cd_title"><br>
CD Year: <input type="text" name="cd_year"><br>
<h4>Enter rent information.</h4>
Start CD Rent Date: <input type="text" name="rent_date"> (Format: YYYY-MM-DD)<br>
Rent Duration (days): <input type="text" name="rent_duration"><br>
<input type="submit" name="submit" value="Submit">
</form>
Here's the E-R diagram I did:
Now, the backend database stuff I'm definitely very new and a beginner to.
I want to insert a regular customer (non- VIP) that borrows/rents a particular CD. How do I write the SQL statement for that?
Essentially, I'm very confused on what table and attributes should the information go into? I have three tables that relate to this information. CD, rent and customer. What INSERT INTO statement am I writing?
I think it may have something to do with assigning IDs but I'm not sure how to do that yet. If there are any resources or tutorials I should read, please feel free to direct them to me. I'm stuck right now and just want someone to point in toward a certain direction on what steps to take or what to do from here?
EDIT:
Here's my attempt:
$sql= "INSERT INTO CD ( CD_title, CD_type, CD_year)
VALUES (‘$_POST[title1]’, ‘$_POST[type1]’, ‘$_POST[year1]’)");
But does that take care of the renting and customer aspect?
Try to read on basic SQL stements. Then try to come up with a solution on your own, and if you get stuck everyone here will be sure to help.
The way that I envision your system working is that you would have customer data in your customer table, cd information on every cd in your inventory in the CD table, and the rent table to be the cd's that are currently checked out. Is that how you have the tables set up?
If that is roughly how you have it setup, then I would write the query like this:
insert into rent(rentID, cdID, customerID,(and any other fields you have, comma separated))
values("(auto generated, or program generated rentID)","(cdID corresponding to the one being rented)","(customerID of the customer renting)","(Whatever else you're inserting)")
The idea is that each table will have a unique id (Primary Key) for each data item, such as an ID for each customer and an ID for each cd in inventory. You really wouldn't need much else in the rent table except the corresponding id's and any rental pertanent data, such as check out time, date item is due to be returned, etc. It is like a bridge connecting the other two tables. The other tables would hold any descriptive information, such as customer name, or year a CD was made, etc., and all be linked through the unique id's.
Hope my ramblings helped. :)
Related
I realise that this may be a duplicate question but I can't seem to find the right answer.
I'm trying to insert multiple rows into a MySQL table but, at the moment, all I'm doing is inserting the same data multiple times.
The table is called 'tblRoomsBooked' and the fields are bookingNumber, roomID, roomRate and depositRate. The table has it's own unique, primary key, roomBookedID which auto-increments.
We have an HTML form for booking rooms that we hire out. There are four rooms that can be hired in any combination. Each room has its own ID, hire cost and deposit amount required. So, for example, if one room was booked with a deposit, there should be one record inserted consisting of the booking number, the room's ID, the room's cost and its deposit. If three rooms were to be hired, there should be three separate records inserted, each record would hold the room ID, room cost and deposit of each room booked but all the rooms booked at the one time would have the same booking number.
The SQL code is:
$bookingNumber = ($_POST['bookingNumber']);
$roomID = ($_POST['roomID']);
$roomRate = ($_POST['roomRate']);
$depositRate = ($_POST['depositRate']);
$RoomsBooked = "INSERT INTO tblRoomsBooked (bookingNumber, roomID, roomRate, depositRate) VALUES ('$bookingNumber', '$roomID', '$roomRate', '$depositRate')";
If I use $RoomsBooked = "INSERT INTO tblRoomsBooked (bookingNumber, roomID, roomRate, depositRate) VALUES ('$bookingNumber', '$roomID', '$roomRate', '$depositRate'), ('$bookingNumber', '$roomID', '$roomRate', '$depositRate'). ('$bookingNumber', '$roomID', '$roomRate', '$depositRate'), ('$bookingNumber', '$roomID', '$roomRate', '$depositRate')"; as I read elsewhere here, it just creates four rows of the same data, regardless of the combination of rooms selected.
The rooms are selected by checkboxes...
<input type="checkbox" class="checkboxMargins" id="meetingRoom" name="roomID" value="1" onClick="Check();">
<input type="checkbox" class="checkboxMargins" id="library" name="roomID" value="2" onClick="Check();">
<input type="checkbox" class="checkboxMargins" id="jajRoom" name="roomID" value="3" onClick="Check();">
<input type="checkbox" class="checkboxMargins" id="annex" name="roomID" value="4" onClick="Check();">
When a checkbox is selected, some JQuery scripting by JonoJames which can be found here Output Data From MYSQL... is used to fill hidden input boxes for the room costs.
I've tried using name="roomID[] but this throws an error regarding arrays and not liking the INSERT statement.
All this works perfectly for booking one room, it's just if more than one is booked that I can't make work.
I think I need to use for or foreach but the examples on here don't really show how to take the data from the form to insert it into the table.
What I need to know is, if for or foreach is the way to go, what to do and how to use it. If not, what do I need to do?
All it needed was to put a 0 into the JavaScript wherever the value of the deposit was ''
For example. In the JavaScript there are lines similar to document.forms["bookform"].room1valueDeposit.value ='';. This need's to be document.forms["bookform"].room1valueDeposit.value ='0';.
I had tried replacing the '' in the script with0 but I had replaced all '' not just those relating to deposits. This way, if a room was booked but no deposit was required, it would fill the field with a zero not an empty string and if a deposit was required, the deposit amount would be entered.
Can't believe something so small could take so long to figure out.
As you didn't share any information about the HTML code, it is really hard to understand your booking process.
Regarding your multiple inserts, you already stated the correct approach. If you send your formular to the backend, you have to send each Booking of the room to the backend. This array contains multiple objects, depending on how many books have been booked.
Every obect should contain your necesarry data, roomID, roomRate, deposite and more. As you said, yes it is possible to loop through that array and then insert every booking into the database.
The error you get, is that you insert the same data over and over again, because you do not loop or didn't correctly fetch your next booking. If you share more code, we can take a look.
So, basically I'm making a little site that has some pages with video game's info and media. I have a user account system setup, but there's something I've been wanting to know how to add. I'm still new to PHP, and I made the user account system only by following a tutorial. Anyways, here's the HTML code that shows on a game info page when a user is logged in:
<form name="wishlistAdd" action="wish_list.php">
<input type="checkbox" name="wagakkiwa_wish" id="wagakkiwa_wish" />
<label for=wagakkiwa_wish>Add <b>WAGAKKIWA</b> to your Wish List?</label><br />
<input type="submit" class="button" value="Add to List" /> This is a alpha feature. It won't work currently.
</form></div>
Basically, what I'm wanting to achieve is if the user logged in checks the box then clicks 'Add to List' it will add it to a list in their User Account Home page under the section 'Wishlist.' For example, if I added that game to my wishlist and I went back to the /user/ section of the site, in the right column under 'Wishlist' it would show 'WAGAKKIWA' as an a tag to that page. Then, if I did it for another game it would add it ontop.
So, what would I need to do to accomplish this? Do I have to add a certain column to the user's table in my DB and have the posted items for the wishlist submitted and retrieved?
Any and all answers are appreciated, thank you.
for the table wishlist, the fields are : Id,UserIP, ProductsID
Id only autoincrement ID
UserIP to record visitor's IP
ProductsID to record ProductsID which cliked by visitor
After you click submit, then you must insert record to table wishlist, then to display you must select all table wishlist and left outer join with table product with ProductsID.
Hopefully you understand with my short explanation
For my first attempt at programming, I am making a simple webapp with php and MySQL, to enter customer information and store information about orders.
So far I've made a form to enter customer information, a page to edit that information and another page to show the different customers, which is all working fine.
I've run into a stump when trying to implement the next stage however.
I have an editcustomers page with 3 columns.
Column 1 shows a form containing customer information, allowing it to
be edited.
Column 2 allows ordering of products and showing how many products were ordered
Column 3 shows the total paid so far, and the total owing.
The code for the page I have at present:
<html>
<?php
$id = $_GET['id'];
require_once('connect.php');
$sth = $dbh->query("SELECT * FROM customers where id = '$id'");
$sth->setFetchMode(PDO::FETCH_ASSOC);
?>
<div id="main">
<div id="left">
<form name="myForm" action ="process.php" method ="post" >
<?php while($row = $sth->fetch()){ ?>
<p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
<p><input type = "text" name ="firstName" size ="30" value=" <?php echo $row["firstName"]?>"/>
<p><input type = "text" name ="lastName" size ="30" value="<?php echo $row["lastName"]?>"/>
<p></p>
<input type="submit" value="Update" />
<?php }?>
</div>
<div id="mid">
<p>Amount owed<br />
<input type = "text" name ="moneyOwed" size ="30" /></p>
<input type="submit" value="Pay" />
<p>Number of aaaa<br />
<input type = "text" name ="numAaa" size ="30" /></p>
<p>Number of bbbb<br />
<input type = "text" name ="numBbb" size ="30" /></p>
<p>
<input type="submit" value="Update" />
</div>
<div id="right">
<b>Total Balance</b>
<p> Money owed: </p>
<p> aaa total: </p>
<p> bbb total: </p>
<p> Total: </p>
<input type = "text" name ="pay" size ="20" /></p>
<input type="submit" value="Make Payment" />
</div>
<?php
$dbh =null;
?>
</body>
</html>
I have the following database structure:
A customers table, basic information like firstname, lastname, etc along with an id field.
A products table, listing the different products, only 1 row containing the costs for each product.
A purchases table, with an id field , fields like numProductA, numProductB, showing the quantity of each product ordered
The problem I'm having is that in my editcustomerspage, I have a database query to read in information from the customers table to fill the fields in the form in my first column and a separate query to update it if that if the function chosen.
Updating the second formshould be OK, as I could use a hidden field to differentiate forms, however I am unsure how to read the information in from the Customers table and from the Purchases table so that I can populate the fields in my second form..
Part of the problem is that the purchases table may be empty for a given customer, if that customer has not yet placed an order.
I was considering getting rid of the purchases table, and tacking the fields onto the users table which would solve my problem, although I think that is generally considered bad practice?
The products will be fixed in the application, and are not going to change. Even so, would it still be better to have column names cost and name and each products as a record?
What are some approaches I could take to solving this problem?
Even what you currently are doing is considered bad practice because of the columns numProductA, numProductB. What happens when you'll have 1000 products and a user orders different amounts from all of them. This work only if the list of products is fixed (i.e. coded into the application) and requires a lot of coding.
You would probably want to create a purchase_item table that has fields like: id, purchase_id, product_id, num_product. It would be like a line on a standard bill: purchase_id identifies which bill the line belongs to; product_id would show the product that line is about and num_product would show the amount (multiplier) on that line.
In this manner you don't need to change the code when new products are added to the database.
Without a detailed description of the problems you've run into, it's hard to say how to fix them. It should be possible to implement what you want with the described design. However, you seem to be misunderstanding relational databases, which is why your database architecture is eyebrow-raising.
A database table is not a spreadsheet. While a table can have millions of rows, the number of columns is usually limited to a much smaller number. Even when the limit is not a problem, the way relational databases are designed does not lend itself well to storage of uniform data across a large number of columns. For example, your database won't be able to answer questions such as “what's the best-selling product”, “what products have been purchased by more than 100 customers”, etc.
The rule of thumb is that each table should contain a fixed number of columns. If you find that you'll have to add columns when you e.g. start selling new products, you're doing it wrong.
Here's what you should probably do in your application:
products should be a table with one row per available product, containing, for each product, a unique id, the price and other information such as name, manufacturer etc.
purchases should contain one row for every combination of (customer, product) where a customer has ordered the product. The row shouuld contain the customer id, the product id, and quantity ordered.
Using SQL, you can then easily construct a query that produces the set of products ordered by a specific customer. Using a join, this information can be enhanced with product names and prices.
I want to make a form in php where there will be two categories. One for the Student profile details and another for the student's
marks details. Now the main problem is all this will be in one single form means in one page and the categories like student details will be save in student detail table of the database and the marks will be save in another database called marks database. I also need CRUD for these two categories.So how to do that?
Any suggestion and help will be highly appreciable.
Well there is no direct link between a form and a database table, so what is your problem?
Why don't you do the mysql_query() on each table one after another when you handle the form post?
You can build your form like this in order to separate the tables:
<input type="text" name="data[student][name]" />
<input type="text" name="data[category][name]" />
you PHP script then goes through the data and save it in the corresponding tables:
foreach($_POST['data'] as $table=>$row){
// do your insert statemnet here (don't foregt to escape the Parameters!)
}
I have some small issue with relationships with tables as below:
I have created a login and registration script which has the following
table name: members:
fields: member_id, firstname, lastname, login, password
Now i have made another table with this
table name: phone
fields: member_id, phoneid, name, number, prefix, time,total
I want to make a form whereby a admin can select the name of the client from a drop down list, and then add a record such as the number called, number prefix, total time and the amount for that period.
I dont know how to do this, please help me by creating a script or help me how to go about this.
So all the time a admin makes a form on Client A it gets added to a new row on the phone table, then i will just add a call script on the client side where they can see all the records that they have done.
Thanks please assist.
Regards
Your db design seems fundamentally flawed - the is no association between the members and the phone_numbers tables. Add a FK (member_id) to phone_numbers table.
Regards creating the front-end, there are quite a lot of libraries that have data stores (we use ExtJS), and upon flushing the store you can do the persistence with php.
Hope this helps!