Page 1 of 1

Compare values in mySQL to Names in HTML

Posted: Sun Jan 30, 2011 8:20 pm
by brassfid
I have a form that is dynamically populated with a mysql table. The name of the sets of radio buttons is that of the name in the database.

SQL Database: Pets

[text]ID NAME
1 Birds
2 Cats
3 Dogs[/text]

HTML

Code: Select all

<INPUT TYPE="radio" NAME="Birds" VALUE="0" checked>
<INPUT TYPE="radio" NAME="Birds" VALUE="1">
<INPUT TYPE="radio" NAME="Cats" VALUE="0" checked>
<INPUT TYPE="radio" NAME="Cats" VALUE="1">
<INPUT TYPE="radio" NAME="Dogs" VALUE="0" checked>
<INPUT TYPE="radio" NAME="Dogs" VALUE="1">
So now I would like to pull the values from the radio buttons back into another database. I assume that this is going to be a relational thing

Code: Select all

SELECT * FROM Pets WHERE Name = $Post['Name']
How would I go about creating this database query so I can dynamically pull all the data I need?

Thanks!

Re: Compare values in mySQL to Names in HTML

Posted: Sun Jan 30, 2011 11:16 pm
by requinix
Is that HTML for some kind of search? Here's an easy way:

Code: Select all

<INPUT TYPE="checkbox" NAME="name[]" VALUE="Birds" CHECKED> Birds
<INPUT TYPE="checkbox" NAME="name[]" VALUE="Cats" CHECKED> Cats
<INPUT TYPE="checkbox" NAME="name[]" VALUE="Dogs" CHECKED> Dogs

Code: Select all

$conditions = array();
foreach ((array)$_POST["name"] as $name) {
    $conditions[] = "`name` = '" . mysql_real_escape_string($name) . "'";
}

$query = "SELECT * FROM Pets";
if ($conditions) $query .= " WHERE " . implode(" AND ", $conditions);

Re: Compare values in mySQL to Names in HTML

Posted: Mon Jan 31, 2011 12:01 am
by brassfid
I created radiobuttons dynamically based on my Categories Table and 'catdesc' column.

For all parts of form that have a name that equals Categories.catdesc I want to insert the Name and Value into a new table called "Test". From what you gave me, I have come up with this, however it is not working, I do not think I have the condition correct. So far it inserts one row of zeros into the Test table.

Here is the dynamic insertion PHP:

Code: Select all

$result = mysql_query("SELECT * FROM Categories ORDER BY catdesc");
while ($array = mysql_fetch_array($result)) {
 
 echo '<table border="1">
<tr>
    <td width = "36%">
	<b>'.$array['catdesc'].'</ b>
    </td>
    <td width = "16%" align=center>
	<INPUT TYPE="radio" NAME="name['.$array['catdesc'].']" VALUE="0" checked>
	<font size="2">None</font>
    </td>
    <td width = "16%" align=center>
	<INPUT TYPE="radio" NAME="name['.$array['catdesc'].']" VALUE="1">
	<font size="2">Mild</font>
    </td>
    <td width = "16%" align=center>
	<INPUT TYPE="radio" NAME="name['.$array['catdesc'].']" VALUE="2">
	<font size="2">Med.</font>
    </td>
    <td width = "16%" align=center>
	<INPUT TYPE="radio" NAME="name['.$array['catdesc'].']" VALUE="3">
	<font size="2">Strong</font>
    </td>
    </tr>
</table>';
 
 }
Here is my attempt at moving the all all data even "0" into the database:

Code: Select all

$conditions = array();
foreach ((array)$_POST["name"] as $name) {
    $conditions[] = "`Categories.catdesc` = '" . mysql_real_escape_string($name) . "'";
}

$query = "SELECT * FROM Categories";
if ($conditions) $query .= " WHERE " . implode(" AND ", $conditions);

$sql2 = "INSERT INTO Test (Variable)
VALUES
('$name')";

if (!mysql_query($sql2,$con))
  {
  die('Error: ' . mysql_error());
  }