-1

I have made a code that creates dynamic rows in a form. I am able to store the data in array and display the same using foreach loop but not able to insert into database.

My UI design:

<div class="form-group">
    <label for="eventname"> Income : </label> 
    <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" class="btn btn-info">
    <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" class="btn btn-info">
    <TABLE id="dataTable" width="350px" border="1">
      <TR>
      <TD><INPUT type="checkbox" name="incomechk[]" class="form-control"></TD>
      <TD>
      <SELECT name="incometype[]" class="form-control">
      <OPTION value="emergency">Emergency Fund</OPTION>
      <OPTION value="investments">Investments</OPTION>
      <OPTION value="retirements">Retirements</OPTION>
      <OPTION value="salary">Salary</OPTION>
      <OPTION value="other">Other</OPTION>
      </SELECT>   
      </TD>
      <TD><INPUT type="number" name="incomevalues[]" class="form-control"></TD>
      </TR>
    </TABLE>
    </div>
    <div class="form-group">
    <label for="Description">Expenses : </label>
    <INPUT type="button" value="Add Row" onclick="addRow('annualTable')" class="btn btn-info">
    <INPUT type="button" value="Delete Row" onclick="deleteRow('annualTable')" class="btn btn-info">
    <TABLE id="annualTable" width="350px" border="1">
      <TR>
      <TD><INPUT type="checkbox" name="expensechk[]"  class="form-control"></TD>
      <TD>
      <SELECT name="expensetype[]" class="form-control">
      <OPTION value="food">Food</OPTION>
      <OPTION value="clothing">Clothing and Accessories</OPTION>
      <OPTION value="shelter">Shelter</OPTION>
      <OPTION value="household">Household</OPTION>
      <OPTION value="tranport">Transportation</OPTION>
      <OPTION value="health">Health</OPTION>
      <OPTION value="loans">Loans</OPTION>
      <OPTION value="miscellaneous">Miscellaneous</OPTION>
      <OPTION value="tuition">Tuition</OPTION>
      <OPTION value="taxes">Taxes</OPTION>
      <OPTION value="vacation">Vacation</OPTION>
      <OPTION value="other">Other</OPTION>
      </SELECT>
      </TD>
      <TD><INPUT type="number" name="expensevalues[]" class="form-control"></TD>
      </TR>
    </TABLE>
    </div>
    <button type="submit" class="btn btn-info" name="submit">SUBMIT</button>
  </form>

budgettest.php :

if($bauth['USER'] === $curuser) {
//Income Extraction
$date = $_POST['date'];
$in1 = $_POST['incometype'];
$in2 = $_POST['incomevalues'];
//echo "Incomes : <br/>";
foreach($in1 as $v => $vv){
    echo "into the for loop<br/>";

    $sql1 = "INSERT INTO $curuser (USER,BDATE,BTYPE,BVALUE) VALUES ('$curuser','$date','$in1[$v]','$in2[$v]')";
    $sql2 = mysqli_query($conn,$sql1);
    if($conn->query($sql1)===TRUE) {
        echo "successfully added into $curuser<br/>";
    }
    else {
        echo "not added to database<br/>";
    }
    echo "$in1[$v] "."-"." $in2[$v]";
    echo "<br/>";

}

//Expense Extraction
$exp1 = $_POST['expensetype'];
$exp2 = $_POST['expensevalues'];
//echo "Expenses : <br/>";
foreach($exp1 as $e => $ee){
    $sql2 = "INSERT INTO $curuser (USER,BDATE,BTYPE,BVALUE) VALUES ('$curuser','$date','$exp1[$e]','$exp2[$e]')";
    if($conn->query($sql2) === TRUE) {
        echo "successfully added into $curuser<br/>";
    }
    else {
        echo "not added to database<br/>";
    }

    //echo "$exp1[$e] "."-"." $exp2[$e]";
    //echo "<br/>";
}
}
}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Did you connect to the database? Did that connection create the variable `$con` – RiggsFolly Feb 27 '19 at 15:12
  • 1
    If you are learning PHP, please learn it the right way and build good habits from the start: use prepared statements instead of using PHP variables directly in your SQL. See https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Bill Karwin Feb 27 '19 at 15:16
  • 2
    When you ask a question on Stack Overflow about an error, please include the _exact_ error message, and tell us which line of code produces the error. You have three calls to `query()` in the code you show. We cannot guess which one returns the error you mention. – Bill Karwin Feb 27 '19 at 15:18
  • Maybe not related to your error, but your first INSERT is executed twice. Once with `mysqli_query()` and once with `$conn->query()`. That is unnecessary. You are inserting two rows with the same values. – Bill Karwin Feb 27 '19 at 15:19
  • When an SQL query returns an error, you should log the error like `error_log($conn->error);` so you can get more information about _why_ it failed. See https://secure.php.net/manual/en/mysqli.error.php – Bill Karwin Feb 27 '19 at 15:21

1 Answers1

1

You are executing the query TWICE and using $sql2 is not a query anyway.

$sql1 = "INSERT INTO $curuser 
                (USER,BDATE,BTYPE,BVALUE) 
            VALUES ('$curuser','$date','$in1[$v]','$in2[$v]')";

$sql2 = mysqli_query($conn,$sql1);
if($conn->query($sql1)===TRUE) {

Instead do

$sql2 = mysqli_query($conn,$sql1);
if($sql2 === TRUE) {

NOTE

Your script is wide open to SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements in either the MYSQLI_ or PDO API's

Using prepared and parametised query

$sql1 = "INSERT INTO $curuser 
                (USER,BDATE,BTYPE,BVALUE) 
            VALUES (?,?,?,?)";

$stmt = $con->prepare($sql1);

// I guessed all params were strings ??
$stmt->bind_values('ssss', $curuser,$date,
                           $in1[$v],$in2[$v]);

$res = $stmt->execute();
if ( !$res ) {
    // query failed
    echo error_log( $con->error );
}else {
    // query success
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149