Jump to content

User input to mysql db


martinstan

Recommended Posts

Hi All

I'm a designer wanting to learn some PHP. I've gained a basic grasp of PHP and can I suppose 'hack' code that has already been written but can't write any code that would be much use at the minute. I'm looking for a tutorial that would allow me to create a form that could be used to collect basic user info. (Name, address, Tel etc..), store this in a Mysql db and then allow me to search by field. A 'bog standard' process using something like Access but I'd like this to be able to be used online.

 

I'd appreciate either a tutorial as to where to start or even better some ready made code that I could hack around with.

 

Thanks in advance :)

Link to comment
https://forums.phpfreaks.com/topic/56066-user-input-to-mysql-db/
Share on other sites

Well as im feeling in a good mood  ;D

 

create a file called form.php

 

Create a database using phpmyadmin on your server and a table named details with 3 fields

 

detail_id  // This should be set up as an auto increment field

name

address


<?php

if(isset($_POST['submit'])){ // This checks to see if the submit button has been pressed.

$name = $_POST['name']; //note the index in between the [] is the name of the inputs
$address = $_POST['address'];

$insert = mysql_query("INSERT INTO details (name, address) VALUES ('$name', '$address')");

if($insert){

echo 'Details added succesfully';

} else {

echo ' There was a problem updating the database';
 }
} else { 
// Here we close the original if statement checking if submit had been pressed.  If it hasn't then 
//the form will display.

?> //closing the php to display html
<form name="form1" action="form.php" method="post">
 <input type="text" name="name" />
 <input type="address name="address" />
 <input type="submit" name="submit" />
</form>

<?php
}
?>  
// this closes the else statement

 

You should also look into sql injection.  The data entered in a form can be used to edit details or even destroy tables in your database. There are a number of function to clean the data you collect in form to check it isn't mailicious. But that is a whole ne ball game.  

 

Is that any good  ;)

 

 

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.