2

I'm currently creating a dummy website under the scenario of an animal adoption agency, where I need to creating a HTML table that displays all animals available for adoption and allow the user to send an adoption request out to be approved by staff.

I have created this table using PHP while loops and have echoed a button on each row that will send the adoption request out to be approved by staff. my issue is that the buttons don't work for their specific row and when pressed sends off a request for all available animals and not for their specified row.

How do I make each button specific to their row?

Here is my code:

    <tr>
    <th>Name</th>
    <th>Type</th>
    <th>DateOfBirth</th>
    <th>Description</th>
    <th>Photo</th>
    <th>Available</th>
    <th>Owner</th>
    <th>Adopt</th>
<tr>

<?php
error_reporting(E_ALL ^ E_DEPRECATED);//put in place due to previous error: Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO.

$hostname="localhost"; //local server name default localhost
$username="root";  //mysql username default is root.
$password="";       //blank if no password is set for mysql.
$database="dc2410";  //database name which you created

mysql_connect($hostname, $username, $password);
mysql_select_db($database);

$sql=mysql_query("SELECT * FROM `animal` WHERE `available` = 'Yes'");

while($animal=mysql_fetch_assoc($sql)){

    echo"<tr>";

    $animal['animalID'];
    echo "<td>".$animal['name']."</td>";
    echo "<td>".$animal['type']."</td>";
    echo "<td>".$animal['dateofbirth']."</td>";
    echo "<td>".$animal['description']."</td>";
    echo "<td>"?> <img src="<?php echo $animal['photo'] ?>" width="100px" height="100px"/> <?php "</td>";
    echo "<td>".$animal['available']."</td>";
    echo "<td>".$animal['owner']."</td>";
    ?>
    <td><form><input type="submit" value="Adopt" name="adopt" onClick="
    <?php

    $req=mysql_query("INSERT INTO adoptionrequest(userID, animalID, approved) VALUES ('1','".$animal['animalID']."','Awaiting Approval')");

     ?>
     " ></form></td>
    <?php
    echo"<tr>";

}

?>

</table>
2
  • 2
    onClick=" <?php $req=mysql_query("INSERT INTO adoptionrequest(userID, animalID, approved) VALUES ('1','".$animal['animalID']."','Awaiting Approval')"); ?> " that's not how it works, this code well be executed when generating the html. Commented May 12, 2015 at 20:16
  • @Cthulhu but what a nice idea. I have never seen something like this before :-) The OP should really read some tutorials to understand the difference of server-side and client-side code. Commented May 12, 2015 at 20:18

3 Answers 3

1
$sub = intval($_POST['sub']);
$selected = intval($_POST['selected']);
if ($sub == 1){
    mysql_query("INSERT INTO adoptionrequest(`userID`, `animalID`, `approved`) VALUES ('1','$selected','Awaiting Approval')");
    if(mysql_errorno() > 0){echo mysql_error();}
}


echo '<form action="#" method="post" ><input type="hidden" name="sub" value="1"><table>';
$results = mysql_query("SELECT `animalID`, `name`,`type`,`dateofbirth`,`description`,`photo`,`available`,`owner` FROM `animal` WHERE `available` = 'Yes'");
while($animal=mysql_fetch_assoc($results, MYSQL_NUM)){
  echo <<<EOT
<tr><td>$animal[0]</td>
<td>$animal[1]</td>
<td>$animal[2]</td>
<td>$animal[3]</td>
<td>$animal[4]</td>
<td><img width="100px" height="100px" src="$animal[5]" alt="$animal[1]"/></td>
<td>$animal[6]</td>
<td>.$animal[7]</td>
<td><button type="submit" name="selected" value="$animal[0]"/></td></tr>
EOT;

}
Sign up to request clarification or add additional context in comments.

Comments

0

As Cthulhu mentioned, putting PHP code into your onClick won't work as it appears you think it will. What you need to do is change the form such that it submits a value (probably a hidden input field) back to the PHP page which would then handle the value by inspecting the $_POST variable

Start by reading this: http://www.w3schools.com/htmL/html_forms.asp

Then read this: http://php.net/manual/en/reserved.variables.post.php

And this: http://php.net/manual/en/tutorial.forms.php

Comments

0

It is not that "when pressed sends off a request for all available animals", it is that in your loop you're sending a request for all availible animals. An onClick statement followed by some PHP code won't execute that code when you press the button, it will execute it when you actually load the page, because PHP is server-side, so let me suggest some further learning.

That said, what you need to do is create a separate PHP file that handles the submits (you may do it in the same file, but this is easier to understand) and make a hidden field with the animalID on each of your forms, and then in the other file read that hidden field and execute your MySQL query.

Also let me warn you: the MySQL class is deprecated in PHP, use MySQLi or PDO instead and make something with SQL Injection, your code is fairly insecure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.