0
<?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..

3
Contributors
10
Replies
44
Views
4 Years
Discussion Span
Last Post by janskey15
Featured Replies
  • 1

    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 … Read More

0
<?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());
?>
0
<?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

0

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());
?>

Edited by broj1

0

And, oops, the mysql_real_escape_string function name is misspelled.

Edited by broj1

0

the output was..

INSERT into tbl_subjectsenrolled(SubjectID) values('')

what does that mean??

1

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());
}
?>

Edited by broj1

Votes + Comments
nice one
0

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..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.