0

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.

<?php


if(isset($_SESSION['userId'])){?>


<div class="col-sm-8">
            <p><form name="form2" method="POST">
         <div class="form-group">
           <input type="text" name="value" class="form-control" id="value" placeholder="Donation amount*" required >
          </div>
<div class="form-group">
Animal:</br></br>
<input type="radio" name="animal" value="panda"> Panda</br></br>
<input type="radio" name="animal" value="tiger"> Tiger</br></br>
<input type="radio" name="animal" value="elephant"> Elephant</br></br>
<input type="radio" name="animal" value="polarbear"> Polar Bear</br></br>
<input type="radio" name="animal" value="koala"> Koala</br></br>
  </div>
</div>
<button type="submit" name="donate" class="btn btn-secondary">DONATE</button>
</form>
<?php
}


else{

what's here isn't important

this bit is important

} 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 '<script>alert("SQL mistake.");</script>';
exit();
}
else{
if ($con->query($sql) === TRUE) {
$res = mysqli_query($con,"SELECT id FROM users WHERE id = 'id'");
while ($rows = mysqli_fetch_array($res)) {
$id = $rows['id'];
$sql = "INSERT INTO donate (value, animal, id) VALUE (?, ?, ?)";
}}}

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.

Yes
  • 1
  • 2
  • What do you need help with? It's a little bit unclear what is not working – Dharman Jun 21 '20 at 18:01
  • No offense, but this code makes no sense. Why do you have `$con->query` and then `mysqli_query` and also `mysqli_stmt_prepare`. What are you trying to do there? Why are you not using PDO? – Dharman Jun 21 '20 at 18:02
  • hi. yeah I know the code makes no sense, I actually commented some of it out but I thought I'd leave it in incase some of it is useful. I took a class and they didn't teach us PDO there. – Yes Jun 21 '20 at 18:07
  • That must have been a bad class then. Check this article out. It should teach you what you need to know. https://phpdelusions.net/pdo – Dharman Jun 21 '20 at 18:09
  • so what I'm trying to do is after the user signs in, they can donate. Their id would tell me who they are, and how much they're trying to donate. the id that is used is in the register db, and also in the donate db. this is how I can link them together, using a foreign key. So what I'm trying to do is take the amounts they put in into the donate db, but it isn't working. I'm not sure if it's because of the foreign key or what. hope this helps – Yes Jun 21 '20 at 18:11
  • This might also help you: https://stackoverflow.com/questions/60741213/how-can-i-get-an-unknown-username-given-an-id – Dharman Jun 21 '20 at 18:13
  • thanks, i'll definitely check it out in the future, but i can't afford to write the whole config, registration and donate forms out again using pdo due to time constraints – Yes Jun 21 '20 at 18:14
  • Where does `$_GET['userId']` come from? What is `'email'`? – Dharman Jun 21 '20 at 18:16
  • Why do you have two inserts and in one you have `id` and in the other you have `userId`? – Dharman Jun 21 '20 at 18:16
  • some of it is originally in another language, I tried to convert it into english and i messed it up. it is the same in the original code – Yes Jun 21 '20 at 18:18
  • Why do you have that SELECT in the middle? Is it important? What are you trying to SELECT? – Dharman Jun 21 '20 at 18:20
  • honestly, i don't know. I've spent so long looking at this screen and i threw everything on it, I just don't have a clue how to fix it. I know it's wrong, but I honestly just don't know how to fix it. im trying to get the user id so that i know which user is donating, then the next part is what i really dont understand. how do i link the 2 id-s from the 2 dbs together? or is that not even necessary because of the foreign key? thats why im trying to select the id of the user – Yes Jun 21 '20 at 18:27
  • I think you mean two tables in the same database. How do you want to link them and why? It looks like you have the id of the user saved in a session variable, correct? `$_SESSION['userId']`. Why do you need to select it again from the database? Why do you have `$_GET['userId']`? I can't help you if I do not understand what you are trying to do. What are your tables and their columns? – Dharman Jun 21 '20 at 18:30
  • because when i tried to normally insert the values into the db, it always came up as SQL mistake. that's why i thought maybe it's the SQL not knowing which id it is – Yes Jun 21 '20 at 18:30
  • so there are 2 databases. the first is for registration. it has the id which is auto incremented, the name of the person, email and password. the second db is the donation one. it has an OrderId which is auto incremented, a value(the amount someone has donated), the animal they chose with the radio input and the id of the person which is the foreign key. this links to the id in the first db – Yes Jun 21 '20 at 18:33
  • yh sorry 2 tables in the database – Yes Jun 21 '20 at 18:35

1 Answers1

0

I assume that if your user is logged in then you would have saved their ID in a session variable. This means you do not need to select anything from the users table. You can just insert directly into a donation table, which is a simple three lines of code.

$value  = $_POST['value'];
$animal = $_POST['animal'];

$stmt = $con->prepare("INSERT INTO donate (`value`, animal, userId) VALUES(?, ?, ?)");
$stmt->bind_param('sss', $value, $animal, $_SESSION['userId']);
$stmt->execute();

That's it. You do not need any if statement other than some sanity checks e.g. check that the value is > 0.

It is possible you were confused by a lack of error message, because it looks like you have error reporting silenced. You need to enable it. Here is How to get the error message in MySQLi?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • oh wait. what does this mean? Fatal error: Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\szakdolgozat\tamogatas.php:117 Stack trace: #0 {main} thrown in C:\xampp\htdocs\szakdolgozat\tamogatas.php on line 117 – Yes Jun 21 '20 at 19:00
  • its this line $stmt->bind_param('sss', $value, $animal, $_SESSION['userId']); – Yes Jun 21 '20 at 19:01
  • See the link at the end. You need to enable error reporting. You are probably only missing this line `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before `mysqli_connect()` – Dharman Jun 21 '20 at 19:03