Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.