-2

Here's my code on inserting data. I'm trying to insert data to two tables, which is the donor and donation, but suddenly it won't show in my database, I can't figure out what is the problem, please help.

<!DOCTYPE html>
<html>
<head>
<?php
  include "queries.php";
  include "header.php";
  ?>
</head>
        <body>
        <?php
if(isset($_POST['submit'])){
  $donor_type = $_POST['donor_type'];

  $donor_type = $_POST['donor_type'];
  if(empty($donor_type)){
      $errordonor_type="not existing";
    }
  $con_num = $_POST['con_num'];
  if(empty($con_num)){
    $errorcon_num="please input your number";
    $name1 = $_POST['name1'];
    if(empty($name1)){
    $errorname1="please enter your name";}
  }
  $categor_type = $_POST['categor_type'];
  if(empty($categor_type)){
    $categor_type="not existing";}
    $location = $_POST['location'];
  if(empty($location)){
    $errorlocation="please enter your location";}
    $date = $_POST['date'];
  if(empty($date)){
    $errordate="not exist";}
    $amount = $_POST['amount'];
  if(empty($amount)){
    $erroramount="please specefic amount";}
  $withdrawal = $_POST['withdrawal'];
  if(empty($withdrawal)){
    $errorwithdrawal="please choose";}
if(!isset($errordonor_type) || !isset($errorcon_num) || !isset($errorname1) || !isset($errorcategor_type) || !isset($errorlocation)|| !isset($errordate)||!isset($erroramount)|| !isset($errorwithdrawal));
{
$insert=mysqli_query($conn,"insert into donor(donor_type, con_num, name1, categor_type, location, date,  amount, withdrawal)values('$donor_type','$con_num','$name1','$categor_type','$location','$date','$amount','$withdrawal')");
echo "<script>alert ('created account');window.location.href='login.php'</script>";
}
}
  
?>
Geshode
  • 3,600
  • 6
  • 18
  • 32
  • Have you checked, that the if clause is entered? That all the variables are set? – Geshode May 11 '23 at 01:42
  • yes , I've already check it my friend – Mohammad Sali Jauhari May 11 '23 at 01:45
  • (1) Please change your queries to parameterized prepared statement which is resilient against SQL injection. (2) From the code you posted, it only has a statement to insert data into the donor db table, there is **no** statement to insert into donation db table (3) Everything happens for a reason. There should not be something like "suddenly" it does not work, so please tell us --- what did you change ? – Ken Lee May 11 '23 at 02:02
  • $insert_donor = mysqli_query($conn, "INSERT INTO donor(donor_type, con_num, name1) VALUES ('$donor_type', '$con_num', '$name1')"); $insert_donation = mysqli_query($conn, "INSERT INTO donation(categor_type, location, date, amount, withdrawal) VALUES ('$categor_type', '$location', '$date', '$amount', '$withdrawal')"); ----- I've change insert state to this – Mohammad Sali Jauhari May 11 '23 at 03:45

1 Answers1

-2

Have you tried using the mysqli_error() function? This will identify if there are any SQL errors and should return a string description of any last error.

Example Usage:

$query = "SELECT * FROM categories";
mysqli_query($database_connection, $query);
$error = mysqli_error($database_connection);
print $error; 

I've written the above code in a non-paramaterised query format for simplicity, however this is not a recommended nor secure practice. It's best to learn how to use paramaterised queries.

Parameterized queries in PHP with MySQL connection

For more information on mysqli_error():

https://www.php.net/manual/en/mysqli.error.php

Zephyr
  • 5
  • 4
  • After your database query, paste the mysqli_error() function. You must provide the database connection information as a parameter in the function. Do note that this function should only be used during the testing phase of your website and the information returned by this function can be sensitive by returning information about your database. For this reason, remove this function call from a live website. – Zephyr May 11 '23 at 02:00
  • There is also a semicolon after the last if statement which is incorrect syntax. – Zephyr May 11 '23 at 02:09
  • 3
    You should never use mysqli_error. its a bad and outdated practice. – Your Common Sense May 11 '23 at 05:11
  • I tend to use a custom error handler however I will not overwhelm a new programmer with this. I cannot find where mysqli_error() is deprecated to be considered a “bad” practice. How is this function a “bad” practice to provide for someone beginning programming who needs a simple answer to their SQL error? Please provide more information and assistance to the individual asking the question. – Zephyr May 11 '23 at 07:09
  • Please don't recommend people use `mysqli_error` without a good reason. The automatic mysqli error reporting is far superior. – Dharman May 11 '23 at 08:30