-1

I have tried debugging and cannot seem to get to the bottom of this problem. My query returns successful, however nothing is inserted into my table within my database. I am working on a CRUD application to enter holdings of cryptocurrency, and this is simply the Create button. My function gets to the very end of the if statement, and Mysqli_query returns a 1. Could this be issues with permissions in PHPAdmin? Or possibly something to do with Ports?

The code below:

$con = createDB();

if (isset($_POST['create'])){

    createData();

}






function createData(){
    $username = textboxValue('Username');
    $BTC = textboxValue('BTC');
    $ETH = textboxValue('ETH');/*$ETH =(isset($_POST['ETH']) ? $_POST['ETH'] : '');*/
    $DASH = textboxValue('DASH');

    if($username && $BTC && $ETH && $DASH){
        $sql = "INSERT INTO cryptoholdings(username,BTC_holdings,ETH_holdings,DASH_holdings)
        VALUES('$username','$BTC','$ETH','$DASH')";
        if($GLOBALS['con']->query($sql) ){                                                                  /*(mysqli_query($GLOBALS['con'],$sql))*/
            $GLOBALS['con']->commit();
            echo "Record Successfully inserted...!";        
        }
        else{
            echo "Error Recording Data <br>" . mysqli_error($GLOBALS['con']);
        }
    }
    else{echo "Provide all data in textboxes.";

    }


}

function createDB(){

    $servername='localhost';
    $username='root';
    $password='password';
    $dbname='holdings';
    //create connection to our database "holdings"
    $con=mysqli_connect($servername,$username,$password,$dbname);

    if(!$con){
        die("Connection Failed: ". mysqli_connect_error());
    }
    //create Database
    $sql= 'CREATE DATABASE IF NOT EXISTS $dbname';

    if(mysqli_query($con,$sql)){
        $con = mysqli_connect($servername,$username,$password,$dbname);

        $sql= 'CREATE TABLE IF NOT EXISTS cryptoholdings(
            username VARCHAR(25) NOT NULL,
            BTC_holdings FLOAT(11) NOT NULL,
            ETH_holdings FLOAT(11) NOT NULL,
            DASH_holdings FLOAT(11) NOT NULL)';
        if(mysqli_query($con,$sql)){
            return $con;}
        else{
            echo "Error when Creating Table...";
        }
        

    }
    else{
        echo "Error while creating Database...". mysqli_error($con);
    }
}

function textboxValue($value){
    $textbox = mysqli_real_escape_string($GLOBALS['con'],trim($_POST[$value]));
    if(empty($textbox)){
        return false;
    }
    else{
        return $textbox;
    }



}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jared Benitez
  • 29
  • 1
  • 8
  • `Mysqli_query returns a 1` - I'm thinking you're referring to the createDB function. What does your `createData` function (namely `$GLOBALS['con']->query($sql)`) return? – rjbathgate Nov 26 '20 at 22:58
  • 1
    You don't have to check for mysqli return code manually. Just enable error reporting [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Nov 26 '20 at 22:59
  • $GLOBALS['con']->query($sql)) returns a 1 – Jared Benitez Nov 27 '20 at 17:37

1 Answers1

-4

First check that the connection was very okay by echoing something from the db or by doing sth on the db. 2nd try using another method other than the global con being used. For the purpose of testing and finding solution, I recommend you create a simpler table (of one or 2 fields) and try inserting into the fields. I recommend this method of connection below.

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
smacaz
  • 148
  • 8
  • These are debugging suggestions, not an answer – Wesley Smith Nov 26 '20 at 23:14
  • You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Nov 26 '20 at 23:18
  • I have tried the above code and i still arrive at a "table not found" error, although it definitely exists within my database. – Jared Benitez Nov 27 '20 at 17:31