0

MySql と phpmyadmin を使用して、2 つのテーブルがpersonあり、フィールドをテーブルの外部キーとしてreport使用してリンクされています。1 つの php フォームを使用して、個人の詳細をテーブルに追加しようとしています。同じフォームを使用して、テーブルに日付を投稿して自動インクリメントを生成し、外部キーを使用してその日付の特定のレポートにその人をリンクさせたいと考えています。person_idreportpersonreportreport_idperson_id

これが私の現在のコードです

<?php

if (isset($_POST['submitted'])) {

    include('dbcon.php'); //link to connection file

    $pid = "SELECT person_id FROM person\n"
         . "ORDER BY person_id DESC\n"
         . "LIMIT 1"; //variable finds last generated person_id

    $sqlinsert1 = "INSERT INTO person (person.title, person.first_name, person.last_name,  person.address, person.contact_no, person.email, person.ha_id) VALUES ('$_POST[title]' , '$_POST[first_name]' , '$_POST[last_name]' , '$_POST[address]' , '$_POST[contact_no]' , '$_POST[email]' , '$_POST[ha_id]')";

    $sqlinsert2 = "INSERT INTO report (report.date) VALUES ('$_POST[date]') WHERE ($pid = report.person_id)";

    if (!mysqli_query($dbcon, $sqlinsert1)) {
        die('Error inserting record');
    } //end of nested if statement

    if (!mysqli_query($dbcon, $sqlinsert2)) {
        die('Error inserting record');
    } //end of nested if statement

    $newrecord = "new record added to database";
} //end of if statement

?>

テーブルで最後に生成された$pidものを見つける変数を作成しました。これをテストしましたが、phpmyadminで動作します。次に、この変数を使用して日付を にリンクし、テーブルに配置します。person_idpersonperson_idreport

これは非常に複雑に聞こえるかもしれませんが、簡単な答えがあると確信しています。

4

2 に答える 2

0

講師と話し、すべての結果をエコーアウトするのに何時間も費やした後、接続 sqli ステートメント ('mysqli_query') はサーバーに接続していましたが、実際にはデータベース自体に接続していないことがわかりました。 select ステートメントを使用して person テーブルから。

標準の sql 接続ステートメントを挿入し、データベース名を個別に指定しました。これで機能するようになり、別のテーブル ('defect_id') から別の外部キーを追加し、欠陥テーブルに別の挿入ステートメントを追加しました。このコードを実行できるようになり、正常に動作します。

<?php

            if (isset($_POST['submitted'])) {

    include('dbcon.php');

            //insert statement 1    
        $sqlinsert1 = "INSERT INTO person (title, first_name, last_name, address, contact_no, email, ha_id) VALUES ('$_POST[title]' , '$_POST[first_name]' , '$_POST[last_name]' , '$_POST[address]' , '$_POST[contact_no]' , '$_POST[email]' , '$_POST[ha_id]')";

        if (!mysqli_query($dbcon, $sqlinsert1)) {
        die('Error inserting record1');
        } //end of nested if statement1


                    // connect to database      
$dbcon2 = mysql_connect('localhost' , 'root' , '' ); //mysqli query would not connect to db so using mysql connection

        if (!$dbcon2){
        die('error connecting to database');
        }

$dbselect = @mysql_select_db('potholes_v2');

        if (!$dbselect){
        die('error connecting database');
        }

                    //insert statement 2            
$sqlinsert2 = "INSERT INTO defect (road, location, carriageway, lane, diameter, depth, speed, description) VALUES ('$_POST[road]' , '$_POST[location]' , '$_POST[carriageway]' , '$_POST[lane]' , '$_POST[diameter]' , '$_POST[depth]' , '$_POST[speed]' , '$_POST[description]')";

        if (!mysqli_query($dbcon, $sqlinsert2)) {
        die('Error inserting record2');
        } //end of nested if statement1


mysql_close(); //close database connection


                   //connect to database
$dbcon2 = mysql_connect('localhost' , 'root' , '' ); //mysqli query would not connect to db so using mysql connection

        if (!$dbcon2){
        die('error connecting to host');
        }

$dbselect = @mysql_select_db('potholes_v2');

        if (!$dbselect){
        die('error connecting database');
        }

                    //select person_id value
$value = mysql_query("SELECT person_id FROM person ORDER BY person_id DESC" , $dbcon2); //selects last entry of person_id in person table

        if (!$value) {
        die ('error, no values'); //error if no value selected
        }

$value2 = mysql_result($value,0); //specifies new value to be inserted into report table as person_id foreign key

                    //select defect_id value    
$defect = mysql_query("SELECT defect_id FROM defect ORDER BY defect_id DESC" , $dbcon2); //selects last defect_id entry from defect table

        if (!$defect) {
        die ('Error, no values for defect'); //error if no value selected
        }

$defect2 = mysql_result($defect,0); //specifies new defect value to be placed in report table

                    //insert statement 3
$sqlinsert3 = "INSERT INTO report (date, person_id, defect_id) VALUES ('$_POST[date]' , $value2 , $defect2)"; //inserts date, person_id and defect_id values into report table

        if (!mysqli_query($dbcon, $sqlinsert3)) {
        die('Error inserting record 3');
        } //end of nested if statement1


mysql_close(); //close database connection  





$newrecord = "new record added to database";  //gives feedback for successful submission

}  //end of initial if statement

?>
于 2013-10-28T21:54:34.867 に答える
0

$_POST[date] ' 引用符を使用してエラーが発生していると思います。次のようなコード..

人物を挿入するときは、人物 ID を取得します。

次に、レポートに挿入します

$sqlinsert2 = "INSERT INTO report (report.pid,report.date) VALUES ('$pid',"$_POST['date']");//changed check it

次に、両方のテーブルがpid(参照キー)でリンクされます。

于 2013-10-24T20:04:14.380 に答える