Page 1 of 2
Re: edit profile php
Posted: Tue Jun 30, 2015 7:08 pm
by Celauran
ianhaney wrote:I have noticed one issue as well, it's the dates are being outputted the same
Code: Select all
<label>Car Tax Renewal Date :</label>
<input type="text" id="datepicker" name="tax" required="required" placeholder="Please Enter your Car Tax Renewal Date" value="<?php echo $row['datedue'];?>" />
<br /><br />
<label>MOT Renewal Date :</label>
<input type="text" id="datepicker2" name="mot" required="required" placeholder="Please Enter your Car MOT Renewal Date" value="<?php echo $row['datedue'];?>" />
<br /><br />
<label>Insurance Renewal Date :</label>
<input type="text" id="datepicker3" name="insurance" required="required" placeholder="Please Enter your Car Insurance Renewal Date" value="<?php echo $row['datedue'];?>" />
You're echoing the same variable three times, of course they're the same.
Re: edit profile php
Posted: Wed Jul 01, 2015 6:48 am
by Celauran
Your renewals table has three rows per user, so you're going to get three rows back from the database. You need to iterate over those and assign the correct datedue to the correct value.
This doesn't do anything
Code: Select all
$insurance = $row['datedue'];
$tax = $row['datedue'];
$mot = $row['datedue'];
You're assigning the same value to three variables which you never use.
Re: edit profile php
Posted: Wed Jul 01, 2015 7:10 am
by Celauran
Where are $row['insurance'], $row['tax'], and $row['mot'] coming from? Have you modified your query to return those columns?
Re: edit profile php
Posted: Wed Jul 01, 2015 7:16 am
by Celauran
It also ignores the fact that this:
Code: Select all
$insurance = $row['insurance'];
$mot = $row['mot'];
$tax = $row['tax'];
does nothing. You're assigning values to variables
you aren't using
Re: edit profile php
Posted: Wed Jul 01, 2015 7:23 am
by Celauran
ianhaney wrote:The mot, tax and insurance I got from the name of the input fields within the form
That's not how database queries work. The $row array is going to be populated with the names of columns returned by the query.
As I mentioned earlier, your best bet is to iterate over the three rows and build up an array of renewal dates. You've got some sort of ID that maps renewal date to type. You'll likely need to alter your query some as users table has a column called id and so does renewals.
Re: edit profile php
Posted: Wed Jul 01, 2015 7:37 am
by Celauran
Because you're echoing the same thing three times. Try something like this
Code: Select all
<?php
// Stuff here
$sql = "SELECT u.id, u.name, u.email, u.address1, u.address2, u.town, u.county, u.postcode,
u.telnumber, u.mobnumber, u.model, u.numplate, DATE_FORMAT(r.renewal_date, '%e %M %Y') AS renewal,
i.description
FROM users AS u
INNER JOIN renewal AS r ON u.id = r.id
INNER JOIN item AS i on r.item_id = i.item_id
WHERE u.id= {$id}";
// execute query
$renewals = [];
foreach ($row as $item) {
$slug = str_replace('.', '', strtolower($item['description']));
$renewals[$item['description']] = $item['renewal'];
}
// Output personal information, then
<label>Car Tax Renewal Date :</label>
<input type="text" id="datepicker" name="tax" required="required" placeholder="Please Enter your Car Tax Renewal Date" value="<?php echo $renewals['tax'];?>" />
<br /><br />
<label>MOT Renewal Date :</label>
<input type="text" id="datepicker2" name="mot" required="required" placeholder="Please Enter your Car MOT Renewal Date" value="<?php echo $renewals['mot'];?>" />
<br /><br />
<label>Insurance Renewal Date :</label>
<input type="text" id="datepicker3" name="insurance" required="required" placeholder="Please Enter your Car Insurance Renewal Date" value="<?php echo $renewals['insurance'];?>" />
Re: edit profile php
Posted: Wed Jul 01, 2015 7:46 am
by Celauran
What version of PHP are you using?
Re: edit profile php
Posted: Wed Jul 01, 2015 7:53 am
by Celauran
That's a really old version and you really ought to upgrade. In the meantime, you'll need to use long array syntax, so replace [] with array()
Re: edit profile php
Posted: Wed Jul 01, 2015 7:57 am
by Celauran
Something like this should do what you're looking for:
Code: Select all
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
require_once "functions.php";
require_once "db-const.php";
$title = "Edit My Account Profile - The Tax Elephants";
$pgDesc = "";
$pgKeywords = "";
include 'includes/header.php';
if (logged_in() == false) {
redirect_to("login.php");
exit;
} else {
if (isset($_GET['id']) && $_GET['id'] != "") {
$id = $_GET['id'];
} else {
$id = $_SESSION['user_id'];
}
$db = mysqli_connect("" , "", "") or die("Check connection parameters!");
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)
mysqli_select_db($db,"") or die(mysqli_error($db));
if (mysqli_connect_error()) {
die ('Failed to connect to MySQL');
}
}
// Prepared statements are better, but at least escape things before tossing them into a query
$id = mysqli_real_escape_string($db, $id);
$sql = "SELECT u.id, u.name, u.email, u.address1, u.address2, u.town, u.county, u.postcode,
u.telnumber, u.mobnumber, u.model, u.numplate, DATE_FORMAT(r.renewal_date, '%e %M %Y') AS renewal,
i.description
FROM users AS u
INNER JOIN renewal AS r ON u.id = r.id
INNER JOIN item AS i on r.item_id = i.item_id
WHERE u.id= {$id}";
$query = mysqli_query($db, $sql) or die (mysqli_error($db));
$rows = array();
while ($row = mysqli_fetch_assoc($query)) {
$rows[] = $row;
}
$renewals = array();
foreach ($row as $item) {
$slug = str_replace('.', '', strtolower($item['description']));
$renewals[$item['description']] = $item['renewal'];
}
// We'll just use the first row for the form since most of the data will be the same
$row = $rows[0];
?>
<!--CONTENT-->
<div id="column-whole">
<form method="post" action="edit-data.php">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<label>Name :</label>
<input type="text" name="name" required="required" placeholder="Please Enter Name" value="<?php echo $row['name']; ?>" />
<br /><br />
<label>Email :</label>
<input type="email" name="email" required="required" placeholder="Please Enter Email" value="<?php echo $row['email']; ?>" />
<br /><br />
<label>Address Line 1 :</label>
<input type="text" name="address1" required="required" placeholder="Please Enter Address Line 1" value="<?php echo $row['address1'];?>" />
<br /><br />
<label>Address Line 2 :</label>
<input type="text" name="address2" required="required" placeholder="Please Enter Address Line 2" value="<?php echo $row['address2'];?>" />
<br /><br />
<label>Town :</label>
<input type="text" name="town" required="required" placeholder="Please Enter Town" value="<?php echo $row['town'];?>" />
<br /><br />
<label>Copunty :</label>
<input type="text" name="county" required="required" placeholder="Please Enter County" value="<?php echo $row['county'];?>" />
<br /><br />
<label>Postcode :</label>
<input type="text" name="postcode" required="required" placeholder="Please Enter Postcode" value="<?php echo $row['postcode'];?>" />
<br /><br />
<label>Telephone Number :</label>
<input type="text" name="telnumber" required="required" placeholder="Please Enter Telephone Number" value="<?php echo $row['telnumber'];?>" />
<br /><br />
<label>Mobile Number :</label>
<input type="text" name="mobnumber" required="required" placeholder="Please Enter Mobile Number" value="<?php echo $row['mobnumber'];?>" />
<br /><br />
<label>Model of Car/Van :</label>
<input type="text" name="model" required="required" placeholder="Please Enter Model of Car/Van" value="<?php echo $row['model'];?>" />
<br /><br />
<label>Car Tax Renewal Date :</label>
<input type="text" id="datepicker" name="tax" required="required" placeholder="Please Enter your Car Tax Renewal Date" value="<?php echo $renewals['tax'];?>" />
<br /><br />
<label>MOT Renewal Date :</label>
<input type="text" id="datepicker2" name="mot" required="required" placeholder="Please Enter your Car MOT Renewal Date" value="<?php echo $renewals['mot'];?>" />
<br /><br />
<label>Insurance Renewal Date :</label>
<input type="text" id="datepicker3" name="insurance" required="required" placeholder="Please Enter your Car Insurance Renewal Date" value="<?php echo $renewals['insurance'];?>" />
<br />
<input type="submit" name="submit value" value="Update">
</form>
</div>
<!--CONTENT-->
<?php include 'includes/footer.php'; ?>
Re: edit profile php
Posted: Wed Jul 01, 2015 8:04 am
by Celauran
That should be $rows, not $row
Re: edit profile php
Posted: Wed Jul 01, 2015 8:08 am
by Celauran
Ha! Whoops.
Code: Select all
$renewals = array();
foreach ($rows as $item) {
$slug = str_replace('.', '', strtolower($item['description']));
$renewals[$slug] = $item['renewal'];
}
Re: edit profile php
Posted: Wed Jul 01, 2015 8:59 am
by Celauran
Where are you calling session_start()?
Re: edit profile php
Posted: Wed Jul 01, 2015 9:24 am
by Celauran
Next question then is where is header.php being included?
Re: edit profile php
Posted: Wed Jul 01, 2015 9:39 am
by Celauran
You're missing FROM. FROM users as u
Re: edit profile php
Posted: Wed Jul 01, 2015 9:59 am
by Celauran
I only just realized you're using UPDATE. That's select syntax.
https://dev.mysql.com/doc/refman/5.0/en/update.html