Saturday 28 September 2013

Update MySQL database with pull-down options on website

Update MySQL database with pull-down options on website

I have a MySQL database that keeps user information. Periodically I will
need to update some of the user data without the hassle of going into the
database itself. I have created an Admin.php page that displays the user
data and the information that I will need to update. The main data that I
need to update is whether they have PAID, whether they would like periodic
UPDATES, and whether they have DOWNLOADED a document already from my
website. The database shows 0's as no and 1's as yes.
The three areas that I need to update currently have pull-down options
with Yes or No as the two choices. When I make a change and then select
the Update button I would like to be able to update my MySQL database with
the currently chosen areas for each user. Probably something fairly simple
but I've done some searching and can't find anything that really fits what
I'm trying to accomplish. The relevant code is below.
<?php
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Retrieve the data from MySQL
$query = "SELECT * FROM User_Database ORDER BY id ASC";
$data = mysqli_query($dbc, $query);
// Loop through the array of data, formatting it as HTML
echo '<table class="tablecontent2">';
echo '<tr><td colspan="10" class="userdataheader">User Database</td></tr>';
echo '<tr><th class="headerinfo2"
style="width:75px;"><strong>ID</strong></th>';
echo '<th class="headerinfo2"><strong>Last Name</strong></th>';
echo '<th class="headerinfo2"><strong>First Name</strong></th>';
echo '<th class="headerinfo2"><strong>E-mail</strong></th>';
echo '<th class="headerinfo2"><strong>Paid</strong></th>';
echo '<th class="headerinfo2"><strong>Contact</strong></th>';
echo '<th class="headerinfo2"><strong>Downloaded</strong></th>';
echo '<th style="width:200px;" class="headerinfo2"><strong>User
Since</strong></th>';
echo '<th class="headerinfo2"><strong>Update</strong></th>';
echo '<th class="headerinfo2"><strong>Remove</strong></th></tr>';
$i = 0;
while ($row = mysqli_fetch_array($data)) {
// Display the user data
if ($i == 0) {
if ($row['paid'] == 0) {
$paid = "No";
} else {
$paid = "Yes";
}
if ($row['updates'] == 0) {
$contact = "No";
} else {
$contact = "Yes";
}
if ($row['download'] == 0) {
$download = "No";
} else {
$download = "Yes";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
echo '<tr><td align="center" width="20px" class="user" >' . $row['id'] .
'</td>';
echo '<td class="user">' . $row['last_name'] . '</td>';
echo '<td class="user">' . $row['first_name'] . '</td>';
echo '<td class="user">' . $row['email'] . '</td>';
?>
<td class="user" >
<select style="width:80px;" id="paid" name="paid" value=$paid >
<option value="Yes" <?php if($paid=="Yes") echo "selected"; ?>
>Yes</option>
<option value="No" <?php if($paid=="No") echo "selected"; ?>
>No</option>
</select></td>
<td class="user" >
<select style="width:80px;" id="contact" name="contact" value=$contact >
<option value="Yes" <?php if($contact=="Yes") echo "selected"; ?>
>Yes</option>
<option value="No" <?php if($contact=="No") echo "selected"; ?> >No</option>
</select></td>
<td class="user" >
<select style="width:80px;" id="download" name="download"
value=$download >
<option value="Yes" <?php if($download=="Yes") echo "selected"; ?>
>Yes</option>
<option value="No" <?php if($download=="No") echo "selected"; ?> >No</option>
</select></td>
<?php
echo '<td class="user">' . $row['date'] . '</td>';
echo '<td class="user"><input style="width:100px;" type="submit"
value="Update" name="submit1" /></td>';
echo '<td class="user"><input style="width:100px;" type="submit"
value="Remove" name="submit2" /></td></tr>';
?>
</form>
When I click the "submit 1" button it goes to the head section of the page
and the update should occur here:
<?php
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$error_msg = "";
if (isset($_POST['submit1'])) {
// Grab the user-entered log-in data
if ($paid == "Yes") {
$paid = 1;
} else {
$paid = 0;
}
if ($contact == "Yes") {
$contact = 1;
} else {
$contact = 0;
}
if ($download == "Yes") {
$download = 1;
} else {
$download = 0;
}
if (!empty($paid) && !empty($contact) && !empty($download)) {
// Verify data is not empty
$query = "SELECT * FROM User_Database WHERE email = $email";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 1) {
// The email is OK so update the database
$query = "UPDATE User_Database SET paid='$paid',
updates='$contact', download='$download'
WHERE email='$email'";
$result = mysqli_query($dbc, $query) or die('Error querying
database.');
}
} else {
// The email/password weren't entered so set an error message
$error_msg = 'Database not updated. Not all of the data was filled in.';
}
?>
At this point I'm not sure what I need to do to update the database. Any
help will be greatly appreciate. Thanks.

No comments:

Post a Comment