I have a problem regarding "no prompted error and data cannot insert into child table".
I have 2 tables (users and useradvert);
- The users is the PARENT
- The useradvert is the CHILD
users(parent table-ID is primary key with auto increment)- ID
- name
- username
- telno
- password
- date (timestamp)
useradvert(child table-ID is index with NO auto increment)- ID
- name2
- color2
- hobby2
I have no problems creating a relation table. The 2 tables are now related.
Then I have a login page (login.php) - runs fine no problem..
And I have a user page (useracc-test.php)-> this is a page after a user log in successfully via login.php. They will be able to view their personal data and also enter their name again (any different nick name they like), color and hobby.This page display user's personal data and secondly, there is also a form where users can enter data like I said previously(name2,color and hobby).
I have no problem displaying the user's personal data in the user page (useracc.test.php). This data is retrieved from parent table "user". The problem I'm having is, I cannot insert data into the child table (useradvert). No error prompted.
<?php
//useracc-test.php
/**
* Start the session.
*/
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
// require 'lib/password.php';
require 'connect-test.php';
$userName= isset($_POST['username']) ? $_POST['username'] : '';
$query = "SELECT id, name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
?>
<html>
<head>
<style type="text/css">
#apDiv2 {
position: absolute;
left: 51px;
top: 238px;
width: 237px;
height: 93px;
z-index: 1;
}
#apDiv1 {
position: absolute;
left: 134px;
top: 123px;
width: 234px;
height: 104px;
z-index: 2;
}
#apDiv3 {
position: absolute;
left: 58px;
top: 146px;
width: 219px;
height: 61px;
z-index: 2;
}
#apDiv4 {
position: absolute;
left: 302px;
top: 102px;
width: 365px;
height: 123px;
z-index: 3;
}
</style>
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css">
<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
</head>
<body>
Your Personal details:</p>
<p><?php while($row = $res->fetch_array()): ?>
<p><?php echo $row['id']; ?></p>
<p><?php echo $row['name']; ?></p>
<p><?php echo $row['username']; ?></p>
<p><?php echo $row['telno']; ?>
<?php
// $userid = $_POST['id'];
$stmt=$conn->prepare("INSERT INTO useradvert (id,name2,color2,hobby2) VALUES (?,?,?,?)");
$stmt->bind_param("isss", $id, $name2, $color2, $hobby2);
$stmt->execute();
if (!$stmt)
{ printf("Errormessage: %s\n", $mysqli->error);}
else {
echo "New records created successfully";}
$stmt->close();
$conn->close();
?>
<form name="form2" method="post" action="useracc-test.php">
<p>INSERT YOUR INTEREST:</p>
<p>
</p>
ID:
<input name="id" type="hidden" id="id" value="<?php echo $row['id']; ?>">
<p>Name :
<input type="text" name="name2" id="name2">
</p>
<p>
<label for="warna2"></label>
Color :
<input type="text" name="color2" id="color2">
</p>
<p>
<label for="hobi2"></label>
Hobby:
<input type="text" name="hobby2" id="hobby2">
</p>
<p>
<input type="submit" name="submit" id="submit" value="submit">
</p>
<p> </p>
</form>
<?php endwhile; ?>
</body>
</html>
--
-- Table structure for table `useradvert`
--
CREATE TABLE IF NOT EXISTS `useradvert` (
`id` int(10) unsigned NOT NULL,
`name2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`color2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`hobby2` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`telno` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=96 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `name`, `telno`, `username`, `password`, `date`) VALUES
(95, 'Test Name', '09999999999', '[email protected]', '$2y$12$fqdmAQk5c8qk8Eh2TWy2n.AdNO.lFjqmi2ruSzk8tsVXcK71OcPae', '2015-12-24 05:00:13');
--
-- Constraints for dumped tables
--
--
-- Constraints for table `useradvert`
--
ALTER TABLE `useradvert`
ADD CONSTRAINT `useradvert_ibfk_1` FOREIGN KEY (`id`) REFERENCES `users` (`id`);