Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,171,628 members, 7,882,126 topics. Date: Saturday, 06 July 2024 at 04:22 PM

Help, Cant Insert Data Into MYSQL Database Table - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Help, Cant Insert Data Into MYSQL Database Table (1461 Views)

How Can I Insert Data From A HTML Table Into Database? / How To Upload An Image Into Mysql Database / How To Upload Mysql Database To Server (2) (3) (4)

(1) (Reply) (Go Down)

Help, Cant Insert Data Into MYSQL Database Table by neowelsh(m): 3:10pm On Dec 02, 2014
hello webmasters, Merry Xmas in advance. Am developing a mini project(kind of course registration portal) for my department, where all the courses, are on a table in the database, in other to register this courses, a student must select a level, depending on the level selected, courses are retrieved from the database.

this is where my problem starts, have been able to display all the courses from the database on a page, but could not insert the ticked courses into the register table on the database, please i need help.

should i paste my codes? i think am getting something wrong.

thanks in Advance
Re: Help, Cant Insert Data Into MYSQL Database Table by DualCore1: 4:08pm On Dec 02, 2014
In the register table, have a varchar field called 'registered_courses'.

When a user ticks all the applicable courses, when processing that form, build all the ticked course IDs into an array. Store the array in the registered_courses table.

For instance the array could look like '25, 65, 45, 68'.

When you need to use the data stored, bear in mind it will not be returned to you as an array. It will be returned to you as a string, you can then 'explode' the data into an array and use as desired.
Re: Help, Cant Insert Data Into MYSQL Database Table by maekhel(m): 5:21pm On Dec 02, 2014
neowelsh:

should i paste my codes? i think am getting something wrong.
Yes paste your codes, will make it easier to point u in d right direction
Re: Help, Cant Insert Data Into MYSQL Database Table by alabi10: 5:24pm On Dec 02, 2014
past ur code so that someone can help u. You can email the code to me on alabi10@yahoo.com to inspect and see how I can help you.
Re: Help, Cant Insert Data Into MYSQL Database Table by neowelsh(m): 1:29pm On Dec 03, 2014
Thank you very much for your replies, below are the codes with screen shots. the first one is is the page where a student is suppose to select his/her level, followed is the script.

1. <table>
<tr>
<td><strong>SURNAME:</strong></td>
<td><?php echo $lastname?></td>
<td><strong>&nbsp;OTHERNAMES:</strong></td>

<td><?php echo $othername?></td></tr>
<tr><td></td></tr>
<tr>
<td><strong>MATRIC NUMBER:</strong></td>
<td><?php echo $matno?></td>
<form action="Precourse_reg.php" method="post" >
<td>&nbsp;<label><strong>SESSION:</strong>
<select name="session" id="sesion">
<option selected>Select...</option>
<option>2009/2010<option>
<option>2010/2011</option>
<option>2011/2012</option>
<option>2012/2013</option>
<option>2013/2014</option>
<option>2014/2015</option>

</select></label></td></tr>

<tr><td><label><strong>LEVEL:</strong>
<select name="level" id="level">
<option selected>Select...</option>
<option>100</option>
<option>200</option>
<option>300</option>
<option>400</option>
<option>500</option>
<option>500+</option>

</select></label> </td>
<td><input type="submit" name="submit" value="SUBMIT"/></td></tr></form>


BELOW IS WHAT HAPPEN HAS SOON AS YOU SELECT YOUR LEVEL AND SESSION


<form action="performreg.php" method="post">
<table><tr>
<td><strong>SURNAME:</strong></td><td><label id="lastname" name="lastname"><?php echo $lastname ?></label></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><strong>OTHERNAMES:</strong></td><td id="othername"><label id="othername" name="othername" ><?php echo $othername ?></label></td></tr>


<tr>
<td><strong>MATRIC NUMBER:</strong></td><td><input type="text" id="matno" name="matno" value="<?php echo $matno ?>"></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><strong>SESSION:</strong></td><td><input type="text" id="session" name="session" value="<?php echo $session ?>"></td></tr>
<tr>
<td><strong>LEVEL:</strong></td><td id="level"><input type="text" id="level" name="level"value="<?php echo $level ?> "></td></tr>
<tr></tr>
</table>

<?php
$asql = mysql_query("SELECT courses.CourseCode, courses.CourseTitle, courses.CourseUnit, courses.CourseStatus, courses.SemesterTaken, lecturer.Title, lecturer.Firstname, lecturer.Lastname FROM courses LEFT JOIN lecturer ON courses.LecturerID = lecturer.LecturerID WHERE courses.Level = '$level'"wink;


echo "<table border='2'><tr ><th>COURSE TITLE</th><th >COURSE UNIT</th><th>COURSE STATUS</th><th>SEMESTER TAKEN</th><th>COURSE CODE</th></td></tr>";

while ($the = mysql_fetch_array($asql)){
echo "<tr>";
echo "<td>" .$the['CourseTitle']. "</td>";
echo "<td>" .$the['CourseUnit']. "</td>";
echo "<td>" .$the['CourseStatus']. "</td>";
echo "<td >" .$the['SemesterTaken']. "</td>";
echo "<td height='7'>".$the['CourseCode']. "<input name='coursecode' id='coursecode' type='checkbox' />1". "</td>";
echo "</tr>";
}

echo "</table>";
echo "<table align='right'><tr><td width = '95'> <input type='submit' name='submit' value='REGISTER'/>
</td></tr></table>";


?>

</form>

PHP SCRIPT

<?php
session_start();
$EE = mysql_connect("localhost", "root" , ""wink;
if(!$EE){
die("cannot connect to database: ".mysql_error());
}
mysql_select_db("project",$EE);
?>
<?php
$matno = $_POST['matno'];
$session = $_POST['session'];
$level = $_POST['level'];




$sql = "INSERT INTO register(Matno, CourseCode, Level) VALUES ('$matno','$session','$level')";

if (mysql_query($sql,$EE))
{
echo '<script type="text/javascript">alert("Query Successful, add another student"wink;window.location=\'addcourses.php\';</script>';
} else echo"query failed";
mysql_close($EE);

?>

THANKS

Re: Help, Cant Insert Data Into MYSQL Database Table by neowelsh(m): 1:46pm On Dec 03, 2014
neowelsh:
Thank you very much for your replies, below are the codes with screen shots. the first one is is the page where a student is suppose to select his/her level, followed is the script.
1. <table>
<tr>
<td><strong>SURNAME:</strong></td>
<td><?php echo $lastname?></td>
<td><strong>&nbsp;OTHERNAMES:</strong></td>
<td><?php echo $othername?></td></tr>
<tr><td></td></tr>
<tr>
<td><strong>MATRIC NUMBER:</strong></td>
<td><?php echo $matno?></td>
<form action="Precourse_reg.php" method="post">
<td>&nbsp;<label><strong>SESSION:</strong>
<select name="session" id="sesion">
<option selected>Select...</option>
<option>2009/2010<option>
<option>2010/2011</option>
<option>2011/2012</option>
<option>2012/2013</option>
<option>2013/2014</option>
<option>2014/2015</option>
</select></label></td></tr>
<tr><td><label><strong>LEVEL:</strong>
<select name="level" id="level">
<option selected>Select...</option>
<option>100</option>
<option>200</option>
<option>300</option>
<option>400</option>
<option>500</option>
<option>500+</option>
</select></label> </td>
<td><input type="submit" name="submit" value="SUBMIT"/></td></tr></form>
BELOW IS WHAT HAPPEN HAS SOON AS YOU SELECT YOUR LEVEL AND SESSION
<form action="performreg.php" method="post">
<table><tr>
<td><strong>SURNAME:</strong></td><td><label id="lastname" name="lastname"><?php echo $lastname ?></label></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><strong>OTHERNAMES:</strong></td><td id="othername"><label id="othername" name="othername"><?php echo $othername ?></label></td></tr>
<tr>
<td><strong>MATRIC NUMBER:</strong></td><td><input type="text" id="matno" name="matno" value="<?php echo $matno ?>"></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><strong>SESSION:</strong></td><td><input type="text" id="session" name="session" value="<?php echo $session ?>"></td></tr>
<tr>
<td><strong>LEVEL:</strong></td><td id="level"><input type="text" id="level" name="level"value="<?php echo $level ?> "></td></tr>
<tr></tr>
</table>

<?php
$asql = mysql_query("SELECT courses.CourseCode, courses.CourseTitle, courses.CourseUnit, courses.CourseStatus, courses.SemesterTaken, lecturer.Title, lecturer.Firstname, lecturer.Lastname FROM courses LEFT JOIN lecturer ON courses.LecturerID = lecturer.LecturerID WHERE courses.Level = '$level'"wink;


echo "<table border='2'><tr ><th>COURSE TITLE</th><th >COURSE UNIT</th><th>COURSE STATUS</th><th>SEMESTER TAKEN</th><th>COURSE CODE</th></td></tr>";

while ($the = mysql_fetch_array($asql)){
echo "<tr>";
echo "<td>" .$the['CourseTitle']. "</td>";
echo "<td>" .$the['CourseUnit']. "</td>";
echo "<td>" .$the['CourseStatus']. "</td>";
echo "<td >" .$the['SemesterTaken']. "</td>";
echo "<td height='7'>".$the['CourseCode']. "<input name='coursecode' id='coursecode' type='checkbox' />1". "</td>";
echo "</tr>";
}

echo "</table>";
echo "<table align='right'><tr><td width = '95'> <input type='submit' name='submit' value='REGISTER'/>
</td></tr></table>";


?>

</form>

PHP SCRIPT

<?php
session_start();
$EE = mysql_connect("localhost", "root" , ""wink;
if(!$EE){
die("cannot connect to database: ".mysql_error());
}
mysql_select_db("project",$EE);
?>
<?php
$matno = $_POST['matno'];
$session = $_POST['session'];
$level = $_POST['level'];




$sql = "INSERT INTO register(Matno, CourseCode, Level) VALUES ('$matno','$session','$level')";

if (mysql_query($sql,$EE))
{
echo '<script type="text/javascript">alert("Query Successful, add another student"wink;window.location=\'addcourses.php\';</script>';
} else echo"query failed";
mysql_close($EE);

?>

THANKS

neowelsh:
Thank you very much for your replies, below are the codes with screen shots. the first one is is the page where a student is suppose to select his/her level, followed is the script.
1. <table>
<tr>
<td><strong>SURNAME:</strong></td>
<td><?php echo $lastname?></td>
<td><strong>&nbsp;OTHERNAMES:</strong></td>
<td><?php echo $othername?></td></tr>
<tr><td></td></tr>
<tr>
<td><strong>MATRIC NUMBER:</strong></td>
<td><?php echo $matno?></td>
<form action="Precourse_reg.php" method="post">
<td>&nbsp;<label><strong>SESSION:</strong>
<select name="session" id="sesion">
<option selected>Select...</option>
<option>2009/2010<option>
<option>2010/2011</option>
<option>2011/2012</option>
<option>2012/2013</option>
<option>2013/2014</option>
<option>2014/2015</option>
</select></label></td></tr>
<tr><td><label><strong>LEVEL:</strong>
<select name="level" id="level">
<option selected>Select...</option>
<option>100</option>
<option>200</option>
<option>300</option>
<option>400</option>
<option>500</option>
<option>500+</option>
</select></label> </td>
<td><input type="submit" name="submit" value="SUBMIT"/></td></tr></form>
BELOW IS WHAT HAPPEN HAS SOON AS YOU SELECT YOUR LEVEL AND SESSION
<form action="performreg.php" method="post">
<table><tr>
<td><strong>SURNAME:</strong></td><td><label id="lastname" name="lastname"><?php echo $lastname ?></label></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><strong>OTHERNAMES:</strong></td><td id="othername"><label id="othername" name="othername"><?php echo $othername ?></label></td></tr>
<tr>
<td><strong>MATRIC NUMBER:</strong></td><td><input type="text" id="matno" name="matno" value="<?php echo $matno ?>"></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><strong>SESSION:</strong></td><td><input type="text" id="session" name="session" value="<?php echo $session ?>"></td></tr>
<tr>
<td><strong>LEVEL:</strong></td><td id="level"><input type="text" id="level" name="level"value="<?php echo $level ?> "></td></tr>
<tr></tr>
</table>

<?php
$asql = mysql_query("SELECT courses.CourseCode, courses.CourseTitle, courses.CourseUnit, courses.CourseStatus, courses.SemesterTaken, lecturer.Title, lecturer.Firstname, lecturer.Lastname FROM courses LEFT JOIN lecturer ON courses.LecturerID = lecturer.LecturerID WHERE courses.Level = '$level'"wink;


echo "<table border='2'><tr ><th>COURSE TITLE</th><th >COURSE UNIT</th><th>COURSE STATUS</th><th>SEMESTER TAKEN</th><th>COURSE CODE</th></td></tr>";

while ($the = mysql_fetch_array($asql)){
echo "<tr>";
echo "<td>" .$the['CourseTitle']. "</td>";
echo "<td>" .$the['CourseUnit']. "</td>";
echo "<td>" .$the['CourseStatus']. "</td>";
echo "<td >" .$the['SemesterTaken']. "</td>";
echo "<td height='7'>".$the['CourseCode']. "<input name='coursecode' id='coursecode' type='checkbox' />1". "</td>";
echo "</tr>";
}

echo "</table>";
echo "<table align='right'><tr><td width = '95'> <input type='submit' name='submit' value='REGISTER'/>
</td></tr></table>";


?>

</form>

PHP SCRIPT

<?php
session_start();
$EE = mysql_connect("localhost", "root" , ""wink;
if(!$EE){
die("cannot connect to database: ".mysql_error());
}
mysql_select_db("project",$EE);
?>
<?php
$matno = $_POST['matno'];
$session = $_POST['session'];
$level = $_POST['level'];




$sql = "INSERT INTO register(Matno, CourseCode, Level) VALUES ('$matno','$session','$level')";

if (mysql_query($sql,$EE))
{
echo '<script type="text/javascript">alert("Query Successful, add another student"wink;window.location=\'addcourses.php\';</script>';
} else echo"query failed";
mysql_close($EE);

?>

THANKS

after ticking all the courses to be registered and click on register, i expect the courses to be saved to a table in the database, titled register, below is a diagram of the register table.

Re: Help, Cant Insert Data Into MYSQL Database Table by maekhel(m): 2:58pm On Dec 03, 2014
update your codes to look like below:

<?php
// first make sure the register button has been clicked
if(isset($_POST['submit'])){
$matno = $_POST['matno'];
$session = $_POST['session'];
$level = $_POST['level'];

$sql = "INSERT INTO register(Matno, CourseCode, Level) VALUES ('$matno','$session','$level')";

if (mysql_query($sql,$EE))
{
echo '<script type="text/javascript">alert("Query Successful, add another student"wink;window.location=\'addcourses.php\';</script>';
} else echo"query failed";
mysql_close($EE);
}
and lets see
Re: Help, Cant Insert Data Into MYSQL Database Table by Nobody: 5:54pm On Dec 03, 2014
you shouldn't be using MySQL, the folks behind PHP says it's deprecated,Use mysqli..

try oga maekhel's code
Re: Help, Cant Insert Data Into MYSQL Database Table by neowelsh(m): 2:58pm On Dec 04, 2014
hello Mr. maekhel thanks for your reply, however have tried the code but yet got the same error. what do you think might be wrong?
Re: Help, Cant Insert Data Into MYSQL Database Table by maekhel(m): 4:20pm On Dec 04, 2014
neowelsh:
hello Mr. maekhel thanks for your reply, however have tried the code but yet got the same error. what do you think might be wrong?
whats the exact error you are getting
Re: Help, Cant Insert Data Into MYSQL Database Table by neowelsh(m): 4:51pm On Dec 04, 2014
maekhel:

whats the exact error you are getting
query failed is the error i got, which is the error echoed is the data are not inserted into the database, am believing that, the concatenation between the course code and the check box is not working. have you done any work on course registration before?
Re: Help, Cant Insert Data Into MYSQL Database Table by maekhel(m): 7:20am On Dec 06, 2014
neowelsh:

query failed is the error i got, which is the error echoed is the data are not inserted into the database, am believing that, the concatenation between the course code and the check box is not working. have you done any work on course registration before?
fill d form and try echoing out the insert query like this:
echo $sql;
then paste wats echoed here

(1) (Reply)

LEAK.: How Am Recieving Unlimited Mb From Mtn / REDACTED / Buy Verified Nigerian Adsense Account

(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. 80
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.