Souci avec PHP Mysqlserver
Résolu
Oseer
Messages postés
18
Statut
Membre
-
yg_be Messages postés 23437 Date d'inscription Statut Contributeur Dernière intervention -
yg_be Messages postés 23437 Date d'inscription Statut Contributeur Dernière intervention -
Bonjour Chers Tous,
J'ai besoin d'aide SVP
Voici l'erreur que je rencontre :
Voici la ligne 98 : $stmt->execute(array('zcomment' => $comment, 'zitemid' => $itemid, 'zuserid' => $userid));
Merci de m'aider SVP
J'ai besoin d'aide SVP
Voici l'erreur que je rencontre :
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shop`.`comments`, CONSTRAINT `items_comment` FOREIGN KEY (`item_id`) REFERENCES `items` (`Item_ID`) ON DELETE CASCADE ON UPDATE CASCADE)' in C:\wamp\www\Admin\items.php on line 98
Voici la ligne 98 : $stmt->execute(array('zcomment' => $comment, 'zitemid' => $itemid, 'zuserid' => $userid));
Merci de m'aider SVP
2 réponses
-
Bonjour,
Yg_be t'a donné les informations nécéssaires à la compréhension de ton erreur...
Si tu ne comprends pas, et pour que l'on puisse t'aider.. il nous faut :
- La structure de tes tables (un dump sql serait pas mal)
- Le code PHP qui provoque cette erreur (celui contenant ta requête)
NB: Pour poster du code sur le forum, il faut utiliser la coloration syntaxique (voir ici pour les explications : https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code )
-
Le code PHP qui provoque l'erreur :
if ($_SERVER['REQUEST_METHOD'] == 'POST') { $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING); $itemid = $item['Cat_ID']; $userid = $_SESSION['uid']; if (! empty($comment)) { $stmt = $con->prepare("INSERT INTO comments(comment, status, comment_date, item_id, user_id) VALUES(:zcomment, 0, NOW(), :zitemid, :zuserid)"); Ligne 98 : $stmt->execute(array('zcomment' => $comment, 'zitemid' => $itemid, 'zuserid' => $userid)); if ($stmt) { echo '<[div] class="alert alert-success">Comment Added</div>'; }
Structure des tables :-- phpMyAdmin SQL Dump -- version 4.0.4 -- http://www.phpmyadmin.net -- -- Client: localhost -- Généré le: Sam 10 Mars 2018 à 12:04 -- Version du serveur: 5.6.12-log -- Version de PHP: 5.4.12 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; -- -- Base de données: `shop` -- CREATE DATABASE IF NOT EXISTS `shop` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE `shop`; -- -------------------------------------------------------- -- -- Structure de la table `categories` -- CREATE TABLE IF NOT EXISTS `categories` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(255) NOT NULL, `Description` text NOT NULL, `Ordering` int(11) DEFAULT NULL, `Visibility` tinyint(4) NOT NULL DEFAULT '0', `Allow_Comment` tinyint(4) NOT NULL DEFAULT '0', `Allow_Ads` tinyint(4) NOT NULL DEFAULT '0', PRIMARY KEY (`ID`), UNIQUE KEY `name` (`Name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=13 ; -- -- Contenu de la table `categories` -- INSERT INTO `categories` (`ID`, `Name`, `Description`, `Ordering`, `Visibility`, `Allow_Comment`, `Allow_Ads`) VALUES (8, 'Hand Made', 'Hand Made Items', 1, 0, 0, 0), (9, 'Computers ', 'Computers Item', 2, 0, 0, 0), (10, 'Cell Phones ', 'Cell Phones', 3, 0, 0, 0), (11, 'Clothing ', 'Clothing And Fashion', 4, 0, 0, 0), (12, 'Tools ', 'Home Tools', 5, 0, 0, 0); -- -------------------------------------------------------- -- -- Structure de la table `comments` -- CREATE TABLE IF NOT EXISTS `comments` ( `c_id` int(11) NOT NULL AUTO_INCREMENT, `comment` text NOT NULL, `status` tinyint(4) NOT NULL, `comment_date` date NOT NULL, `item_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`c_id`), KEY `items_comment` (`item_id`), KEY `comment_user` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=83 ; -- -- Contenu de la table `comments` -- INSERT INTO `comments` (`c_id`, `comment`, `status`, `comment_date`, `item_id`, `user_id`) VALUES (17, 'Very Nice ', 1, '2018-03-09', 19, 9), (20, 'This Is Very Nice', 1, '2018-03-08', 20, 10), (24, 'Very Thanks for the Speaker', 1, '2018-03-10', 12, 10), (25, 'Very Good This is Speaker', 1, '2018-03-10', 12, 10), (42, 'TET HAMMER', 1, '2018-03-10', 12, 10), (47, 'Very Cool', 1, '2018-03-10', 12, 10), (48, 'Test Cool HAMMER', 1, '2018-03-14', 19, 10), (50, 'Essai Test HAMMER', 1, '2018-03-10', 12, 10), (51, 'This Is Good Phone ', 1, '2018-03-15', 18, 10), (61, 'Test', 1, '2018-03-10', 12, 10), (62, 'Essai', 1, '2018-03-10', 12, 10); -- -------------------------------------------------------- -- -- Structure de la table `items` -- CREATE TABLE IF NOT EXISTS `items` ( `Item_ID` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(255) NOT NULL, `Description` text NOT NULL, `Price` varchar(255) NOT NULL, `Add_Date` date NOT NULL, `Country_Made` varchar(255) NOT NULL, `Image` varchar(255) NOT NULL, `Status` varchar(255) NOT NULL, `Rating` smallint(6) NOT NULL, `Approve` int(11) NOT NULL DEFAULT '0', `Cat_ID` int(11) NOT NULL, `Member_ID` int(11) NOT NULL, PRIMARY KEY (`Item_ID`), KEY `member_1` (`Member_ID`), KEY `cat_1` (`Cat_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=27 ; -- -- Contenu de la table `items` -- INSERT INTO `items` (`Item_ID`, `Name`, `Description`, `Price`, `Add_Date`, `Country_Made`, `Image`, `Status`, `Rating`, `Approve`, `Cat_ID`, `Member_ID`) VALUES (12, 'Speaker', 'Very Good Speaker', '$10', '2018-03-07', 'China', '', '1', 0, 0, 9, 8), (13, 'Yeti Blue Mic', 'Very Good Microphone Very Good Microphone ', '$108', '2018-03-07', 'USA', '', '1', 0, 0, 9, 8), (15, 'Magic House', 'Apple Magic House ', '$150', '2018-03-07', 'USA', '', '1', 0, 0, 9, 4), (17, 'Game', 'Test Game For Item', '120', '2018-03-09', 'USA', '', '2', 0, 0, 9, 10), (18, 'iPhone 6s', 'iPhone 6s Very Cool Phone', '1500', '2018-03-09', 'USA', '', '1', 0, 0, 10, 10), (19, 'Hammer', 'A Very Good Iron Hammer', '30', '2018-03-09', 'China', '', '2', 0, 0, 12, 10), (20, 'Good Box', 'Nice Hand Made Box', '40', '2018-03-09', 'Egypt', '', '1', 0, 1, 8, 10), (22, 'Game Boy 4', 'Game Boy 4 PS', '15', '2018-03-10', 'Usa', '', '1', 0, 0, 9, 9), (23, 'Network Cable ', 'Cat 9 Network Cable ', '100', '2018-03-10', 'USA', '', '1', 0, 0, 9, 10), (24, 'Microphone 4', 'Good Microphone 4', '20', '2018-03-10', 'China', '', '2', 0, 1, 10, 10), (25, 'SamsungGalaxy S8', 'Very Good Phone Last Generation', '200', '2018-03-10', 'USA', '', '2', 0, 0, 10, 8), (26, 'Samsung S7', 'Samsung Galaxy S7', '150', '2018-03-10', 'USA', '', '2', 0, 0, 10, 10); -- -------------------------------------------------------- -- -- Structure de la table `users` -- CREATE TABLE IF NOT EXISTS `users` ( `UserID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'To Identify User', `Username` varchar(255) NOT NULL COMMENT 'Username To Login', `Password` varchar(255) NOT NULL COMMENT 'Password To Login', `Email` varchar(255) NOT NULL, `FullName` varchar(255) NOT NULL, `GroupeID` int(11) NOT NULL DEFAULT '0' COMMENT 'Identify User Group', `TrustStatus` int(11) NOT NULL DEFAULT '0' COMMENT 'Seller Rank', `RegStatus` int(11) NOT NULL DEFAULT '0' COMMENT 'User Approval', `Date` date NOT NULL, PRIMARY KEY (`UserID`), UNIQUE KEY `Username` (`Username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ; -- -- Contenu de la table `users` -- INSERT INTO `users` (`UserID`, `Username`, `Password`, `Email`, `FullName`, `GroupeID`, `TrustStatus`, `RegStatus`, `Date`) VALUES (1, 'Osee', 'b1cc6d1236555bc8697c08a2e8525f3a28f9ef62', 'osee@yahoo.com', 'Osee KOUAKOU', 1, 0, 1, '0000-00-00'), (4, 'Joel', '9b20e3fda40f19fb383c7e2514d7ed7c0f1f7e8a', 'joel@gmail.com', 'Joel NIKEBIE', 0, 0, 1, '2018-03-06'), (5, 'Oseek', '872460d8d00ac671722d85b0e7009f2668200c6d', 'oseek@gmail.com', 'Oseek KOUAKOU ', 0, 0, 1, '2018-03-06'), (6, 'Koffi', 'd337d00783ddfcf439b6456b2f7d77d5416f7d2e', 'koffi1@gmail.com', 'Koffi KOUAKOU', 0, 0, 1, '2018-03-06'), (7, 'Application', '7d1043473d55bfa90e8530d35801d4e381bc69f0', 'app@app.com', 'Application API', 0, 0, 0, '2018-03-07'), (8, 'MichelO', '181178b1bd9a2e2c445075cad43d010ff0f73295', 'michelo@gmail.com', 'Michel Osée', 0, 0, 1, '2018-03-08'), (9, 'Solange', '41e26370f5c33c92d1ed618bdb6678afda69a625', 'solange@gmail.com', '', 0, 0, 0, '2018-03-09'), (10, 'Michel', 'd86cf0597f44b91b8401b079c7b26728c48c7301', 'michel@gmail.com', '', 0, 0, 1, '2018-03-09'), (11, 'Oseekouakou', '7e8f0575b8d3bdbc742142924b27e2c99c40cb44', 'oseekouakou@gmail.com', '', 0, 0, 0, '2018-03-09'); -- -- Contraintes pour les tables exportées -- -- -- Contraintes pour la table `comments` -- ALTER TABLE `comments` ADD CONSTRAINT `comment_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`UserID`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `items_comment` FOREIGN KEY (`item_id`) REFERENCES `items` (`Item_ID`) ON DELETE CASCADE ON UPDATE CASCADE; -- -- Contraintes pour la table `items` -- ALTER TABLE `items` ADD CONSTRAINT `cat_1` FOREIGN KEY (`Cat_ID`) REFERENCES `categories` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `member_1` FOREIGN KEY (`Member_ID`) REFERENCES `users` (`UserID`) ON DELETE CASCADE ON UPDATE CASCADE; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
-
-
if ($_SERVER['REQUEST_METHOD'] == 'POST') { $comment = filter_var($_POST['comment'], FILTER_SANITIZE_STRING); $itemid = $item['Cat_ID']; $userid = $_SESSION['uid']; if (! empty($comment)) { $stmt = $con->prepare("INSERT INTO comments(comment, status, comment_date, item_id, user_id) VALUES(:zcomment, 0, NOW(), :zitemid, :zuserid)"); $stmt->execute(array('zcomment' => $comment, 'zitemid' => $itemid, 'zuserid' => $userid)); if ($stmt) {cho '<div class="alert alert-success">Comment Added</div>';} -
-
J'ai mis cette ligne en commentaire :
$stmt->execute(array('zcomment' => $comment, 'zitemid' => $itemid, 'zuserid' => $userid));
Et j'ai faisecho $stmt;
, voici le résultat :Catchable fatal error: Object of class PDOStatement could not be converted to string in C:\wamp\www\Admin\items.php on line 100
En laissant cette ligne :$stmt->execute(array('zcomment' => $comment, 'zitemid' => $itemid, 'zuserid' => $userid));
Voici ce que j'obtiens :Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shop`.`comments`, CONSTRAINT `items_comment` FOREIGN KEY (`item_id`) REFERENCES `items` (`Item_ID`) ON DELETE CASCADE ON UPDATE CASCADE)' in C:\wamp\www\Admin\items.php on line 98
-
-
yg_be Messages postés 23437 Date d'inscription Statut Contributeur Dernière intervention Ambassadeur 1 588
bonjour, ta base de données inclut cette contrainte:CONSTRAINT `items_comment` FOREIGN KEY (`item_id`) REFERENCES `items` (`Item_ID`) ON DELETE CASCADE ON UPDATE CASCADE
je suppose que tu sais pourquoi.
et le changement que tu essaies de faire via php ne respecte pas cette contrainte.-
-
-
Okay,
J'ai crée des Items (Exemple: Speaker) et je veux ajouter des commentaires, lorsque j'écris le commentaire, que je valide j'ai le message d'erreur : <code>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shop`.`comments`, CONSTRAINT `items_comment` FOREIGN KEY (`item_id`) REFERENCES `items` (`Item_ID`) ON DELETE CASCADE ON UPDATE CASCADE)' in C:\wamp\www\Admin\items.php on line 98></code
Pour la contrainte, c'est pour que lorsque j'effectue une modification soit Update ou Delete sur le User ou la Catégorie ou ou la Itemcela prenne en compte les tables qui sont liées -
quand je te demande des explications supplémentaires, il est inutile de répéter ce que tu as déjà expliqué, et de redonner les mêmes informations. il est plus utile de donner d'autres informations.
explique-nous ton contexte, qu'essaies-tu de réaliser? modifies-tu un programme qui fonctionnait, crées-tu un nouveau programme, une nouvelle base de données?
ensuite, explique aussi quelle requête SQL tu exécutes en ligne 98. insert ou update, dans quelle table?
la contrainte fait plus que ce que tu décris: elle empêche de créer des enregistrements non liés (Cannot add or update a child row). -
-