bir web formu üzerinden php kullanarak birden fazla tabloya veri ekleme

1 Cevap

Geçenlerde php ve mysql okumakta olan ve aynı zamanda şu anda PHP ve MySQL kullanarak bir online web formunu geliştiriyor. şeklinde i veri göndermek için gereken birden çok tablo var. ben bu kodu denedim;

 $required_fields = array('event_type','accommodation_type','public_user','comments','grand_total');
    $required_fields2 = array('first_name','last_name','gender','age_group');
    $errors = array_merge($errors, check_required_fields($required_fields, $_POST));
    $errors2 = array_merge($errors2, check_required_fields($required_fields2, $_POST));

    $fields_with_lengths = array('event_type' => 50, 'accommodation_type' => 50, 'public_user' => 50,'comments' => 10000,'grand_total' => 50);
    $fields_with_lengths2 = array('first_name' => 50, 'last_name' => 50, 'gender' => 50,'age_group' => 50);
    $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
    $errors2 = array_merge($errors2, check_max_field_lengths($fields_with_lengths2, $_POST));

    $event_type = trim(mysql_prep($_POST['event_type']));
    $accommodation_type= trim(mysql_prep($_POST['accommodation_type']));
    $public_user = trim(mysql_prep($_POST['public_user']));
    $comments = trim(mysql_prep($_POST['comments']));
    $grand_total = trim(mysql_prep($_POST['grand_total']));

    if (empty($errors)){
        $query = "INSERT INTO event_registration (event_type, accommodation_type, public_user, comments, grand_total) VALUES ('{$event_type}','{$accommodation_type}','{$public_user}','{$comments}','{$grand_total}')";
        $result = mysql_query($query, $connection);

        if($result){
            $message = "The User was successfully registered";
        } else{
            $message = "The User could not be registered";
            $message .= "<br />" . mysql_error();
        }
    } else{

        if(count($errors)==1){
            $message = "There was 1 error in the form.";
        } else {
            $message = "There were" . count($errors) . " error in the form.";
        }   
    }

} else { // Form has not been submitted
    $event_type = "";
    $accommodation_type = "";
    $public_user = "";
    $comments = "";
    $grand_total = "";
}

1 Cevap

Genel olarak, burada iki tabloya bir form verileri göndermek nasıl:

<?php
$dbhost="server_name";
$dbuser="database_user_name";
$dbpass="database_password";
$dbname="database_name";

$con=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to the database:' . mysql_error());

$mysql_select_db($dbname, $con);

$sql="INSERT INTO table1 (table1id, columnA, columnB) 
         VALUES (' ', '$_POST[columnA value]','$_POST[columnB value]')";

mysql_query($sql);

$lastid=mysql_insert_id();

$sql2=INSERT INTO table2 (table1id, table2id, columnA, columnB)
              VALUES ($lastid, ' ', '$_POST[columnA value]','$_POST[columnB value]')";

//tableid1 & tableid2 are auto-incrementing primary keys 

mysql_query($sql2);

mysql_close($con);

?>

Bu örnek katları tabloları içine bir forma veri eklemek için nasıl gösterir, herhangi bir güvenlik önlemi olmadığını göstermiştir