Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Ifrah Shahid
lab-exercises
Commits
9ce59ab1
Commit
9ce59ab1
authored
Nov 29, 2016
by
Sorrel Harriet
Browse files
removing unused file
parent
0a3688dd
Changes
1
Hide whitespace changes
Inline
Side-by-side
lab-8/music-store-app/views/update-stock.php
deleted
100644 → 0
View file @
0a3688dd
<?php
$content
=
"<h1>Update Album Stock</h1>"
;
// define a variable with path to the script which will process form
// -> $_SERVER["PHP_SELF"] is a path to the current script (index.php)
$action
=
$_SERVER
[
"PHP_SELF"
]
.
"?page=update-stock"
;
// turn autocommit off
mysqli_autocommit
(
$link
,
FALSE
);
// start a transaction
mysqli_query
(
$link
,
'START TRANSACTION'
);
// fetch the albums and associated stock info
// apply FOR UPDATE to prevent reads until
// the transaction is completed with COMMIT or ROLLBACK
$sql
=
"SELECT a.upc, a.title, i.stock
FROM Album a
INNER JOIN Inventory i
ON a.upc=i.album_upc
ORDER BY title FOR UPDATE"
;
$result
=
mysqli_query
(
$link
,
$sql
);
// check query returned a result
if
(
$result
===
false
)
{
echo
mysqli_error
(
$link
);
}
else
{
$options
=
""
;
// create an option for each artist
while
(
$row
=
mysqli_fetch_assoc
(
$result
))
{
$options
.
=
"<option value='"
.
$row
[
'upc'
]
.
"'>"
;
$options
.
=
$row
[
'title'
];
$options
.
=
"</option>"
;
}
}
// define the form HTML (would ideally be in a template)
$form_html
=
"<form action='"
.
$action
.
"' method='POST'>
<fieldset>
<label for='t_name'>Name:</label>
<input type='text' name='t_name' required>
</fieldset>
<fieldset>
<label for='t_number'>Track number:</label>
<input type='number' name='t_number' min='1' max='25'>
</fieldset>
<fieldset>
<label for='a_upc'>Album:</label>
<select name='a_upc' required>
<option value='' disabled selected>Select an album</option>
"
.
$options
.
"
</select>
</fieldset>
<button type='submit'>Submit</button>
</form>"
;
// append form HTML to content string
$content
.
=
$form_html
;
// ------- START form processing code... -------
// define variables and set to empty values
$title
=
$artist_id
=
$price
=
$year
=
$genre
=
""
;
// check if there was a POST request
if
(
$_SERVER
[
"REQUEST_METHOD"
]
==
"POST"
)
{
// validate the form data
$a_upc
=
mysqli_real_escape_string
(
$link
,
clean_input
(
$_POST
[
"a_upc"
]));
$t_name
=
mysqli_real_escape_string
(
$link
,
clean_input
(
$_POST
[
"t_name"
]));
$t_number
=
mysqli_real_escape_string
(
$link
,
clean_input
(
$_POST
[
"t_number"
]));
// define the insertion query
$sql
=
sprintf
(
"INSERT INTO Track (album_upc, name, track_number)
VALUES ('%s', '%s', %d)"
,
$a_upc
,
$t_name
,
$t_number
);
// run the query to insert the data
$result
=
mysqli_query
(
$link
,
$sql
);
// check if the query went ok
if
(
$result
===
false
)
{
mysqli_rollback
(
$link
);
// if query returns false, rollback transaction
// handle specific errors based on mysli error number code
// (in order to output more useful message to user!)
$errno
=
mysqli_errno
(
$link
);
switch
(
$errno
)
{
case
1062
:
// case for duplicate entry
$content
.
=
"There is already a track with that name or number."
;
break
;
default
:
echo
mysqli_error
(
$link
);
}
}
else
{
mysqli_commit
(
$link
);
// else, commit transaction
$content
.
=
"Track successfully added."
;
}
}
// ------- END form processing code... -------
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment