0

Please i have a little problem here. the below code i wrote was meant to insert into two tables simultaneously but it those not work. but if i remove the second INSERT the first INSERT will work dont know whats wrong. ITs meant insert in the first table and also collect the last Insert Id of the First table to the Second table. What did i do wrong

<?php
  $english_name = $_POST['EnglishName'];
  $tel_number = $_POST['TelNumber'];
  $email_address = $_POST['EmailAddress'];
  $gender = $_POST['Gender'];
  $age = $_POST['Age'];
  $region = $_POST['Region'];

  mysql_connect("localhost", "root", "") or die ('Error: ' . mysql_error());
  mysql_select_db("fruitmarket");

  $query="INSERT INTO data (english_name, tel_number, email_address, gender,         age, region) VALUES (";
  $query.="'".$english_name."', ";
  $query.="'".$tel_number."', ";
  $query.="'".$email_address."', ";
  $query.="'".$gender."', ";
  $query.="'".$age."', ";
  $query.="'".$region."')";

  $query .= "INSERT INTO data_category (id, english_name)
VALUES (LAST_INSERT_ID(), '$english_name');";


  mysql_query($query) or die ('Error updating database');

  echo "Record is inserted."; 
?>

2 Answers2

1

its almost 2018, so please stop using depreciated and removed mysql_* functions use PDO/mysqli with prepared statements.

I have re-written your code with prepared statements, please follow these links :

Why shouldn't I use mysql_* functions in PHP?

How can I prevent SQL injection in PHP?

Prepared statements

<?php
$servername = "localhost";
$username   = "username";
$password   = "";
$dbname     = "fruitmarket";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$stmt = "INSERT INTO data (english_name,tel_number,email_address,gender,age,region) VALUES(?,?,?,?,?,?)";

$sql = $conn->prepare($stmt);
$sql->bind_param("ssssis", $english_name, $tel_number, $email_address, $gender, $age, $region);

if ($sql->execute()) {

    $id = $sql->insert_id;

    $insert = $conn->prepare("INSERT INTO data_category (id, english_name) VALUES(?,?)");
    $insert->bind_param("is", $id, $english_name);

    if ($insert->execute()) {

        echo "data inserted successfully";
    } else {

        printf("Errormessage: %s\n", $mysqli->error);
    }
} else {

    printf("Errormessage: %s\n", $mysqli->error);
}

A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency.

Prepared statements basically work like this:

  • Prepare: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?"). Example: INSERT INTO myTabvle VALUES(?, ?, ?)
  • The database parses, compiles, and performs query optimization on the SQL statement template, and stores the result without executing it
  • Execute: At a later time, the application binds the values to the parameters, and the database executes the statement. The application may execute the statement as many times as it wants with different values Compared to executing SQL statements directly, prepared statements have three main advantages:
  • Prepared statements reduces parsing time as the preparation on the query is done only once (although the statement is executed multiple times)
  • Bound parameters minimize bandwidth to the server as you need send only the parameters each time, and not the whole query
  • Prepared statements are very useful against SQL injections, because parameter values, which are transmitted later using a different protocol, need not be correctly escaped. If the original statement template is not derived from external input, SQL injection cannot occur.
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
1

I tested the above code and noticed you just need just to add and change some code see my below example

<?php
  $english_name = $_POST['EnglishName'];
  $tel_number = $_POST['TelNumber'];
  $email_address = $_POST['EmailAddress'];
  $gender = $_POST['Gender'];
  $age = $_POST['Age'];
  $region = $_POST['Region'];

  mysql_connect("localhost", "root", "") or die ('Error: ' . mysql_error());
  mysql_select_db("fruitmarket");

  $query="INSERT INTO data (english_name, tel_number, email_address, gender, age, region) VALUES (";
  $query.="'".$english_name."', ";
  $query.="'".$tel_number."', ";
  $query.="'".$email_address."', ";
  $query.="'".$gender."', ";
  $query.="'".$age."', ";
  $query.="'".$region."')";

  mysql_query($query) or die ('Error updating database');

  echo "Record is inserted."; 

  $query= "INSERT INTO data_category (id, english_name)
VALUES (LAST_INSERT_ID(), '$english_name');";

mysql_query($query) or die ('Error updating database');

  echo "Record is inserted.";
?>

test it to check if it will work

Sir-myke
  • 31
  • 6