What I'm trying to acomplish is to have a list of years in a <form> with a checkbox for each year and let the user select what year applies to the post such as: 2008, 2009, 2012 (they might select 4 out of 5 years, not just one year). Then after the user submits this form I need it to post the variable say as: $2009=2009, $2008=2008 so I can use those variables to select a unique id out of the mysql db.

Not sure if I'm on a wild goose chase with my code, but so far I got:

post.php looks like:

<form method="POST" action="post1.php?id=<?=$id?>&cat=<?=$cat?>">
<p>
<span id="title">Select years that apply to vehicle:</span><br />
<input type="checkbox" name="formDoor[]" value="2012" />2012<br />
<input type="checkbox" name="formDoor[]" value="2011" />2011<br />
<input type="checkbox" name="formDoor[]" value="2010" />2010<br />
<input type="checkbox" name="formDoor[]" value="2009" />2009<br />
<input type="checkbox" name="formDoor[]" value="2008" />2008 />
     <input type="submit" value="Submit" />
</form>

The for my post1.php it looks like:

<?php
include 'connect.php';

  $aDoor = $_POST['formDoor'];
  if(empty($aDoor))
  {
    echo("You didn't select any years.");
  }
  else
  {
    $N = count($aDoor);
    echo("You selected $N years(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo ($aDoor[$i] . " ");
    }
  }



?>

I dont have my insert statment done yet since I cant figure out how to create these variables. I guess I'm not sure if there should be a foreach loop or stick with this one? Right now it comes out to echo "You selected 4 years: 2008 2009 2010, but they dont have variable attach to each one..If anyone can help me I sure would appreciate it. :) I know its not the most advanced code, and might not even be the correct way to go about doing it, but thats why I need an expert to look at it. Thanks!

Ok so I got this below and sort of got it working...

$a2012 = $_POST['2012'];
$a2011 = $_POST['2011'];
$a2010 = $_POST['2010'];
$a2009 = $_POST['2009'];
$a2008 = $_POST['2008'];


$cars=array(
"$a2012",
"$a2011",
"$a2010",
"$a2009",
"$a2008"

);
$matches = implode(',', $cars);

//test to echo out array from select values
echo "$matches";

$result = mysql_query("SELECT * FROM VehicleYearModel WHERE year = $matches");
while($row = mysql_fetch_array($result))
  {

  echo $row['id'];
  echo "<br />";
  }

But how can I use this select statment with $matches being a array and then returning the row id for each year???

What you are currently doing is trying to find records where year = '2012, 2011, 201, 2009, 2008' and I would think that you don't have a value like that in your database. I presume you are trying to see if a year matches ONE of the years in your array, in which case do this:

$result = mysql_query("SELECT * FROM VehicleYearModel WHERE year IN ($matches)");

Thanks, I will try that when I get home tonight.. In my DB I have:

|  ID  |  Vehicle  |  Year  |  Model   |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|   1  |  Ford     |  2012  |  Focus   |
|   2  |  Ford     |  2011  |  Focus   |
|   3  |  Chevy    |  2012  |  Colbalt |
|   4  |  Ford     |  1995  |  Mustang |

I want to let the user make a note about a certain vehicle in a form, but if the same note applies to multiple vechile years, I want the user to select the years, that it applies to before the form is submitted, thats where the check boxes come in.

The first table above holds all the vehicles

The second table below is called general info and hold notes submitted. It would be based on the unique "ID" from above. So if the user typed a note about a ford focus 2012 and checkboxed 2011 saying "this is a great car" I would want that note to transfer to the below table using the unique "id" of 1 and 2 and save that note to each one.

|  ID |  Engine   |  note  |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|   need this populated with unique "id" from above  |  2.5     | "this is a great car    
|     |       |    |  
|     |       |    |  
|     |       |    |  

@417linux -

You emailed me asking whether a previous thread I'd commenting on regarding dynamic or variable variables was applicable to your problem...

The number of times you'll actually need to use dynamic variables is very low indeed. The problem you have is just to preserve a user's selection of various checkboxes and to use these in a query to select and insert data into your database.

The start you've made is heading in the correct direction.

  1. Checkbox input elements posted as array - check;
  2. Accessing them via the $_POST array after form submission - check;
  3. Use the values in a query (see @simplypixie's post) - check;

    // Form submitted
    $years = isset($_POST['formDoor']) ? $_POST['formDoor'] : array();
    
    // Build query to find vehicle ids
    $sql = sprintf('SELECT `id` FROM `vehicles` WHERE `year` IN (%s)', implode(', ', $years));
    $result = mysql_query($sql);
    
    // Prepare your engine size and note (assumed posted with years)
    $engine = isset($_POST['engine']) ? $_POST['engine'] : '';
    $note = isset($_POST['note']) ? $_POST['note'] : '';
    
    // Iterate over vehicles ids
    while(($row = mysql_fetch_assoc($result)) {
            // Build query to insert vehicle note
            $sql = sprintf("INSERT INTO `vehicle_notes` SET `vehicle_id` = %d, `engine` = '%s', `note` = '%s'", $row['id'], $engine, $note);
            mysql_insert($sql);
    }
    

I would suggest modifying your vehicle note table to include a primary key (unique identifier) and a foriegn key to the vehicle table.

Hope this helps to answer your question.

@blocblue
Wow, I think this is going to work great, I cant wait to get off work to try it! I did forget to include my primary ID in the above post, but I do have one and the "id" in the above post is called "carid" and the primary is just called "id". I do not though have a foriegn id, so I will implement that. Thanks so much and I will let you know how it goes!

If the carid corresponds to the car / vehicle table, then that is your foriegn key.

Well it came back as this:

2012,2011,,2009,
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/73/7015973/html/autorepair/post1.php on line 53

below is post.php

<form method="POST" action="post1.php?id=<?=$id?>&cat=<?=$cat?>">

<span id="title">Enter a Title: </span><input type="text" size="50" alt="title" name="title" value="" onclick="javascript:select();" />
<p>
<span id="title">Engine Type: </span><input type="text" size="50" alt="engine" name="engine" value="" onclick="javascript:select();" />
<p>
<span id="title">Select years that apply to repair:</span><br />
<input type="checkbox" name="2012" value="2012" />2012<br />
<input type="checkbox" name="2011" value="2011" />2011<br />
<input type="checkbox" name="2010" value="2010" />2010<br />
<input type="checkbox" name="2009" value="2009" />2009<br />
<input type="checkbox" name="2008" value="2008" />2008

<h2>Add a new step below:</h2>
  <div id="dynamicInput">
          Step 1<br><textarea rows="5" cols="120" name="myInputs[]" ></textarea>
     </div>
     <input type="button" value="Add another Step" onClick="addInput('dynamicInput');"/>
     <input type="submit" value="Submit" />
</form>

below is post1.php

<?php
include 'connect.php';

//get variables from what I passed for pervious page
$id = $_GET['id'];
$cat = $_GET['cat'];

//creating the foreach loop to pull from each textarea box
$myInputs = $_POST["myInputs"];
foreach ($myInputs as $eachInput) {
     echo $eachInput . "<br>";
}

//imploding all the results from $myInput into one variable called $results
$result = implode("<p>", $myInputs);

//adding to the database
mysql_query("INSERT INTO $cat (carid, title, repair)
VALUES ('$id', '$_POST[title]', '$result')");

$years = isset($_POST['formDoor']) ? $_POST['formDoor'] : array();

$a2012 = $_POST['2012'];
$a2011 = $_POST['2011'];
$a2010 = $_POST['2010'];
$a2009 = $_POST['2009'];
$a2008 = $_POST['2008'];


$cars=array(
"$a2012",
"$a2011",
"$a2010",
"$a2009",
"$a2008"

);
$matches = implode(',', $cars);
echo "$matches";

//$result = mysql_query("SELECT * FROM VehicleYearModel WHERE year IN $matches");

// Build query to find vehicle ids
$sql = sprintf('SELECT id FROM VehicleModelYear WHERE year IN (%s)', implode(', ', $years));
$result3 = mysql_query($sql);


// Prepare your engine size and note (assumed posted with years)
$engine = isset($_POST['engine']) ? $_POST['engine'] : '';
$note = isset($_POST['title']) ? $_POST['title'] : '';

// Iterate over vehicles ids
while($row = mysql_fetch_assoc($result3)) {
// Build query to insert vehicle note
$sql = sprintf("INSERT INTO geninfo SET carid = %d, engine = '%s', repair = '%s'", $row['id'], $engine, $note);
mysql_insert($sql);
}

?>

I attached a snapshot of my two tables below. I also had to adjust the result.php to results3.php becuase I already had one in the code. Anyother suggestions? Thanks!!

I think its this part thats messed up...Not positive though..

while($row = mysql_fetch_assoc($result)) {
// Build query to insert vehicle note
$sql = sprintf("INSERT INTO geninfo SET carid = %d, engine = '%s', repair = '%s'", $row['carid'], $engine, $note);
mysql_insert($sql);
}

Yay I got it to work!!!!

One little problem it returns on values for 2011 and populates the table

I have to use $model to filter in this line:

$sql = sprintf('SELECT id FROM VehicleModelYear WHERE model="$model" AND year IN (%s)', implode(', ', $years));

But doing it comes back with an error.....I can use model='mdx' and it works fine, but I cant use the variable...
Any ideas?

Also thanks so much for helping me get this working!!!

Where are you setting the variable value? You'll need to retrieve this from the $_POST array after form submission (assuming there's a corresponding form field of course).

E.g.

$model = isset($_POST['model']) ? $_POST['model'] : '';
$years = isset($_POST['formDoor']) ? $_POST['formDoor'] : array();

$sql = sprintf("SELECT `id` FROM `VehicleModelYear` WHERE `model` = '%s' AND `year` IN (%s)", $model, implode(', ', $years));

I set the variable $year a little further up in the code, if this is what your meaning. I posted the full snippet below:

$years = isset($_POST['formDoor']) ? $_POST['formDoor'] : array();

$sql = sprintf('SELECT id FROM VehicleModelYear WHERE model="'.$model.'" AND year IN (%s)', implode(', ', $years));
$result1 = mysql_query($sql);

$engine = isset($_POST['engine']) ? $_POST['engine'] : '';
$note = isset($_POST['repair']) ? $_POST['repair'] : '';

while($row = mysql_fetch_assoc($result1)) {
// Build query to insert vehicle note
$sql = sprintf("INSERT INTO geninfo SET carid = %d, engine = '%s', repair = '%s'", $row['id'], $engine, $note);
mysql_query($sql);
}
?>

One thing thats kinda intersting is the model = '%s', I used: model="'.$model.'" and somehow got it working, but is one of the things I got stuck on last night. When I get back tonight I am going to try your method of calling variable with %s...

Also another thing I found interesting was I couldnt get this to work:

mysql_insert($sql);
}

to insert into table geninfo one the very last line of the code.. I ended up having to use:

mysql_query($sql);
}
?>

Is this an alright way of doing it?

Thanks!

Where are you setting the variable value?

I was asking where you were setting $model. You need to set a value before you can use it.

One thing thats kinda intersting is the model = '%s'.

The placeholders allow for the creation of prepared SQL statements. This helps to prevent SQL injection attacks.

mysql_insert();

Ignore me. That should have been mysql_query(). I don't usually use the native MySQL functions in PHP. I usually use a database object, so they're wrapped in different method names.

@blocblue
I sure do appreciate you helping me get through this peice of code. Anything I can do for you in the future just let me know!
Thanks again!

commented: No worries +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.