adding data to mysql using php

Discussion in 'PHP' started by internally1, Aug 11, 2012.

  1. #1
    i am developing a school management system,n came into ambigious situation, how can i add school fee for a student for different academic years into the database in one row?
     
    internally1, Aug 11, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Two choices. Add 4 fields, one for each of a student's 4 years, or add a table that includes the student's ID number, the year and the fee. The second method is preferred for purity, but the first method will probably result in easier code.
     
    Rukbat, Aug 11, 2012 IP
  3. shubhamm

    shubhamm Member

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    if you want code then here it is
    
    mysql_query("Insert into table_name (Col1,Col2,Col3,Col4) VALUES ('Col1_Value','Col2_Value','Col3_Value','Col4_Value')");
    PHP:
     
    Last edited: Aug 12, 2012
    shubhamm, Aug 12, 2012 IP
  4. seoexposure

    seoexposure Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you want to add directly:
    
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("my_db", $con);
    
    mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
    VALUES ('Peter', 'Griffin',35)");
    
    mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
    VALUES ('Glenn', 'Quagmire',33)");
    
    mysql_close($con);
    ?>
    
    PHP:
    Through Form :
    
    <html>
    <body>
    
    <form action="insert.php" method="post">
    Firstname: <input type="text" name="firstname" />
    Lastname: <input type="text" name="lastname" />
    Age: <input type="text" name="age" />
    <input type="submit" />
    </form>
    
    </body>
    </html>
    
    PHP:
    Insert PHP Code:
    
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("my_db", $con);
    
    $sql="INSERT INTO Persons (FirstName, LastName, Age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    
    mysql_close($con);
    ?>
    
    PHP:
     
    seoexposure, Aug 12, 2012 IP
  5. internally1

    internally1 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thenx guys i apprecite
     
    internally1, Aug 15, 2012 IP
  6. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #6
    Seoexposure and Shubham,

    You shouldn't be using mysql_* functions. They've been deprecated for a while, look on PHP.net.

    Use PDO or mysqli
     
    BRUm, Aug 18, 2012 IP
  7. InstaCoders

    InstaCoders Peon

    Messages:
    53
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    One thing you could do is check the $_POST array and recommended you make sure the variables are cleaned - but then use a foreach() statement and place your insert statement there.

    That would then essentially loop over the $_POST array and then add it, instead of having to do it line by line.

    Let me know if this is clear and if not then I will show a working example.
     
    InstaCoders, Aug 25, 2012 IP
  8. fastestsms

    fastestsms Greenhorn

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #8
    You can use implode and explode to do it. Implode it before inserting to database and explode it after selecting from database. 8->
     
    fastestsms, Aug 26, 2012 IP