简体   繁体   中英

inserting data from another table and data from textbox and checkbox to create a new record

I have to insert from table 1 into table 2 the values of username, name, lastname, roomnum and dormID.

I have to insert into table 2 the current date of today(it changes everyday), comments(the user wishes to enter- this is a textbox) and the attendance(present, absent, late)(these are the checkboxes).

When trying to click on the submit button the only thing that inserts into the database is the records that I am pulling from table1 into table 2.

When i try to incorporate to also insert the date, latecomments(textbox), and the attendance(checkboxes) it does not store any record.

This are the inserts:

$checkbox1 = $_POST['chk1'];
$latecomment=trim($_POST['latecomment']);
$d=strtotime("today");//todays date
$day=date("Y-m-d", $d);//date format


if($_POST["Submit"]=="Submit"){

    for ($i=0; $i < sizeof ($checkbox1); $i++){
        $query="Insert into dailyattendance (attendance) VALUES ('".$checkbox1[$i]."')";
        mysql_query($query);
        $daily=mysql_query("INSERT INTO dailyattendance(username, name, lastname, roomnum, dormID) SELECT user.username, user.name, user.lastname, user.roomnum, user.dormID FROM user WHERE user.role='Student'" ); 
        $otherinfo=mysql_query("INSERT INTO daily attendance set  date='".$day."', latecomment='".$latecomment."' WHERE dailyid=''");   
    }
}
  1. There's a blank in your table name (in your given code):

    INSERT INTO daily attendance

  2. There's a WHERE clause in your INSERT statement and you are using UPDATE syntax with SET:

    INSERT INTO daily attendance set date='".$day."', latecomment='".$latecomment."' WHERE dailyid=''");

  3. Question: Is there a column named "attendance" in your table?

  4. You should use only one INSERT statement if you want to add one row with all columns.


To clearify it a bit here is a code example you could start with. The SELECT on user is once before the loop because it seems to be same data all the time. You should add a function to escape strings before using them in the SQL statement to avoid errors when quotes are in it. Have a look at PHP or SQL error messages if something went wrong.

if($_POST["Submit"]=="Submit"){
    $sql = "SELECT username, name, lastname, roomnum, dormID FROM user WHERE user.role='Student'";
    $result = mysql_query($sql);
    $student = mysql_fetch_assoc($result);
    for($i=0; $i < sizeof ($checkbox1); $i++){
        $sql = "INSERT INTO dailyattendance (username, name, lastname, roomnum, dormID, attendance, date, latecomment)";
        $sql.= " VALUES ('".$student["username"]."', '".$student["name"]."', '".$student["lastname"]."', '".$student["roomnum"]."', '".$student["dormID"]."'";
        $sql.= ", '".$checkbox1[$i]."', now(), '".$latecomment."')";
        mysql_query($sql); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM