title: How can I put a donation into a MySQL db from a PHP form?
body: <p>I'm quite new to PHP and MySQL, and i desperately need some help. I've made a register page that connects to a db in MySQL, which has a foreign key for id in my donate db. So a user has to sign up before they can donate. But for some reason, I cannot figure out how to add the donations to the donate db. The form only has a donate amount value that the user can enter, and also a radio input where they can choose which animal they would like to donate to. So basically I need to add the donations to the donate db. Any help would be appreciated.</p>
  <pre><code>&lt;?php
  
  
  if(isset($_SESSION['userId'])){?&gt;
  
  
  &lt;div class=&quot;col-sm-8&quot;&gt;
              &lt;p&gt;&lt;form name=&quot;form2&quot; method=&quot;POST&quot;&gt;
           &lt;div class=&quot;form-group&quot;&gt;
             &lt;input type=&quot;text&quot; name=&quot;value&quot; class=&quot;form-control&quot; id=&quot;value&quot; placeholder=&quot;Donation amount*&quot; required &gt;
            &lt;/div&gt;
  &lt;div class=&quot;form-group&quot;&gt;
  Animal:&lt;/br&gt;&lt;/br&gt;
  &lt;input type=&quot;radio&quot; name=&quot;animal&quot; value=&quot;panda&quot;&gt; Panda&lt;/br&gt;&lt;/br&gt;
  &lt;input type=&quot;radio&quot; name=&quot;animal&quot; value=&quot;tiger&quot;&gt; Tiger&lt;/br&gt;&lt;/br&gt;
  &lt;input type=&quot;radio&quot; name=&quot;animal&quot; value=&quot;elephant&quot;&gt; Elephant&lt;/br&gt;&lt;/br&gt;
  &lt;input type=&quot;radio&quot; name=&quot;animal&quot; value=&quot;polarbear&quot;&gt; Polar Bear&lt;/br&gt;&lt;/br&gt;
  &lt;input type=&quot;radio&quot; name=&quot;animal&quot; value=&quot;koala&quot;&gt; Koala&lt;/br&gt;&lt;/br&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;button type=&quot;submit&quot; name=&quot;donate&quot; class=&quot;btn btn-secondary&quot;&gt;DONATE&lt;/button&gt;
  &lt;/form&gt;
  &lt;?php
  }
  
  
  else{
  </code></pre>
  <p>what's here isn't important</p>
  <p>this bit is important</p>
  <pre><code>} if(isset($_POST['donate'])){
  
  $id = $_GET['userId'];
  $value  = $_POST['value'];
  $animal = $_POST['animal'];
  $stmt = mysqli_stmt_init($con);
  if(!mysqli_stmt_prepare($stmt, $sql)){
  echo '&lt;script&gt;alert(&quot;SQL mistake.&quot;);&lt;/script&gt;';
  exit();
  }
  else{
  if ($con-&gt;query($sql) === TRUE) {
  $res = mysqli_query($con,&quot;SELECT id FROM users WHERE id = 'id'&quot;);
  while ($rows = mysqli_fetch_array($res)) {
  $id = $rows['id'];
  $sql = &quot;INSERT INTO donate (value, animal, id) VALUE (?, ?, ?)&quot;;
  }}}
  </code></pre>
  <p>I hope what I've written isn't too dumb, sorry if it is. But if anyone can help me with this, it would be appreciated. Thank you.</p>