-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaceholderposts.php
More file actions
37 lines (30 loc) · 1.14 KB
/
placeholderposts.php
File metadata and controls
37 lines (30 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
// שם הקובץ
$fileName = 'C:\Users\user\Desktop\PHP\assets\posts.json';
// קריאת תוכן הקובץ
$content = file_get_contents($fileName);
// בדיקת שגיאה
if ($content === false) {
$error = error_get_last();
echo "שגיאה: " . $error['message'];
} else {
// המרת הנתונים מ-JSON למערך PHP
$postsData = json_decode($content, true);
// יצירת קשר לבסיס הנתונים
$pdo = new PDO("mysql:host=localhost;dbname=inmange", "root", "");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// הכנסת נתונים לטבלה
foreach ($postsData as $post) {
$insertData = "INSERT INTO Posts
(id, user_id, title,content)
VALUES (:id, :user_id, :title, :content)";
$stmt = $pdo->prepare($insertData);
$stmt->bindParam(':id', $post['id']);
$stmt->bindParam(':user_id', $post['userId']);
$stmt->bindParam(':title', $post['title']);
$stmt->bindParam(':content', $post['body']);
$stmt->execute();
}
echo "נתונים הוכנסו בהצלחה לטבלה.";
}
?>