8 Replies - 308 Views - Last Post: 07 November 2017 - 02:45 PM Rate Topic: -----

#1 moh3en  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-July 14

Simple Forms that Added User to Database

Posted 07 November 2017 - 01:39 PM

Hello, I want make Simple From for Added User to Database

What's Problem i should fix it ? Can you help me ?

<form action="register.php" method="post">
	<label for="firstname">First Name</label> <input type="text" name="firstname" id="firstname"></p>
	<label for="lastname">Last Name</label> <input type="text" name="lastname" id="lastname"></p>
	<label for="email">Email Address</label> <input type="text" name="email" id="email"></p>
	<label for="username">Username</label> <input type="text" name="username" id="username"></p>
	<input type="submit" name="submit" value="Submit">
</form>


<?php
require "config.php";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
	die("Connection failed: " . mysqli_connect_error());
}
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
extract($_POST, EXTR_PREFIX_ALL, 'p');
if ($_POST==TRUE)
$sql="INSERT INTO register(name , firstname, lastname , email) Value ('$p_username','$p_firstname','$p_lastname','$p_email')";
if ($conn->query($sql) == TRUE)
{
	echo "WELCOME $p_firstname , Successfully added.";
}
	else 
	{
		echo "Error.$sql <br/>";
	}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Simple Forms that Added User to Database

#2 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 131
  • Joined: 28-March 15

Re: Simple Forms that Added User to Database

Posted 07 November 2017 - 01:53 PM

Quote

What's Problem i should fix it


You can start here. This will teach you how to properly post on a coding forum.
How To Ask Questions The Smart Way
Was This Post Helpful? 1
  • +
  • -

#3 moh3en  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-July 14

Re: Simple Forms that Added User to Database

Posted 07 November 2017 - 01:59 PM

I mean for coding problem. It's basic code and i'm starter in php.

This post has been edited by Dormilich: 08 November 2017 - 01:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,847
  • Joined: 12-June 08

Re: Simple Forms that Added User to Database

Posted 07 November 2017 - 02:01 PM

I believe they mean you forgot to ask a question.
Was This Post Helpful? 1
  • +
  • -

#5 moh3en  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-July 14

Re: Simple Forms that Added User to Database

Posted 07 November 2017 - 02:05 PM

Yes, I know but i think when see code can help me for fix problem!

First in register.php when user refresh page Automatic added data to database

Second in form.php can i this form put in my site? maybe hack my sql with this code ?

This post has been edited by Dormilich: 08 November 2017 - 01:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 131
  • Joined: 28-March 15

Re: Simple Forms that Added User to Database

Posted 07 November 2017 - 02:11 PM

First, all your code should be in one page with Php at the top and html below that. You need to use Prepared statements. Do not ever put variables in a query and do not output internal errors to the user. That info is only good for hackers. Do not use EXTRACT.

I suggest you learn PDO. You will also need to learn about PRG
Was This Post Helpful? 1
  • +
  • -

#7 moh3en  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-July 14

Re: Simple Forms that Added User to Database

Posted 07 November 2017 - 02:15 PM

Thank you. Why no mysqli ? why i should learn PDO ?

why not use EXTRACT ?

This post has been edited by Dormilich: 08 November 2017 - 01:34 AM

Was This Post Helpful? 0
  • +
  • -

#8 benanamen  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 131
  • Joined: 28-March 15

Re: Simple Forms that Added User to Database

Posted 07 November 2017 - 02:27 PM

PDO is better and easier to use properly. EXTRACT creates "magic" variables that appear out of nowhere. You already have the POST variables, just use them. You are basically creating variables for nothing but worse since they just "magically" appear. It does the same thing Register_globals did which has been completely removed from Php hundreds of versions ago.

This post has been edited by benanamen: 07 November 2017 - 02:30 PM

Was This Post Helpful? 1
  • +
  • -

#9 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Simple Forms that Added User to Database

Posted 07 November 2017 - 02:45 PM

You also have no validation at all. If someone just submits an empty form, you want to add that to the database?

If you want to avoid refresh causing the form data to be submitted again, then after you've processed the form you need to redirect them. It doesn't matter if you redirect to the same URL, you just need to redirect to remove the post data from their last request.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1