HI guys i have a little problem while i post the data that is class_id and subject_id to my db with it extra field is generated having zero in class_id and subject_id e.g below
Edit Delete 0 0
Edit Delete 315 1

beside the original ids.here is my code below i cant figure it out that why is this happening can someone help me

<?php

if(isset($_POST)){
    

$con = mysql_connect("localhost","root","");
if ($con)
  {
/*  die('connected: ' );*/
  }
  
  else{
	  
	  die ("not connected");
	  }

$db_selected = mysql_select_db("project", $con);

  $subject = $_POST['subject_name'];
  $id = $_POST['id2'];


	///insertion code
$sql = mysql_query("INSERT INTO `connection` (subject_id,class_id) VALUES ('$subject','$id') ");

	

}
?>



<html>
<head>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/div_validation.js"></script>



<link rel="stylesheet" type="text/css" href="css/connection.css" />

</head>

<body>


                  <div id="form2">
                    <center id="Center">
                    Enter Subject
                    </center>
                    </div>
    
           
                    <hr id="new">
                    </hr>
                    <center class="Center">
            
<form id="class" class="class" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"  style="width:600px">
 
                   <div class="test">
                       
    
                    <select class="form-dropdown" style=" 
    width: 150px;" >
                    <option>  </option>
                    <option value="Option 1"> Option 1 </option>
                    <option value="Option 2"> Option 2 </option>
                    <option value="Option 3"> Option 3 </option>
                    </select>
               
                    <label class="label1"> Go to
                    </label>
                    </div>
  
                    <p class="name1"> 
                    <select  id="id2" name="id2">
                    <option value="" selected="true"></option>
                    <option value="315">315</option>
                    <option value="316">316</option>
                    <option value="317">317</option> 
                    </select> 
                    <label for="name"><i>Class id</i></label> 
                    </p>
                    
                    <p class="name">  
                    <select id="subject_name" name="subject_name">
                    <option value="" selected="true"></option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>  
                    </select>
                     <label for="name"><i>Subject id</i></label> 
                    </p>  
 
                    <p class="submit">  
                    <input type="submit"  id="submit"value="Submit"  />  
                    </p>  
  
                    </form> 
                    </center>

                    <div id="msg"></div>
                    <color> <horizontal offset> <vertical offset> <blur> 


</body>
</html>

Recommended Answers

All 5 Replies

Could you clarify the problem more than this?
What do you mean by:

Edit Delete 0 0
Edit Delete 315 1

?
If the problem is in inserting the data into MySQL, then replace the line #25 with:

$sql = mysql_query("INSERT INTO connection (subject_id,class_id) VALUES ('$subject','$id')") or die(mysql_error());

I think it might be because of these lines:

<option value="" selected="true"></option>

It sees the option as selected and inserts value 0 into the table.

i have done it but the problem is when the page refresh after selecting value it insert extra row of class_id zero and sub_id zero

i figure out the problem its was in insert query as the class_id was first field of the table and subject_id was second and put it second one field in first place and first one in second.Here is query below.

$sql = mysql_query("INSERT INTO `connection` (subject_id,class_id) VALUES ('$id','$subject') ")or die(mysql_error());

Correct one

$sql = mysql_query("INSERT INTO `connection` (class_id,subject_id) VALUES ('$id','$subject') ")or die(mysql_error());

Thanks for the concern

Thanks for sharing the solution :)

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.