Jump to content

Insert Data


prinzzchyz

Recommended Posts

Hi

 

 

I have a first project for my self is to create a simple WEBSite

 

I have a scripts that it can add new person and notify if added already

 

the problem is that even i refresh the page the content of the database increases

 

 

for example if I add Firstname Joe and the last name is MOnkey it will added

to the database.

then if I refresh the page it will also added to the database but it is blank

 

 

 

please can anyone help me if my codes is correct,

 

 

 

Im planning to make a COMMENT Menu in my website

 

Firstname:

Last name:

Email:

Comments:

 

 

then save button

 

 

 

Code:

<?php

 

include('connect_db.php');

 

$fname = $_POST['fname'];

$lname = $_POST['lname'];

$sqlinsert = "INSERT INTO people (firstname, lastname) VALUES ('$fname', '$lname')";

 

if (!mysqli_query($dbcon, $sqlinsert)) {

die('error inserting new record');

} // end of my nested if statement

 

$newrecord = "1 record added to the database";

 

?>

 

<html>

<head>

<title>Insert Data into DB</title>

</head>

<body>

<h1>Insert Data into DB</h1>

 

<form method="post" action="insert-data.php">

<input type="hidden" name="submitted" value="true" />

<fieldset>

<legend>New People</legend>

<label>First Name:<input type="text" name="fname" /></label>

<label>Last Name:<input type="text" name="lname" /></label>

</fieldset>

<br />

<input type="submit" value="add new person" />

</form>

 

<?php

echo $newrecord

?>

 

</body>

</html>

 

 

 

 

Thanks in Advance

Link to comment
https://forums.phpfreaks.com/topic/271284-insert-data/
Share on other sites

your problem is that you are not checking to see if the form has been submitted before running the insert, you also have no validation or sanitization on the form inputs. As a starting point it's not three bad though, also please use code tags when posting code - it makes everything much easier to read.

Link to comment
https://forums.phpfreaks.com/topic/271284-insert-data/#findComment-1395844
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.