Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host_database = 'mysql:dbname=tabla_db;host=localhost';
- $username = "root";
- $password = "";
- try{ $pdo = new PDO($host_database,$username,$password); }
- catch(PDOException $e){ echo 'Error: ' . $e->getMessage(); }
- if ($_SERVER['REQUEST_METHOD'] == 'POST'){
- $student_name = $_POST['student_name'];
- $statment = $pdo->prepare( 'INSERT INTO students (student_id,student_name) VALUES (null, :student_name)' );
- $statment->execute(array( ':student_name' => $student_name, )); }
- ?>
- <form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
- <input type="text" class="form-control" name="student_name" placeholder="Name Student">
- <select multiple class="form-control" name="course_id[]">
- <option value="1">Photoshop</option>
- <option value="2">Illustator</option>
- <option value="3">Cinema 4D</option>
- <option value="4">Indesign</option>
- <option value="5">3D Max</option>
- </select>
- <button type="submit">Submit</button>
- </form>
- <?php
- $host_database = 'mysql:dbname=tabla_db;host=localhost';
- $username = "root";
- $password = "";
- try{ $pdo = new PDO($host_database,$username,$password); }
- catch(PDOException $e){ echo 'Error: ' . $e->getMessage(); }
- if ($_SERVER['REQUEST_METHOD'] == 'POST'){
- $student_name = $_POST['student_name'];
- $statment = $pdo->prepare( 'INSERT INTO students (student_id,student_name) VALUES (null, :student_name)' );
- $statment->execute(array( ':student_name' => $student_name, )); }
- ?>
- <form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
- <input type="text" class="form-control" name="student_name" placeholder="Name Student">
- <select multiple class="form-control" name="course_id[]">
- <option value="1">Photoshop</option>
- <option value="2">Illustator</option>
- <option value="3">Cinema 4D</option>
- <option value="4">Indesign</option>
- <option value="5">3D Max</option>
- </select>
- <button type="submit">Submit</button>
- </form>
- <?php
- $host_database = 'mysql:dbname=tabla_db;host=localhost';
- $username = "root";
- $password = "";
- try{ $pdo = new PDO($host_database,$username,$password); }
- catch(PDOException $e){ echo 'Error: ' . $e->getMessage(); }
- if ($_SERVER['REQUEST_METHOD'] == 'POST'){
- $student_name = $_POST['student_name'];
- $statment = $pdo->prepare( 'INSERT INTO students (student_id,student_name) VALUES (null, :student_name)' );
- $statment->execute(array( ':student_name' => $student_name, )); }
- ?>
- <form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
- <input type="text" class="form-control" name="student_name" placeholder="Name Student">
- <select multiple class="form-control" name="course_id[]">
- <option value="1">Photoshop</option>
- <option value="2">Illustator</option>
- <option value="3">Cinema 4D</option>
- <option value="4">Indesign</option>
- <option value="5">3D Max</option>
- </select>
- <button type="submit">Submit</button>
- </form>
- <?php
- $host_database = 'mysql:dbname=tabla_db;host=localhost';
- $username = "root";
- $password = "";
- try{ $pdo = new PDO($host_database,$username,$password); }
- catch(PDOException $e){ echo 'Error: ' . $e->getMessage(); }
- if ($_SERVER['REQUEST_METHOD'] == 'POST'){
- $student_name = $_POST['student_name'];
- $courses = $_POST['course_id']; //Recibimos los valores de los cursos seleccionados
- $statment = $pdo->prepare( 'INSERT INTO students (student_id,student_name) VALUES (null, :student_name)' );
- $statment->execute(array( ':student_name' => $student_name, ));
- //Después del insert consultamos el id insertado:
- $statment = $pdo->prepare("SELECT @@identity AS id");
- $statment->execute();
- $resultado = $statment->fetchAll();
- $id = 0;
- foreach ($resultado as $row) {
- $id = $row["id"];
- }
- //Ya tenemos el último id insertado, ahora hacemos el insert en la tabla students_courses
- $statment = $pdo->prepare( 'INSERT INTO students_courses (course_id,student_id) VALUES (:course_id, :student_id)' );
- $statment->bindParam(':course_id', $idCourse);
- $statment->bindParam(':student_id', $id); //Variable del último id registrado en la tabla de students
- //Insertamos dependiendo de los id's elegidos
- foreach ($courses as $option_value)
- {
- $idCourse = $option_value;
- $statment->execute();
- }
- }
- ?>
- <?php
- $host_database = 'mysql:dbname=tabla_db;host=localhost';
- $username = "root";
- $password = "";
- try{ $pdo = new PDO($host_database,$username,$password); }
- catch(PDOException $e){ echo 'Error: ' . $e->getMessage(); }
- if ($_SERVER['REQUEST_METHOD'] == 'POST'){
- $student_name = $_POST['student_name'];
- $courses = $_POST['course_id']; //Recibimos los valores de los cursos seleccionados
- $statment = $pdo->prepare( 'INSERT INTO students (student_id,student_name) VALUES (null, :student_name)' );
- $statment->execute(array( ':student_name' => $student_name, ));
- //Después del insert consultamos el id insertado:
- $statment = $pdo->prepare("SELECT @@identity AS id");
- $statment->execute();
- $resultado = $statment->fetchAll();
- $id = 0;
- foreach ($resultado as $row) {
- $id = $row["id"];
- }
- //Ya tenemos el último id insertado, ahora hacemos el insert en la tabla students_courses
- $statment = $pdo->prepare( 'INSERT INTO students_courses (course_id,student_id) VALUES (:course_id, :student_id)' );
- $statment->bindParam(':course_id', $idCourse);
- $statment->bindParam(':student_id', $id); //Variable del último id registrado en la tabla de students
- //Insertamos dependiendo de los id's elegidos
- foreach ($courses as $option_value)
- {
- $idCourse = $option_value;
- $statment->execute();
- }
- }
- ?>
- $statment = $pdo->prepare( 'INSERT INTO students (student_id,student_name) VALUES (null, :student_name)' );
- $statment->execute(array( ':student_name' => $student_name, )); }
- $statment = $pdo->prepare("SELECT MAX(id) AS id FROM students_courses");
- $statment->execute();
- $row = $statment->fetch();
- $ultimo_id_recuperado_de_students = $row['id'];
- $statment = $pdo->prepare( 'INSERT INTO students_courses(course_id,student_id) VALUES (null, :student_id)' );
- $statment->execute(array( ':student_id' => $ultimo_id_recuperado_de_students, )); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement