1

A little bit stuck here (and a newbie) I'm trying to insert data from arrays into database, but not sure how to extract it: `

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

// Determine how many categories there are
$cnt = count($_POST['cat']);
$insertArr = array();

for ($i = 0; $i<$cnt; $i++){
$insertArr[] = "('" . ($_POST['cat'][$i]) . "', '" . ($_POST['grade'][$i]) . "')";
}

    $q = "INSERT INTO grit (feed, grade) VALUES ('$insertArr[$i]', '$insertArr[$i]')";
    $stmt = mysqli_prepare($dbc, $q);

    // For debugging purposes:
   if (!$stmt) echo mysqli_stmt_error($stmt);
    //mysqli_stmt_bind_param($stmt, 'ss', $final_[0], $final_[1]);

    // Execute the query:
    mysqli_stmt_execute($stmt);

    if (mysqli_stmt_affected_rows($stmt) >= 1) { // If it ran OK.

        // Print a message:
        echo '<h4>feedback product has been added!</h4>';

        // Clear $_POST:
        $_POST = array();


    } else { // If it did not run OK.
        trigger_error('feedback could not be added due to a system error. We apologize for any inconvenience.');
    }

}
`

The arrays seem to be fine, but nothing get passed to the database.

3
  • post your html content. Commented Mar 19, 2014 at 10:33
  • use implode(',', $insertArr); Commented Mar 19, 2014 at 10:35
  • Can you post the content of $q? Commented Mar 19, 2014 at 10:43

3 Answers 3

1
for ($i = 0; $i<$cnt; $i++){
     $insertArr[] = "('" . ($_POST['cat'][$i]) . "', '" . ($_POST['grade'][$i]) . "')";
}

like already mentioned, you are already ending the for loop here, while you use the loop index in the query.

Just one thing to add: You should escape your queries, otherwise, people can write weird things into your database, and maybe destroy your datas with it.

for example, use: mysql_real_escape_string

Sign up to request clarification or add additional context in comments.

Comments

0

Your query should read :

$q = "INSERT INTO grit (feed, grade) VALUES ('" . $insertArr[$i]. "', '". $insertArr[$i] ."')";

Plus it is out of the loop and you're using the loop index...

Try that :

$mysqli = new mysqli("localhost", "CHANGE", "CHANGE", "stackoverflow");

// This is my test array, remove that
$_POST = array ('cat' => array('meowy', 'kitty'), 'grade' => array(1,2));

$cnt = count($_POST['cat']);

for ($i = 0; $i<$cnt; $i++){

    $q = "INSERT INTO grit (feed, grade) VALUES ('" . $_POST['cat'][$i] . "', '" . $_POST['grade'][$i] . "')";
    $stmt = $mysqli->prepare($q);

    // For debugging purposes:
    if (!$stmt) echo mysqli_stmt_error($stmt);

    // Execute the query:
    $stmt->execute();

    if (mysqli_stmt_affected_rows($stmt) >= 1) { // If it ran OK.

        // Print a message:
        echo '<h4>feedback product has been added!</h4>';

    } else { // If it did not run OK.
       trigger_error('feedback could not be added due to a system error. We apologize for any inconvenience.');
    }
}

ATTENTION : Keep in mind that you have to sanitize data from the superglobal POST array. Do never use data from users without making sure that data is what you actually expect ! You should improve your code on that matter.

1 Comment

It will work with the present way too. Maybe this can be used for more clarity.
0
if(isset($_POST['timetab'])){
    $cos=$_POST['course'];
    $dat=$_POST['date'];
    $tim=$_POST['time'];
    $c=count($cos);
    /*a for loop for each value per column . It also works as a multi-row insertion*/
    for($i=0;$i<=$c;$i++)
    {
        $sql="INSERT INTO timetable (course_module, date, time) VALUES('$cos[$i]', '$dat[$i]', '$tim[$i]')";
        $ins=mysqli_query($conlog, $sql);       
    }
    echo(!mysqli_affected_rows($conlog))?"QUERY WRONG":"QUERY SUCCESS";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.