0

[!]problem: whenever I get the data from frontend and try to insert into database...the data are inserted as a single alphabet in elective cell...but what I really needed that the whole data to be inserted in the elective cell with separated by comma(,)

1

<?php
include_once '../../config/db_connection.php';
include_once '../../config/functions.php';
include "autho.php";
include('../db_mysqli.php');
if (isset($_POST['submit'])) {
$username1 = $_POST['username'];
$username1 = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $username1);
$rollno = $_POST['register_no'];
$rollno = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $rollno);

$subjectcode = implode(',', $_POST['subject']);   ;

date_default_timezone_set("Asia/Kolkata");

$today = date('g:ia \o\n l jS F Y');

$rollnos = array_map('strlen', $rollno);
$rollnos = min($rollnos);
if ($rollnos < 6) {
    $error = 'Roll Number is too short.';
} else {
}
if (!isset($error)) {
    for ($i = 0; $i < count($rollno); $i++) {
$sql = "UPDATE students SET elective='$subjectcode[$i]' WHERE register_number='$rollno[$i]'";
$result = mysqli_query($conn, $sql);
if ($result) {
header('Location:./?edu_camp=elective_student_update&success');

        } else {
             header('Location:./?edu_camp=elective_student_update&fail');
        }
    }

} else {
    //echo "Sorry! something Wrong.";
}

}
?>
3
  • 1
    implode might be what you are looking for. Commented Apr 10, 2018 at 10:27
  • 1
    NOTE: Posting images in your question isn't very useful, especially when the image has been reduced to an unreadable size. Commented Apr 10, 2018 at 10:33
  • Before you update the row in the table, you need to build the subjects as a string. So, build the string in your for loop, then after the for loop, perform your update. Commented Apr 10, 2018 at 10:34

1 Answer 1

2

As the comments mentioned, you can implode your array into a string an insert it (docs).

Also, you're using MySQLi, but not using bound parameters which you REALLY should be using (docs).

// Concatenate all values together
$commaSeparatedList = implode(',',$subjectcode);
// Prepare your statement
$stmt = $mysqli->prepare("UPDATE students SET elective=? WHERE register_number=?");
// Bind the relevant parameters (not sure what the where clause should be here)
$stmt->bind_param('si', $commaSeparatedList, $i);
// Check if rows were affected
if ($mysqli->affected_rows > 0) {
   // Success, rows were changed
}
// Execute and close the statement object
$stmt->execute();
$stmt->close();
Sign up to request clarification or add additional context in comments.

2 Comments

already I used implode(',',$subjectcode);....but not working...whether the for loop is creating a problem..............?
You're changing variables. First you were using $subjectcode as an array, now after the edit, you're trying to access each element of a string in your query. So I'm not sure which variables you should be using where. Perhaps comment your code with what each intended line is supposed to be doing, so people can understand it better?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.