首先从表1中选择SELECT到mysqli PHP中的TABLE2中的INSERT

时间:2017-03-03 14:12:40

标签: php mysql mysqli

我遇到这个错误需要帮助:

  

警告:mysqli_num_rows()期望参数1为mysqli_result,boolean given

在我的PHP代码中,我首先在表单中找到输入数据的firstname并将其插入表2中。

这是我的代码

<?php
    include('admin/db/database_configuration.php');
    $sql = "SELECT alum_id FROM tblalumni ORDER BY alum_id DESC LIMIT 1";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()){
    $id = $row['alum_id'] + 1;
    } 
}
$conn->close();
?>

<?php
   include('admin/db/database_configuration.php');

   if(isset($_POST['submit'])) {
   if (empty($_POST['fname'])){$fname = 'NULL'; } else{ $fname ="'". mysqli_real_escape_string($conn, $_POST['fname']) . "'";}
   if (empty($_POST['mname'])){$mname = 'NULL'; } else{ $mname ="'". mysqli_real_escape_string($conn, $_POST['mname']) . "'";}
   if (empty($_POST['lname'])){$lname = 'NULL'; } else{ $lname ="'". mysqli_real_escape_string($conn, $_POST['lname']) . "'";}
   if (empty($_POST['gender'])){$gender = 'NULL'; } else{ $gender ="'". mysqli_real_escape_string($conn, $_POST['gender']) . "'";}
   if (empty($_POST['CivilStat'])){$civilstat = 'NULL'; } else{ $civilstat ="'". mysqli_real_escape_string($conn, $_POST['CivilStat']) . "'";}
   $bday = $_POST['bdate'];
   if (empty($_POST['address'])){$address = 'NULL'; } else{ $address ="'". mysqli_real_escape_string($conn, $_POST['address']) . "'";}
   if (empty($_POST['telnum'])){$telnum = 'NULL'; } else{ $telnum ="'". mysqli_real_escape_string($conn, $_POST['telnum']) . "'";}
   if (empty($_POST['mobnum'])){$mobnum = 'NULL'; } else{ $mobnum ="'". mysqli_real_escape_string($conn, $_POST['mobnum']) . "'";}
   if (empty($_POST['emailadd'])){$email = 'NULL'; } else{ $email ="'". mysqli_real_escape_string($conn, $_POST['emailadd']) . "'";}
   if (empty($_POST['yeargrad'])){$yeargrad = 'NULL'; } else{ $yeargrad ="'". mysqli_real_escape_string($conn, $_POST['yeargrad']) . "'";}
   if (empty($_POST['Department'])){$department = 'NULL'; } else{ $department ="'". mysqli_real_escape_string($conn, $_POST['Department']) . "'";}
   if (empty($_POST['Course'])){$course = 'NULL'; } else{ $course ="'". mysqli_real_escape_string($conn, $_POST['Course']) . "'";}
   if (empty($_POST['awards'])){$awards = 'NULL'; } else{ $awards ="'". mysqli_real_escape_string($conn, $_POST['awards']) . "'";}
   if (empty($_POST['scholarships'])){$scholarships = 'NULL'; } else{ $scholarships ="'". mysqli_real_escape_string($conn, $_POST['scholarships']) . "'";}
   if (empty($_POST['attainment'])){$attainment = 'NULL'; } else{ $attainment ="'". mysqli_real_escape_string($conn, $_POST['attainment']) . "'";}
   if (empty($_POST['employed'])){$employed = 'NULL'; } else{ $employed ="'". mysqli_real_escape_string($conn, $_POST['employed']) . "'";}
   if (empty($_POST['empstatus'])){$empstatus = 'NULL'; } else{ $empstatus ="'". mysqli_real_escape_string($conn, $_POST['empstatus']) . "'";}
   if (empty($_POST['presoccup'])){$presoccup = 'NULL'; } else{ $presoccup ="'". mysqli_real_escape_string($conn, $_POST['presoccup']) . "'";}
   if (empty($_POST['gradstud'])){$gradstud = 'NULL'; } else{ $gradstud ="'". mysqli_real_escape_string($conn, $_POST['gradstud']) . "'";}
   if (empty($_POST['advstud'])){$advstud = 'NULL'; } else{ $advstud ="'". mysqli_real_escape_string($conn, $_POST['advstud']) . "'";}
   if (empty($_POST['participated'])){$participated = 'NULL'; } else{ $participated ="'". mysqli_real_escape_string($conn, $_POST['participated']) . "'";}
   if (empty($_POST['assist'])){$assist = 'NULL'; } else{ $assist ="'". mysqli_real_escape_string($conn, $_POST['assist']) . "'";}
   if (empty($_POST['programs'])){$programs = 'NULL'; } else{ $programs ="'". mysqli_real_escape_string($conn, $_POST['programs']) . "'";}
   if (empty($_POST['cts'])){$cts = 'NULL'; } else{ $cts ="'". mysqli_real_escape_string($conn, $_POST['cts']) . "'";}
   if (empty($_POST['solvecomplexprob'])){$scp = 'NULL'; } else{ $scp ="'". mysqli_real_escape_string($conn, $_POST['solvecomplexprob']) . "'";}
   if (empty($_POST['workwithother'])){$wwo = 'NULL'; } else{ $wwo ="'". mysqli_real_escape_string($conn, $_POST['workwithother']) . "'";}
   if (empty($_POST['learnind'])){$learnind = 'NULL'; } else{ $learnind ="'". mysqli_real_escape_string($conn, $_POST['learnind']) . "'";}
   if (empty($_POST['writtencommskills'])){$wcs = 'NULL'; } else{ $wcs ="'". mysqli_real_escape_string($conn, $_POST['writtencommskills']) . "'";}
   if (empty($_POST['spokencommskills'])){$scs = 'NULL'; } else{ $scs ="'". mysqli_real_escape_string($conn, $_POST['spokencommskills']) . "'";}
   if (empty($_POST['knowfield'])){$knowfield = 'NULL'; } else{ $knowfield ="'". mysqli_real_escape_string($conn, $_POST['knowfield']) . "'";}
   if (empty($_POST['workrelatedknow'])){$wrk = 'NULL'; } else{ $wrk ="'". mysqli_real_escape_string($conn, $_POST['workrelatedknow']) . "'";}
   if (empty($_POST['relevantdegree'])){$relevantdegree = 'NULL'; } else{ $relevantdegree ="'". mysqli_real_escape_string($conn, $_POST['relevantdegree']) . "'";}
   if (empty($_POST['impfctr'])){$impfactor = 'NULL'; } else{ $impfactor ="'". mysqli_real_escape_string($conn, $_POST['impfctr']) . "'";}
   if (empty($_POST['overall'])){$overall = 'NULL'; } else{ $overall ="'". mysqli_real_escape_string($conn, $_POST['overall']) . "'";}


$query = "SELECT fname FROM tblalumni WHERE fname = $fname";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {

    if ($_POST['mytext']) {
    foreach ( $_POST['mytext'] as $key=>$value ) {
    $values = mysqli_real_escape_string($conn, $value);
    $query = mysqli_query($conn,"INSERT INTO tblalumni_past_o (pastoccu, alum_id) VALUES ('$values', '$id')");
        }
    }
    $sql = "INSERT INTO tblAlumni (fname, mname, lname, gender, civilstat, birthdate, address, telnum, mobnum, emailadd, yeargrad, department, course, awards, scholarships, attainment, employed, empstatus, presoccup, gradstud, advstud, participated, assist, programs, cts, solvecomplexprob, workwithother, learnind, writtencommskills, spokencommskills, knowfield, workrelatedknow, relevantdegree, impfctr, overall, alum_status) 
        VALUES ($fname,$mname,$lname,$gender,$civilstat, '$bday', $address, $telnum, $mobnum, $email, $yeargrad, $department, $course, $awards, $scholarships, $attainment, $employed, $empstatus, $presoccup, $gradstud, $advstud, $participated, $assist, $programs, $cts, $scp, $wwo, $learnind, $wcs, $scs, $knowfield, $wrk, $relevantdegree, $impfactor, $overall, '1')";

    if (mysqli_query($conn, $sql)) {
                echo "New record created successfully";
            }
    else{
        echo "error";
    }

    mysqli_close($conn);
}
else{
    echo "wrong";//displays erro if he/she is not in the system
    }
}

?>

如果有人看到错误在这里,请帮助我。 谢谢

已编辑和答案

在$ query中删除了变量$ fname中的单引号,并且混合样式的数据库仅更新为Mysqli过程

$query = "SELECT fname FROM tblalumni WHERE fname = $fname";
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {

    if ($_POST['mytext']) {
    foreach ( $_POST['mytext'] as $key=>$value ) {
    $values = mysqli_real_escape_string($conn, $value);
    $query = mysqli_query($conn,"INSERT INTO tblalumni_past_o (pastoccu, alum_id) VALUES ('$values', '$id')");
        }
    }
    $sql = "INSERT INTO tblAlumni (fname, mname, lname, gender, civilstat, birthdate, address, telnum, mobnum, emailadd, yeargrad, department, course, awards, scholarships, attainment, employed, empstatus, presoccup, gradstud, advstud, participated, assist, programs, cts, solvecomplexprob, workwithother, learnind, writtencommskills, spokencommskills, knowfield, workrelatedknow, relevantdegree, impfctr, overall, alum_status) 
        VALUES ($fname,$mname,$lname,$gender,$civilstat, '$bday', $address, $telnum, $mobnum, $email, $yeargrad, $department, $course, $awards, $scholarships, $attainment, $employed, $empstatus, $presoccup, $gradstud, $advstud, $participated, $assist, $programs, $cts, $scp, $wwo, $learnind, $wcs, $scs, $knowfield, $wrk, $relevantdegree, $impfactor, $overall, '1')";

    if (mysqli_query($conn, $sql)) {
                echo "New record created successfully";
            }
    else{
        echo "error";
    }

    mysqli_close($conn);
}
else{
    echo "wrong";//displays erro if he/she is not in the system
    }
}

?>

1 个答案:

答案 0 :(得分:-2)

您正在混合程序和面向对象的数据库风格

而不是

$result = mysqli_query($conn, $query);

应该是

$result = $conn->query($query);

并且

mysqli_num_rows($result)

应该是

$result->num_rows