could anyone check this coding for me pls?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mtvaran
Forum Newbie
Posts: 2
Joined: Thu Nov 04, 2010 3:29 pm

could anyone check this coding for me pls?

Post by mtvaran »

could anyone please help me with the code which is i have already displayed data from two table as a drop-down list and a multi select list but now i need to select one from drop-down list and one or more from multi select list the insert into another database table.
here is the coding. it shows no error message( blank page after submit) and nothing inserted on the test table

Code: Select all

 <?php$con = mysql_connect("localhost","root",""); 
if (!$con)   {   die('Could not connect: ' . mysql_error());   }  
error_reporting(E_ALL); 
ini_set('display_errors', 1);    
mysql_select_db("uni", $con)or trigger_error('MySQL error: ' . mysql_error()); 
$result  = mysql_query("SELECT * FROM course") or trigger_error('MySQL error: ' . mysql_error()); 
echo '<select name ="cid[]" multiple="multiple" size="10">'; 
while($row = mysql_fetch_array($result)) 
{     echo '<option value="' . $row['CourseID'] . '">' . $row['CourseName'] . '</option>'; } 
echo '</select>'; /
/ ---------------- 
$result  = mysql_query("SELECT * FROM student") or trigger_error('MySQL error: ' . mysql_error()); 
echo '<select name="sid">'; while($row = mysql_fetch_array($result)) 
{     echo '<option value="' . $row['StudentID'] . '">' . $row['StudentName'] . '</option>'; } 
echo '</select>'; 
mysql_close($con); ?>

<form id="form1" name="form1" method="post" action="update_result.php">  
 <label>   <input type="submit" name="Submit" value="Submit" />   </label> 
</form> 
</body> 
</html>


update_result.php

Code: Select all

<?php $con = mysql_connect("localhost","root",""); 
if (!$con)   {   die('Could not connect: ' . mysql_error());   }   
  mysql_select_db("uni", $con)or trigger_error('MySQL error: ' . mysql_error());    
  if (!empty($_POST['sid']) && !empty($_POST['cid'])) 
{     foreach ($_POST['cid'] as $key => $course)   
  {      $courses .= $course."-";      }     $courses = rtrim($courses,"-");   
  $student = $_POST['sid'];   
  $sql = "INSERT INTO test (StudentID, CourseID) VALUES('".mysql_real_escape_string($student)."','".mysql_real_escape_string($courses)."')";    
 $query = mysql_query($sql) or trigger_error('MySQL error: ' . mysql_error());    
 if (mysql_affected_rows() > 0)     
{         echo mysql_affected_rows() . ' rows added.';    
 } }   
mysql_close($con); 
?> 
</body>
 </html>


Last edited by mtvaran on Thu Nov 04, 2010 4:05 pm, edited 1 time in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: could anyone check this coding for me pls?

Post by Jonah Bron »

Please edit your post and format your code. It is unreadable in it's current form. Thank you.
mtvaran
Forum Newbie
Posts: 2
Joined: Thu Nov 04, 2010 3:29 pm

Re: could anyone check this coding for me pls?

Post by mtvaran »

i have done! is that ok now?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: could anyone check this coding for me pls?

Post by Jonah Bron »

Ugh, unreadable. This is what I had in mind:

Code: Select all

<?php

$con = mysql_connect("localhost","root","");

if (!$con) {
    die('Could not connect: ' . mysql_error());
}

error_reporting(E_ALL);
ini_set('display_errors', 1);  
  
mysql_select_db("uni", $con)
    or trigger_error('MySQL error: ' . mysql_error());

$result  = mysql_query("SELECT * FROM course")
    or trigger_error('MySQL error: ' . mysql_error());

echo '<select name ="cid[]" multiple="multiple" size="10">';
while($row = mysql_fetch_array($result)) {
    echo '<option value="' . $row['CourseID'] . '">' . $row['CourseName'] . '</option>';
}
echo '</select>';

$result  = mysql_query("SELECT * FROM student")
    or trigger_error('MySQL error: ' . mysql_error());

echo '<select name="sid">';
while($row = mysql_fetch_array($result)) {
    echo '<option value="' . $row['StudentID'] . '">' . $row['StudentName'] . '</option>';
}
echo '</select>';

mysql_close($con);

?>

        <form id="form1" name="form1" method="post" action="update_result.php">  
            <label>
                <input type="submit" name="Submit" value="Submit" />
            </label>
        </form>
    </body>
</html>
Edit your post and paste this in, and format the other block the same way.

You will get a lot more answers if you do this :)
Post Reply