Jump to content

Show the user all of their information?


scm22ri

Recommended Posts

if a user has more than one vehicle stored in my database how would I show them all vehicles in their admin panel? My current code is only showing one vehicle but "user 5" currently has two vehicles. How would I show/present "user 5" with all of their information?

 

This is my database info.

http://whatsmyowncarworth.com/auto-members/display-vehicle-info.php

 

How would I be able to show all of "user 5" vehicles in his admin panel?

 

Thanks!

Link to comment
Share on other sites

Hi Scootash,

 

Thanks for the reply. I am looping but are my loops or mysql commands incorrect? I don't want the user to see one result, I want them to see all of their results.

 

Thanks!

 

<?php
session_start(); // Must start session first thing

// Here we run a login check
if (!isset($_SESSION['id'])) { 
   echo 'Please <a href="login.php">log in</a> to access your account';
   exit(); 
}
//Connect to the database through our include 
include_once "connect_to_mysql.php";
// Place Session variable 'id' into local variable
$id = $_SESSION['id'];
// Process the form if it is submitted
if ($_POST['make']) {
    $year = mysql_real_escape_string($_POST['year']); 
    $make = mysql_real_escape_string($_POST['make']);
    $model = mysql_real_escape_string($_POST['model']);
    $price = mysql_real_escape_string($_POST['price']);
$exteriorcolor = mysql_real_escape_string($_POST['exteriorcolor']);
$interiorcolor = mysql_real_escape_string($_POST['interiorcolor']);
$engine = mysql_real_escape_string($_POST['engine']);
$transmission = mysql_real_escape_string($_POST['transmission']);
    $fueltype = mysql_real_escape_string($_POST['fueltype']);
$mileage = mysql_real_escape_string($_POST['mileage']);
$state = mysql_real_escape_string($_POST['state']);
$city = mysql_real_escape_string($_POST['city']);
$number = mysql_real_escape_string($_POST['number']);

    $sql = mysql_query("UPDATE car_data SET year='$year', make='$make', model='$model', price='$price', exteriorcolor='$exteriorcolor', 
interiorcolor='$interiorcolor', engine='$engine', transmission='$transmission', fueltype='$fueltype', mileage='$mileage', state='$state', city='$city', number='$number' WHERE id='$id'"); 
    echo 'Your account info has been updated, visitors to your profile will now see the new info.<br /><br />
    To return to your profile edit area, <a href="/auto-members/member_account.php">click here</a>';
exit();
} // close if post
?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM car_data WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$year = $row["year"];
$make = $row["make"];
$model = $row["model"];

$price = $row["price"];
$exteriorcolor = $row["exteriorcolor"];
$interiorcolor = $row["interiorcolor"];
$engine = $row["engine"];
$transmission = $row["transmission"];
$fueltype = $row["fueltype"];
$mileage = $row["mileage"];
$state = $row["state"];
$city = $row["city"];
$number = $row["number"];
}
?>
<html>
<head>
<title>Submit Information</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!-- Form Validation -->
function validate_form ( ) { 
valid = true; 
if ( document.form.year.value == "" ) { 
alert ( "Year must not be blank." ); 
valid = false;
}
if ( document.form.make.value == "" ) { 
alert ( "Make must not be blank." ); 
valid = false;
}
if ( document.form.model.value == "" ) { 
alert ( "Model must not be blank." ); 
valid = false;
}
if ( document.form.price.value == "" ) { 
alert ( "Price must not be blank." ); 
valid = false;
}
if ( document.form.exteriorcolor.value == "" ) { 
alert ( "Exterior Color must not be blank." ); 
valid = false;
}
if ( document.form.interiorcolor.value == "" ) { 
alert ( "Interior Color must not be blank." ); 
valid = false;
}
if ( document.form.engine.value == "" ) { 
alert ( "Engine must not be blank." ); 
valid = false;
}
if ( document.form.mileage.value == "" ) { 
alert ( "Mileage must not be blank." ); 
valid = false;
}
if ( document.form.transmission.value == "" ) { 
alert ( "Transmission must not be blank." ); 
valid = false;
}
if ( document.form.fueltype.value == "" ) { 
alert ( "Fuel Type must not be blank." ); 
valid = false;
}
if ( document.form.state.value == "" ) { 
alert ( "State must not be blank." ); 
valid = false;
}
if ( document.form.city.value == "" ) { 
alert ( "City/Town must not be blank." ); 
valid = false;
}
if ( document.form.number.value == "" ) { 
alert ( "Favorite Number must not be blank." ); 
valid = false;
}
return valid;
}
<!-- Form Validation -->
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
Here you can update vehicle information for your 
<?php echo "$year"; ?>
, 
<?php echo "$make"; ?>
, 
<?php echo "$model"; ?>
<form action="edit_car.php" method="post" enctype="multipart/form-data" name="form" id="form" onsubmit="return validate_form ( );">
    <table>
<tr>
      <td>Year:</td>
      <td><select name="year">
  <option value="<?php echo "$year"; ?>"><?php echo "$year"; ?></option>
      <option value="2012">2012</option>
      <option value="2011">2011</option>  
  <option value="2010">2010</option>
      <option value="2009">2009</option>
  <option value="2008">2008</option>
      <option value="2007">2007</option>
  <option value="2006">2006</option>
      <option value="2005">2005</option>
  <option value="2004">2004</option>
      <option value="2003">2003</option>
  <option value="2002">2002</option>
      <option value="2001">2001</option>
  <option value="2000">2000</option>
      <option value="1999">1999</option>
  <option value="1998">1998</option>
      <option value="1997">1997</option>
  <option value="1996">1996</option>
      <option value="1995">1995</option>
  <option value="1994">1994</option>
      <option value="1993">1993</option>
  <option value="1992">1992</option>
      <option value="1991">1991</option>
  <option value="1990">1990</option>
      </select></td>
    </tr>
    <tr>
<td>Make:</td>
<td><input type="text" name="make" value="<?php echo "$make"; ?>" /></td>
    </tr>
    <tr>
<td>Model:</td>
<td><input type="text" name="model" value="<?php echo "$model"; ?>" /></td>
    </tr>
    <tr>
<td>Price:</td>
<td><input type="text" name="price" value="<?php echo "$price"; ?>" /></td>
    </tr>
    <tr>
<td>Exterior Color:</td>
<td><input type="text" name="exteriorcolor" value="<?php echo "$exteriorcolor"; ?>" /></td>
    </tr>
<tr>
<td>Interior Color:</td>
<td><input type="text" name="interiorcolor" value="<?php echo "$interiorcolor"; ?>" /></td>
    </tr>
<tr>
<td>Engine:</td>
<td><input type="text" name="engine" value="<?php echo "$engine"; ?>" /></td>
    </tr>
<tr>
<td>Mileage:</td>
<td><input type="text" name="mileage" value="<?php echo "$mileage"; ?>" /></td>
    </tr>
<tr>
      <td>Transmission:</td>
      <td><select name="transmission">
  <option value="<?php echo "$transmission"; ?>"><?php echo "$transmission"; ?></option>
      <option value="Automatic">Automatic</option>
      <option value="Standard">Standard</option>
      </select></td>
    </tr>
<tr>
      <td>Fuel Type:</td>
      <td><select name="fueltype">
  <option value="<?php echo "$fueltype"; ?>"><?php echo "$fueltype"; ?></option>
      <option value="Gas">Gas</option>
      <option value="Diesel">Diesel</option>
      </select></td>
    </tr>
<tr>
      <td>State:</td>
      <td><select name="state">
  <option value="<?php echo "$state"; ?>"><?php echo "$state"; ?></option>
      <option value="Alabama">Alabama</option>
      <option value="Alaska">Alaska</option>  
  <option value="Arizona">Arizona</option>
      <option value="Arkansas">Arkansas</option>
  <option value="California">California</option>
      <option value="Colorado">Colorado</option>
  <option value="Connecticut">Connecticut</option>
      <option value="Delaware">Delaware</option>
  <option value="Florida">Florida</option>
      <option value="Georgia">Georgia</option>
  <option value="Hawaii">Hawaii</option>
      <option value="Idaho">Idaho</option>
  <option value="Illinois">Illinois</option>
      <option value="Indiana">Indiana</option>
  <option value="Iowa">Iowa</option>
      <option value="Kansas">Kansas</option>
  <option value="Kentucky">Kentucky</option>
      <option value="Louisiana">Louisiana</option>
  <option value="Maine">Maine</option>
      <option value="Maryland">Maryland</option>
  <option value="Massachusetts">Massachusetts</option>
      <option value="Michigan">Michigan</option>
  <option value="Minnesota">Minnesota</option>
      <option value="Mississippi">Mississippi</option>
  <option value="Missouri">Missouri</option>
      <option value="Montana">Montana</option>
  <option value="Nebraska">Nebraska</option>
      <option value="Nevada">Nevada</option>
  <option value="New Hampshire">New Hampshire</option>
      <option value="New Jersey">New Jersey</option>
  <option value="New Mexico">New Mexico</option>
      <option value="New York">New York</option>
  <option value="North Carolina">North Carolina</option>
      <option value="North Dakota">North Dakota</option>
  <option value="Ohio">Ohio</option>
      <option value="Oklahoma">Oklahoma</option>
  <option value="Oregon">Oregon</option>
      <option value="Pennsylvania">Pennsylvania</option>
  <option value="Rhode Island">Rhode Island</option>
      <option value="South Carolina">South Carolina</option>
  <option value="South Dakota">South Dakota</option>
      <option value="Tennessee">Tennessee</option>
  <option value="Texas">Texas</option>
      <option value="Utah">Utah</option>
  <option value="Vermont">Vermont</option>
      <option value="Virginia">Virginia</option>
  <option value="Washington">Washington</option>
      <option value="West Virginia">West Virginia</option>
  <option value="Wisconsin">Wisconsin</option>
  <option value="Wyoming">Wyoming</option>
      </select></td>
    </tr>
    <tr>
<td>City/Town:</td>
<td><input type="text" name="city" value="<?php echo "$city"; ?>" /></td>
    </tr>
 <tr>
<td>Fav. Number?</td>
<td><input type="text" name="number" value="<?php echo "$number"; ?>" /></td>
    </tr>
    <tr>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr>
    </table>



</form>
</body>
</html>

Link to comment
Share on other sites

Hi Everyone,

 

I deleted the LIMIT 1 but the code is still only showing one item from the database.  I tried changing the mysql function to mysql_fetch_assoc and I changed my "while" statement to a "if" and it's still only showing one item per user.

 

Is my problem still with how I'm calling the information from the database or is it something else?

 

$sql = mysql_query("SELECT * FROM car_data WHERE id='$id'");

 

Thanks everyone

Link to comment
Share on other sites

The problem is that you are not outputting anything in the loop, you are just setting a variable to output later. But, that variable overwrites itself on every iteration, so the value will only be equal to the last row. Here's an example, in case that didn't make any sense.

 

If this is your data set:

ID | Year | Make     | Model
5  | 2004 | Ford     | Explorer
5  | 2002 | Dodge    | Durango
5  | 2005 | Chrysler | Town & Country

 

And this is your loop:

$sql = mysql_query("SELECT * FROM car_data WHERE id='$id'");
while($row = mysql_fetch_array($sql)){
    $year = $row["year"];
    $make = $row["make"];
    $model = $row["model"];
}

 

This is how it will work out:

Iteration 1
$year = 2004
$make = Ford
$model = Explorer

Iteration 2
$year = 2002
$make = Dodge
$model = Durango

Iteration 3
$year = 2005
$make = Chrysler
$model = Town & Country

 

So, as you can see, on each iteration you are over writing the previously-set variable with the current row's data. So in the end, you only have one row's worth of data.

 

You can do a couple of things here:

1. Output directly from the while loop. This doesn't seem like a good idea in this case, since you have all your markup afterwards

2. Make a new array and push to it on each iteration, and then loop through it later. This doesn't sound like a good idea either, since you aren't doing anything else in the while loop.

3. Move the while loop to where it needs to be output.

4. Construct an output string inside the while loop, to be output later.

 

I would go with either #3 or #4.

Link to comment
Share on other sites

You are also going to have a problem in the update functionality once you fix that because you would have no way to know which entry the user submitted for update. If you want the user to be able to update all the records at once you want ONE form encompassing all the input fields for all records. In that case I would use input fields named as array with the unique id of the record as the index. If you want the user to only update one record at a time then you can create individual forms for each and just use a hidden field for the record id. Right now your update query is using what appears to be the ID of the user - which would update all the records of the user based upon the content of one set of input fields.

 

 

Link to comment
Share on other sites

@scootstah, I think, fetch_assoc and fetch_row works if you want to retrieve a data only from one table.

What about this?

Ps. Sorry wrong place  :'(

 

$query = "SELECT `cities`.`Name` ,`countries`.`Name` FROM `cities`
LEFT JOIN `countries` ON `cities`.`CountryCode` = `countries`.`Code`
WHERE `cities`.`ID` = 10";

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    echo '<pre>'.print_r($row, true).'</pre>';  
}
// output
Array
(
    [0] => Tilburg
    [Name] => Netherlands
    [1] => Netherlands
)


$query = "SELECT `cities`.`Name` ,`countries`.`Name` FROM `cities`
LEFT JOIN `countries` ON `cities`.`CountryCode` = `countries`.`Code`
WHERE `cities`.`ID` = 10";

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
    echo '<pre>'.print_r($row, true).'</pre>';  
}
// output
Array
(
    [Name] => Netherlands
)

Link to comment
Share on other sites

Use aliasing to change the name of the index.

$query = "SELECT `cities`.`Name`AS city_name ,`countries`.`Name`AS country_name FROM `cities`
LEFT JOIN `countries` ON `cities`.`CountryCode` = `countries`.`Code`
WHERE `cities`.`ID` = 10";

 

EDIT: Also, if you know that data will be pulled from two tables like this then it is best to prefix the columns in the database ahead of time, to avoid confusion.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.