TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Israel
701
1.3k
217.1k
It's saving very well without bug but datas doesnt appear into DB
Aug 27 2020 12:23 PM
Hi,
I am learning PHP. But I am trying to save into my mysql Database.
When I click on the save button its saving without bugg's message but my DB still blank. I do verify all the connection's functions (there is not problem). But my DB doesnt receive any datas. Can I have a solution please?
index.php
<?php
include
(
'server.php'
); ?>
<html>
<head>
<title>Add to create, update,
delete
Database records</title>
<link rel=
"stylesheet"
type=
"text/css"
href=
"style.css"
>
</head>
<body>
<?php
if
(isset(
$_SESSION
[
'msg'
])): ?>
<div
class
=
"msg"
>
<?php
echo
$_SESSION
[
'msg'
];
unset(
$_SESSION
[
'msg'
]);
?>
</div>
<?php
endif
?>
<table>
<thread>
<tr>
<th>Name</th>
<th>Address</th>
<th colspan=
"2"
>Action</th>
</tr>
</thread>
</tbody>
<?php
while
(
$row
= mysqli_fetch_array(
$results
)) ?>
<tr>
<td><?php
echo
$row
[
'name'
]; ?></td>
<td><?php
echo
$row
[
'address'
]; ?></td>
<td>
<a href=
"#"
>Edit</a>
</td>
<td>
<a href=
"#"
>
Delete
</a>
</td>
</tr>
<?php ?>
</tbody>
</table>
<form method=
"post"
action=
"server.php"
>
<div
class
=
"input-group"
>
<label>Name</label>
<input type=
"text"
name=
"name"
>
</div>
<div
class
=
"input-group"
>
<label>Address</label>
<input typr=
"type"
name=
"address"
>
</div>
<div
class
=
"input-group"
>
<button type=
"submit"
name=
"save"
class
=
"btn"
>Save</button>
</div>
</form>
</body>
</html>
server.php
<?php
// initialize variables
$name
=
""
;
$address
=
""
;
$id
= 0;
// connect to database
$db
= mysqli_connect(
'localhost:3307'
,
'root'
,
''
,
'crud1'
);
// if save button is clicked
if
(isset(
$_POST
[
'save'
])) {
$name
=
$_POST
[
'name'
];
$address
=
$_POST
[
'address'
];
$query
=
"INSERT INTO info (name, address) VALUES ('$name', '$address')"
;
mysqli_query(
$db
,
$query
);
$_SESSION
[
'msg'
] =
"Address saved"
;
header(
'location: index.php'
);
// redirect to index page after inserting
}
// retrieve records
$results
= mysqli_query(
$db
,
"SELECT * FROM info"
);
?>
style.css
body {
font-size
:
19px
;
}
table{
width
:
50px
;
margin
:
30px
auto
;
border-collapse
:
collapse
;
text-align
:
left
;
}
tr{
border-bottom
:
1px
solid
#cbcbcb
;
}
th, td{
border
:
none
;
height
:
30px
;
padding
:
2px
;
}
tr:hover{
background
:
#f5f5f5
;
}
form{
width
:
45%
;
margin
:
50px
auto
;
text-align
:
left
;
padding
:
20px
;
border
:
1px
solid
#bbbbbb
;
border-radius:
5px
;
}
.input-group{
margin
:
10px
0px
10px
0px
;
}
.input-group label{
display
:
block
;
text-align
:
left
;
margin
:
3px
;
}
.input-group input{
height
:
30px
;
width
:
93%
;
padding
:
5px
10px
;
font-size
:
16px
;
border-radius:
5px
;
border
:
1px
solid
gray
;
}
.btn {
padding
:
10px
;
font-size
:
15px
;
color
:
white
;
background
:
#5F9EA0
;
border
:
none
;
border-radius:
5px
;
}.msg{
margin
:
30px
auto
;
padding
:
10px
;
border-radius:
5px
;
color
:
#3c763d
;
background
:
#dff0d8
;
width
:
50%
;
text-align
:
center
;
}
Reply
Answers (
4
)
What is the problem with the code below? What will it output?
Update doest work?