mysqli_fetch_assoc
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\USB WebServer\root\Youtube9\toon.php on line 77
Die hadden we al een keer. Iemand die me deze functie kan uitleggen? (En mogelijke oplossingen heeft)
Code
Code (php)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
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
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
https://www.php.net/manual/en/mysqli-result.fetch-assoc.php
Toevoeging op 18/05/2022 19:05:36:
Vermoedelijk is de query niet goed .
Haal de ‘’ rond registration eens weg.
En dan dit
De bedoeling is al volgt:
Controleer of de query fout is of anders uitgevoerd kan worden:
Indien dit false geeft: Geeft een error met echo mysqli_error($con)
Indien dit anders geen false geeft (een resource om precies te zijn), ga verder met je data ophalen, en ga aan de slag met mysqli_fetch_assoc(). Je kan eventueel nog eerst met mysqli_num_rows() kijken of het record bestaat.
Als je het op deze logische manier je queries opbouwt, dan kom je nooit voor opvolgende verrassingen te staan.
Gewijzigd op 18/05/2022 19:45:35 door - Ariën -
mysqli_result Object ( [current_field] => 0 [field_count] => 4 [lengths] => [num_rows] => 8 [type] => 0 )
De code
Code (php)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
<?php
$sql = 'SELECT * FROM registration ORDER BY ID DESC';
// print_r($sql);
$result = mysqli_query($con, $sql);
print_r ($result);
while($row = mysqli_fetch_assoc($result))
/* {
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td>'.$image.'</td>
</tr>';
}
*/
?>
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
<?php
$sql = 'SELECT * FROM registration ORDER BY ID DESC';
// print_r($sql);
$result = mysqli_query($con, $sql);
print_r ($result);
while($row = mysqli_fetch_assoc($result))
/* {
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td>'.$image.'</td>
</tr>';
}
*/
?>
Maar wat is de oplossing?
Met het verwijderen van die quotes om je tabelnaam zal het denk ik wel moeten werken. Maar zorg gewoon voor goede logica die je systematisch toevoegt.
Gewijzigd op 18/05/2022 19:47:04 door - Ariën -
Code (php)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Data</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<body>
<h1 class = "text-center my-4">User Data</h1>
<dic class= "container mt-5 flex justify-content-center"></dic>
<center><table class="table table-bordered w-50">
<thead>
<tr>
<th scope="col">sl no</th>
<th scope="col">Username</th>
<th scope="col">Image</th>
</tr>
</thead>
<tbody>
<?php
$sql = 'SELECT * FROM registration ORDER BY ID ASC';
// print_r($sql);
$result = mysqli_query( $con, $sql);
//print_r($result);
while($row = mysqli_fetch_assoc($result))
{
$id=$row['id'];
$name=$row['name'];
$image=$row['image'];
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td><img src='.$image.'/></td>
</tr>';
}
?>
</tbody>
</table></center>
</body>
</html>
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Data</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<body>
<h1 class = "text-center my-4">User Data</h1>
<dic class= "container mt-5 flex justify-content-center"></dic>
<center><table class="table table-bordered w-50">
<thead>
<tr>
<th scope="col">sl no</th>
<th scope="col">Username</th>
<th scope="col">Image</th>
</tr>
</thead>
<tbody>
<?php
$sql = 'SELECT * FROM registration ORDER BY ID ASC';
// print_r($sql);
$result = mysqli_query( $con, $sql);
//print_r($result);
while($row = mysqli_fetch_assoc($result))
{
$id=$row['id'];
$name=$row['name'];
$image=$row['image'];
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td><img src='.$image.'/></td>
</tr>';
}
?>
</tbody>
</table></center>
</body>
</html>
$result was niet goed! Daarvoor foutmeldingen. Echter hij toont mijn plaatjes nog steeds niet
Zie bijgaand:
https://ibb.co/3Wpx64W
https://ibb.co/bdq3tx0
Wat doe ik nog niet goed?
Code (php)
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
38
39
40
41
42
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
38
39
40
41
42
<tbody>
<tr>
<td>1</td>
<td>ed</td>
<td><img src=images/Arien.png/></td>
</tr>
<tr>
<td>2</td>
<td>ed</td>
<td><img src=images/Arien.png/></td>
</tr>
<tr>
<td>3</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>4</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>5</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>6</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>7</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>8</td>
<td>ED</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>1</td>
<td>ed</td>
<td><img src=images/Arien.png/></td>
</tr>
<tr>
<td>2</td>
<td>ed</td>
<td><img src=images/Arien.png/></td>
</tr>
<tr>
<td>3</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>4</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>5</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>6</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>7</td>
<td>ed</td>
<td><img src=images/Arien5.png/></td>
</tr>
<tr>
<td>8</td>
<td>ED</td>
<td><img src=images/Arien5.png/></td>
</tr>
De code tot nu toe
Code (php)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Data</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<body>
<h1 class = "text-center my-4">User Data</h1>
<dic class= "container mt-5 flex justify-content-center"></dic>
<center><table class="table table-bordered w-50">
<thead>
<tr>
<th scope="col">sl no</th>
<th scope="col">Username</th>
<th scope="col">Image</th>
</tr>
</thead>
<tbody>
<?php
$sql = 'SELECT * FROM registration ORDER BY ID ASC';
// print_r($sql);
$result = mysqli_query( $con, $sql);
//print_r($result);
while($row = mysqli_fetch_assoc($result))
{
$id=$row['id'];
$name=$row['name'];
$image=$row['image'];
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td><img src='.$image.'/></td>
</tr>';
}
?>
</tbody>
</table></center>
</body>
</html>
@ Arien is er ook een aparte code om de |HTML beter zichtbaar te krijgen?
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Data</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<body>
<h1 class = "text-center my-4">User Data</h1>
<dic class= "container mt-5 flex justify-content-center"></dic>
<center><table class="table table-bordered w-50">
<thead>
<tr>
<th scope="col">sl no</th>
<th scope="col">Username</th>
<th scope="col">Image</th>
</tr>
</thead>
<tbody>
<?php
$sql = 'SELECT * FROM registration ORDER BY ID ASC';
// print_r($sql);
$result = mysqli_query( $con, $sql);
//print_r($result);
while($row = mysqli_fetch_assoc($result))
{
$id=$row['id'];
$name=$row['name'];
$image=$row['image'];
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td><img src='.$image.'/></td>
</tr>';
}
?>
</tbody>
</table></center>
</body>
</html>
@ Arien is er ook een aparte code om de |HTML beter zichtbaar te krijgen?
Complete code:
Code (php)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Data</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<body>
<h1 class = "text-center my-4">User Data</h1>
<dic class= "container mt-5 flex justify-content-center"></dic>
<center><table class="table table-bordered w-50">
<thead>
<tr>
<th scope="col">sl no</th>
<th scope="col">Username</th>
<th scope="col">Image</th>
</tr>
</thead>
<tbody>
<?php
$sql = 'SELECT * FROM registration ORDER BY ID ASC';
// print_r($sql);
$result = mysqli_query( $con, $sql);
//print_r($result);
while($row = mysqli_fetch_assoc($result))
{
$id=$row['id'];
$name=$row['name'];
$image=$row['image'];
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td><img src="'.$image.'"/></td>
</tr>';
}
?>
</tbody>
</table></center>
</body>
</html>
include ('./connect.php');
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$mobile = $_POST['mobile'];
$image = $_FILES['file'];
//echo $username;
//echo "<br>";
//echo $mobile;
//echo "<br>";
//print_r($image);
$imagefilename=$image['name'];
//print_r($imagefilename);
//echo "<br>";
$imagefileerror=$image['error'];
//print_r($imagefileerror);
//echo "<br>";
$imagefiletemp=$image['tmp_name'];
//print_r($imagefiletemp);
//echo "<br>";
$filaname_seperate=explode('.', $imagefilename);
//print_r($filaname_seperate);
$file_extension=strtolower(end($filaname_seperate));
//print_r($file_extension);
$extension=array('jpeg', 'jpg', 'png');
if(in_array($file_extension,$extension))
{
$upload_image='images/'.$imagefilename;
move_uploaded_file($imagefiletemp, $upload_image);
$sql = "INSERT INTO `registration` (name, mobile, image) VALUES ('$username','$mobile', '$upload_image')";
// $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')";
$result=mysqli_query($con, $sql);
if($result)
{
echo '<div class="alert alert-success" role="alert"><strong>Succesfully </strong>Data inserted succesfully</div>';
}
else
{
die(mysqli_error($con));
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Data</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<body>
<h1 class = "text-center my-4">User Data</h1>
<dic class= "container mt-5 flex justify-content-center"></dic>
<center><table class="table table-bordered w-50">
<thead>
<tr>
<th scope="col">sl no</th>
<th scope="col">Username</th>
<th scope="col">Image</th>
</tr>
</thead>
<tbody>
<?php
$sql = 'SELECT * FROM registration ORDER BY ID ASC';
// print_r($sql);
$result = mysqli_query( $con, $sql);
//print_r($result);
while($row = mysqli_fetch_assoc($result))
{
$id=$row['id'];
$name=$row['name'];
$image=$row['image'];
echo '
<tr>
<td>'.$id.'</td>
<td>'.$name.'</td>
<td><img src="'.$image.'"/></td>
</tr>';
}
?>
</tbody>
</table></center>
</body>
</html>
Mooi :-)
(en daarna met Bobby Tables, https://imgs.xkcd.com/comics/exploits_of_a_mom.png )
Gewijzigd op 19/05/2022 13:34:01 door Ivo P
...en om deze reden dus mysqli_real_escape_string(....) toepassen voordat iemand per ongeluk of expres je query om zeep helpt, met mogelijk gevaren van dien!
Ik ben er nog niet uit!
Code (php)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
// Create database connection
$db = mysqli_connect("localhost", "root", "usbw", "photos");
// Initialize message variable
$msg = "";
// If upload button is clicked ...
if (isset($_POST['upload'])) {
// Get image name
$image = $_FILES['image']['name'];
// Get text
$image_text = mysqli_real_escape_string($db, $_POST['text']);
// image file directory
$target = "images/".basename($image);
//query
$sql = "INSERT INTO images (image, image_text) VALUES ('$image', '$text')";
// execute query
mysqli_query($db, $sql);
//file upload
if (move_uploaded_file($_FILES['image']['tmp_name'], $target))
{
$msg = "Image uploaded successfully";
}else
{
$msg = "Failed to upload image";
}
}
$result = mysqli_query($db, "SELECT * FROM images");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Upload</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id ="content">
<form method="POST"action = "index.php" enctype = "multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols = "40" rows = "4"placeho;der = "Say something about this image..."></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image">
</div>
</form>
</div>
</body>
</html>
// Create database connection
$db = mysqli_connect("localhost", "root", "usbw", "photos");
// Initialize message variable
$msg = "";
// If upload button is clicked ...
if (isset($_POST['upload'])) {
// Get image name
$image = $_FILES['image']['name'];
// Get text
$image_text = mysqli_real_escape_string($db, $_POST['text']);
// image file directory
$target = "images/".basename($image);
//query
$sql = "INSERT INTO images (image, image_text) VALUES ('$image', '$text')";
// execute query
mysqli_query($db, $sql);
//file upload
if (move_uploaded_file($_FILES['image']['tmp_name'], $target))
{
$msg = "Image uploaded successfully";
}else
{
$msg = "Failed to upload image";
}
}
$result = mysqli_query($db, "SELECT * FROM images");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Upload</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id ="content">
<form method="POST"action = "index.php" enctype = "multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols = "40" rows = "4"placeho;der = "Say something about this image..."></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image">
</div>
</form>
</div>
</body>
</html>
Ik krijg geen foutmeldingen maar het plaatje wordt niet geupload! Wat doe ik toch fout. Help aub!!!
Kijk ook eens naar $_FILES['image']['error']
Verder zie ik ook een hoop fouten met variabelen waar je niks mee doet.
Laten we maar in dit topic verder gaan. Die ging over het uploaden (gemengd met je fetch_assoc), deze sluit ik maar.
Gewijzigd op 26/05/2022 23:01:34 door - Ariën -