从两个不同表的两个不同列中选择数据,并在php中插入一个表

时间:2014-08-31 11:50:29

标签: php mysql sql

我想在用户名=“somevalue”时从Table1(user_info)中选择uid,并在start_date =“somedate”时从Table2(周)中选择wid并分别将其插入Table3(时间表)列uid和wid。你的帮助会非常明显。感谢

   <?php
    include "login.php";

    $connect = mysql_connect("localhost","root","") or die("Couldn't Connect");
    mysql_select_db("user_login") or die ("Couldn't Find Database"); 
    // set current date
    $startdate = $_GET["startDate"];
    $enddate = $_GET["endDate"];
    $mon=$_GET["mon"];
    $tue=$_GET["tue"];
    $wed=$_GET["wed"];
    $thur=$_GET["thur"];
    $fri=$_GET["fri"];
    $sat=$_GET["sat"];
    $sun=$_GET["sun"];
    $totalhour= $mon+$tue+$wed+$thur+$fri+$sat+$sun ;
    $insert_uid= "Insert into timesheet(uid) select uid from user_info where username=".$_SESSION['user']."";
    $result1= mysql_query($insert_uid);
    $insert_wid=  "insert into timesheet(wid) select wid from week where start_date= '$startdate'" ;
    $result2= mysql_query($insert_wid);

    $sql= "insert into timesheet(uid,wid,mon,tue,wed,thur,fri,sat,sun,totalhour) values('$result1','$result2','$mon','$tue','$wed','$thur','$fri','$sat','$sun',$totalhour)";
    mysql_query($sql);
    echo $sql;
      echo "<h2>Thanks! You Timesheet submitted successfully</h2>";
    ?>

1 个答案:

答案 0 :(得分:0)

使用mysqli_而不是mysql _

你正在使用mysql_函数,你应该使用mysqli_,就像这样:

<?php
    include "login.php";

    $connect = mysqli_connect("localhost","root","","user_login");
    if(!$connect){die("Couldn't Connect");}
    // set current date
    $startdate = $_GET["startDate"];
    $enddate = $_GET["endDate"];
    $mon=$_GET["mon"];
    $tue=$_GET["tue"];
    $wed=$_GET["wed"];
    $thur=$_GET["thur"];
    $fri=$_GET["fri"];
    $sat=$_GET["sat"];
    $sun=$_GET["sun"];
    $totalhour= $mon+$tue+$wed+$thur+$fri+$sat+$sun ;
    $insert_uid= "Insert into timesheet(uid) select uid from user_info where username=".$_SESSION['user']."";
    $result1= mysqli_query($connect,$insert_uid);
    $insert_wid=  "insert into timesheet(wid) select wid from week where start_date= '$startdate'" ;
    $result2= mysqli_query($connect,$insert_wid);

    $sql= "insert into timesheet(uid,wid,mon,tue,wed,thur,fri,sat,sun,totalhour) values('$result1','$result2','$mon','$tue','$wed','$thur','$fri','$sat','$sun',$totalhour)";
    mysqli_query($connect,$sql);
    echo $sql;
      echo "<h2>Thanks! You Timesheet submitted successfully</h2>";
    ?>

从user_info

中选择uid
$select1 = mysqli_fetch_assoc(mysqli_query($connect,"SELECT uid FROM user_info WHERE username='somevalue'"));
$uid = $select1['uid']; //assuming that you expect just one result from this query

从周选择wid

   $select2 = mysqli_fetch_assoc(mysqli_query($connect,"SELECT wid FROM week WHERE start_date=''"));
   $wid = $select2['wid']; //assuming that you expect just one result

插入数据库

mysqli_query($connect,"INSERT INTO timesheet(uid,wid) VALUES('".$uid."','".$wid."')");