Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ??Passing Form variables to mysql table

    i have a table like this



    i want to enter info to this table via using this form.



    the code of my form is

    PHP Code:
    <?php
    $con
    =mysql_connect("localhost","root") or die("mysql error");
    mysql_select_db("proje",$con) or die("database connection error");
    $qry="select * from courses";
    $result=mysql_query($qry,$con);

    echo 
    "<form action='reg.php' method='post'>";

    echo 
    "<table border=1><tr><td colspan='1'>";

    echo 
    "Student No:";
    echo 
    "<input type='text' name='stnr'>";
    echo 
    "<tr><td align='center'>";
    while(
    $row=mysql_fetch_array($result,MYSQL_ASSOC))
    {
    $r=$row['CourseName'];
    $c=$row['CourseCode'];
    echo 
    $r;
    echo 
    "<input type='checkbox' name=$c']>";
    echo 
    "<br>";
    }
    echo
    "<tr><td align='right'>";
    echo 
    "<input type='submit' align='right'>";

    echo 
    "</table>";
    echo 
    "</form>";

    ?>
    i need a reg.php to save this forms records to registered courses table.can you please help me

  • #2
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,096
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Let me see if I have this straight. You've written one script in this process, now you want us to write the rest of it for you? Sorry, but it looks like you're doing pretty good on your own. Why not give it a go yourself? Then, if it doesn't work for some reason, come back here with specific questions.

    BTW, in the script you posted, I did note one small error here:

    Code:
    echo "<input type='checkbox' name=$c']>";
    You need a left single quote around $c, like so:

    Code:
    echo "<input type='checkbox' name='$c']>";
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #3
    New Coder
    Join Date
    Dec 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    vinyl-junkie

    thank you ,

    btw ,what i need is just an idea..i do pretty good yes you are right but at this point i am confused.if you dont want to help DON'T HELP

    anyways,so should i use a loop in reg.php for pass this form records to mysql table??

  • #4
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,096
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Quote Originally Posted by kesikkafa
    if you dont want to help DON'T HELP
    You may have misunderstood where I am coming from. For that matter, I may have misunderstood what kind of help you were looking for. Please accept my apologies if that is the case.

    As I said in a post just yesterday to someone else, we are here to help you so that you can do the work yourself. We are NOT here to do the work for you, at least not in this forum. The Paid work offers and requests forum is where you would go for that.

    anyways,so should i use a loop in reg.php for pass this form records to mysql table??
    Yes, that's exactly what you would do. You would have a while loop in reg.php, similar to the code you posted, except it would have something similar to this:

    Code:
    while($row=mysql_fetch_array($result,MYSQL_ASSOC)) 
    { 
    $r=$row['CourseChecked']; 
    if ($r > "") {
       // code here to insert data into database
    }
    }
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #5
    New Coder
    Join Date
    Dec 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you ,i will try

  • #6
    New Coder
    Join Date
    Dec 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you wrote

    Code:
    // code here to insert data into database
    before while loop i make and run a query like this

    Code:
    $qry="insert into registeredcourses(StudentNo,CourseCode) values($stn,'$a') ";
    $result=mysql_query($qry,$con);
    then should i re-write the query in the loop again?

    i am asking stupid qustions i know ,i am confused

    and what is CourseChecked

    after check the courses and submit them via form ,i have variables like

    $stn=$_POST['strn'];
    $a=$_POST['c1'];
    $b=$_POST['c2'];
    $c=$_POST['c3'];
    $d=$_POST['c4'];

    in reg.php..
    Last edited by kesikkafa; 01-01-2006 at 02:17 PM.

  • #7
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,096
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Sorry, I should have used the same variables you were using. Basically, what you want to do is make your checkbox an array, then process your array via a loop to insert records into your database. I'm getting *way* out of my area of expertise on this and may be leading you astray with exactly how this would work, but I think what you first need to do is change this statement in your code:

    Code:
    echo "<input type='checkbox' name=$c']>";
    to this:

    Code:
    echo "<input type='checkbox' name=$c[]'>";
    I found another forum post here which has some code similar to how I think this would work. Perhaps you can find some better examples by searching the forum. Or maybe someone else can provide some better code examples for you.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #8
    New Coder
    Join Date
    Dec 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you ,i found the right code at another web site..i changed the checkbox to an array and use a foreach loop...

    Code:
    <?php
    $con=mysql_connect("localhost","root") or die("mysql error");
    mysql_select_db("proje",$con) or die("database connection error");
    
    
    $stnr = $_POST['stnr'];
    $courses = $_POST['courses'];
    
    foreach($courses as $course) 
    {
    $sql = "INSERT INTO registeredcourses (StudentNo, CourseCode) VALUES ($stnr, '$course')";
    mysql_query($sql);
    
    }
    
    $af=mysql_affected_rows($con);
    
    if($af>0)
    {
    echo "Courses Registered Succesfully";
    }
    else
    {
    echo "error";
    } 
    
    
    ?>

  • #9
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alternatively, a single query which inserts multiple records...
    (also changed error check to see if all rows were inserted correctly)

    PHP Code:
    <?php
    $con
    =mysql_connect("localhost","root") or die("mysql error");
    mysql_select_db("proje",$con) or die("database connection error");


    $stnr $_POST['stnr'];
    $courses $_POST['courses'];

    $sql 'INSERT INTO registeredcourses (StudentNo, CourseCode) VALUES ';
    foreach(
    $courses as $course
    {
        
    $sql_values[] = '("' mysql_real_escape_string($stnr) . '","' mysql_real_escape_string($course) . '")';
    }
    mysql_query($sql implode(','$sql_values));

    $af=mysql_affected_rows($con);

    if(
    $af == count($courses))
    {
    echo 
    "Courses Registered Succesfully";
    }
    else
    {
    echo 
    "error";



    ?>
    "$question = ( to() ) ? be() : ~be();"

  • #10
    New Coder
    Join Date
    Dec 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you ,can you please tell me about the disadvantages of this code



    Code:
    if($af>0)
    {
    echo "Courses Registered Succesfully";
    }
    else
    {
    echo "error";
    }

  • #11
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by kesikkafa
    thank you ,can you please tell me about the disadvantages of this code



    Code:
    if($af>0)
    {
    echo "Courses Registered Succesfully";
    }
    else
    {
    echo "error";
    }
    Because your not checking to see if ALL the courses were added. Your just checking if one or more are added. Velox usage of the count() function allows you to check if the number of courses submitted, were inserted into MySQL.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •