<?php
include "dbConfig.php";
$sql_query = mysql_query("SELECT SubjectID, SubjectDesc, FROM tbl_subject");
?>
<select name="X" value="X">
<option value="Choose">Choose</option>
<?php
while($fetch = mysql_fetch_array($sql_query))
{ ; 
?>
<option value="<?php $fetch['SubjectID'];?>" selected="selected"><?php echo $fetch['SubjectID']; ?></option>
<?php
}
?>
</select>

INSERT code

<?php
include "dbConfig.php";
$Y = $_POST["Y"];
$query="INSERT into tbl_subjectsenrolled(SubjectID) values('".mysql_real_escape_string($Y)."')";
?>

But I cannot INSERT the SubjectID inside tbl_subjectsenrolled..

Recommended Answers

All 10 Replies

<?php
include "dbConfig.php";
$Y = $_POST["Y"];
$Y = mysql_real_escape_string($Y);
$query = mysql_query("INSERT INTO (SubjectID) VALUES ('$Y')")or die(mysql_error());
?>
<?php
include "dbConfig.php";
$Y=$_POST["Y"];
$query="INSERT into tbl_subjectsenrolled (SubjectID) values ('".mysql_rel_escape_string($Y)."')"; mysql_query($query) or die (mysql_error());
?>
pixelsoul

Still cant INSERT

No error message back?

I had to leave the for a while. If it's not solved when I return, I will take a look at it.

pixelsoul

No error message sir..

Insert a temporary debug code. It is simple but it often helps. Test the displayed query in phpmyadmin or post it here.

<?php
include "dbConfig.php";
$Y=$_POST["Y"];
$query="INSERT into tbl_subjectsenrolled (SubjectID) values ('".mysql_rel_escape_string($Y)."')";

// Temp DEBUG code
die($query);

mysql_query($query) or die (mysql_error());
?>

And, oops, the mysql_real_escape_string function name is misspelled.

the output was..

INSERT into tbl_subjectsenrolled(SubjectID) values('')

what does that mean??

Itmeans that there is no value for the $Y variable which is basicaly the $_POST["Y"] variable. In other words posted data does not exist and the query inserts empty string (which is probably what you do not want). Where is it supposed to come form?

Good practice is to check for for submittion or existence first:

<?php
include "dbConfig.php";
if(isset($_POST["Y"])) {
    // you can escape here
    $Y=mysql_rel_escape_string($_POST["Y"]);
    // use escaped value
    $query="INSERT into tbl_subjectsenrolled (SubjectID) values ('$Y')";
    mysql_query($query) or die (mysql_error());
}
?>
commented: nice one +5
broj1

This is now solved sir..This is my approach..

avesubject2.php
<?php
    include "dbConfig.php";
    if (isset($_GET["Y"]))
    {
    $Y = mysql_real_escape_string($_GET["Y"]);

    if (isset($_GET["STID"]))
    {

    $STID = mysql_real_escape_string($_GET["STID"]);
    if (isset($_GET["A"]))
    {

    $A = mysql_real_escape_string($_GET["A"]);
    if (isset($_GET["S"]))
    {
    $S = mysql_real_escape_string($_GET["S"]);

    $query =("INSERT into tbl_subjectsenrolled(SubjectID, StudentID, Year, Semester) values('$Y', '$STID', '$A', '$S')"); 
    mysql_query($query) or die (mysql_error());
    header("location:addsubject2.php");
    }
    }
    }
    }
?>

Since I have to store other data such as Student ID number, academic year and semester.. thank you for your help..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.