Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,155,527 members, 7,826,980 topics. Date: Tuesday, 14 May 2024 at 02:05 AM

Can Not Post Data Into Database - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Can Not Post Data Into Database (1281 Views)

Please How Do I Print This Data From MY Database. MYSQL, PHP / How Can I Insert Registration Data Into MYSQL Database After Checking Checkbox (2) (3) (4)

(1) (Reply) (Go Down)

Can Not Post Data Into Database by usman4people: 7:13am On Nov 17, 2016
I write to seek your kind assistant in my Project of student registration. after echoing the table data, the student is expected to select the course he wish and the record will be sent to database. but I only receive the student Id with out other parameters. Below is the codes:

<div class="span10">



<div class="alert alert-info"><Strong>Registered Courses!</strong>&nbsp;Welcome to Online Grade Inquiry</div>



<form class="form-horizontal" method="POST" action="">
<?php
include('dbcon.php');

$q = "SELECT status FROM invoice WHERE student_no = '$session_id'";
$result = mysqli_query ($db, $q);
if (mysqli_num_rows($result) > 0){

$qry = mysqli_query($db,"SELECT course, year_level FROM students WHERE student_no = '$session_id'"wink or die("Error: ".mysqli_error($db));

while($row1 = mysqli_fetch_array($qry)){
$course_id = $row1['course'];
$level = $row1['year_level'];

$query_course = mysqli_query($db, "SELECT * FROM subject where course_id = '$course_id' AND year = '$level' "winkor die(mysqli_error());


echo"<table class='table table-hover table-striped table-responsive'>";
echo "<tr><th>Course COde</th><th>Title</th><th>Status</th><th>Unit</th><th>Check</th></tr>";

while($row = mysqli_fetch_array($query_course)) {
$code = $row['code'];
$title = $row['title'];
$status = $row['status'];
$unit = $row['unit'];
$amm = $row['subject_id'];


$tick ="<input name='check[]' value='<?php echo $amm; ?>' id='coursecode' rel = '$unit' type='checkbox' />";
$unt ="<input name='unit[]' value='<?php echo $unit; ?>' id='coursecode' rel = '$unit' type='hidden' />";


echo"<tr>";

echo "<td>" .$row['code']. "</td>";

echo "<td>" .$row['title']. "</td>";

echo "<td>" .$row['status']. "</td>";
echo "<td>" .$row['unit']. "</td>";



echo "<td>"."<input name='check[]' value='<?php echo $amm; ?>' id='coursecode' rel = '$unit' type='checkbox' />"."</td>";

echo "</tr>";


}
}


echo"</table>";


mysqli_close($db);

}else{

echo '<h2>System Error</h2>
<p class="error">Registration failed because of a system error. We apologize for the inconvenience.</p>';

}

?>
<button type='submit' class='btn btn-info btn-lg' name = 'submit' >Register</button>
</form>

<div class="alert alert-info pull-right" ><Strong>Registered Units!</strong>&nbsp;<span id="output"></span> </div>


</div>

</div>
</div>
</div>




<?php

include('dbcon.php');

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

$counter = count($_POST["check"]); /* COUNT THE PASSED ON NAME */

for($x=0; $x<=$counter; $x++){

if(!empty($_POST["check"][$x])){

$subj = $_POST["check"][$x];
// $sub = $_POST["check"][$x];
// $un = mysqli_real_escape_string($_POST[".$row['unit']."][$x]);
$student_id = $session_id;

$qry="INSERT INTO registration (course, student) VALUES ('$subj','$student_id')";
mysqli_query($db, $qry);

} /* END OF CHECKING THE CHECKBOX IF SELECTED */

}
} /* END OF FOR LOOP */

?>

Re: Can Not Post Data Into Database by usman4people: 10:16am On Nov 17, 2016
Please help me Professional Programmers, I am Newbee[color=#990000][/color][font=Lucida Sans Unicode][/font]
Re: Can Not Post Data Into Database by Whytelyon(m): 8:58pm On Nov 17, 2016

Need to make my fonts a bit to match the page. You fùck up the code and it's ethics the way you go about it.
Follow this example and see the codes insert


<?php
$host = "127.0.0.1";
$user = "root";
$pass = "";
$db = "peopledata";

$con = mysqli_connect($host,$user,$pass,$db) or die ('cannot connect ');

$fname = mysqli_real_escape_string($con,$_POST['firstname']);
$lname =mysqli_real_escape_string($con,$_POST['lastname']);
$email = mysqli_real_escape_string($con,$_POST['email']);

$sql = "INSERT INTO infoz (firstname,lastname,email) VALUES ('".$fname."', '".$lname."', '".$email."')";

$query = mysqli_query($con,$sql);
If(!$query)
{
echo " Failed Query";
}
else {
echo " 1 Result inserted successfully";
}

mysqli_close($con);

?>


I'm coding from my mobile phone, but this should give you an idea of what it looks like. I suggest you try understanding codes better and watch videos on YouTube in order to have a clear understanding of what you are doing.

Either you use PDO or Mysqli and prepared statements to shield yourself from mysql injection.

The mysqli_real_escape_string() is used also to shield third party intrusion from hackers and the likes. As errors could trigger reading info from db as well.

Select statements are used mainly in the getting of information from the RDBMS (mysql,oracle, db2, etc)
Do have a good time learning.

1 Like 1 Share

Re: Can Not Post Data Into Database by usman4people: 4:19pm On Nov 18, 2016
Hello, Thanks for your kind response, I am looking for how to insert array of data with checkbox into mysql database.
Re: Can Not Post Data Into Database by BestbrainMaven(m): 4:20am On Nov 21, 2016
try pdo, I will copy you my crud class and change the way you loop
Re: Can Not Post Data Into Database by kudaisi(m): 3:18pm On Nov 21, 2016
usman4people:
Hello, Thanks for your kind response, I am looking for how to insert array of data with checkbox into mysql database.

Your question is not clear, but let me try from what I understand:

I imagine you have a tables as so:

student_subjects Table Structure
___________________________
| id | subject_id | student_id |
------------------------------------
|___|__________|___________|
|___|__________|___________|

You have an array variable say $check that holds all the subject that were checked
And another variable $student_id which holds the ID of the student in context of your code.
Then the $conn variable which I assume is a mqsqli connection to your database

A code as simple as below should do the trick (note the code is without error checking or performance considerations).
<?php
$check_count = count($check);
for($i=0; $i < $check_count; $i++)
{
$sql = "INSERT into student_subjects (subject_id, student_id) VALUES ('".$check[$i]."','".$student_id."' ) " ;
$conn->query($sql)
}
?>

(1) (Reply)

Develop Applications That Can Perform Mathematical Operations And Get Paid / I Can Build Any Of Your Final Year Project / Young Wordpress Developer Living In Ikeja

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.