Wednesday 1 March 2017

php - Insert data to table then insert the newly added id to the foreign key

I'm studying php myself and I want my table to get the id of 2 table, the first one is from the new title I added, the second one is from the genre table, so I can put up a lot of category for the title but I can't find any tutorial and it's not in w3school. Don't mind the lack of security as I haven't tackled it yet



My tables(db name is testdb:



    title
---------------------
|title_id|title_name|

---------------------

genre
---------------------
|genre_id|genre_name|
---------------------
|1 | action |
|2 | horror |
---------------------


filter
-----------------------------
|filter_id|genre_id|title_id|
-----------------------------


My php connect is (saved as con.php):



DEFINE ('DB_HOST', 'localhost');

DEFINE ('DB_USER', 'root');
DEFINE ('DB_PSWD', '****');
DEFINE ('DB_NAME', 'testdb');

$dbtest = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);
?>


PHP code is:




if (isset($_POST['submit'])) 
{
include_once('con.php');
$ntitle = $_POST['newtitle'];
$sqlinsert = "INSERT INTO title (title_name) VALUES ('$ntitle')";
if (!mysqli_query($dbtest, $sqlinsert))
{
mysqli_error($dbtest);
} else {

echo ('1recordadded');
}
}


?>


My form:






legend>New Content:




action



horror





No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...