Basic getting info script, possibilities are endless!

With a script like this you can build off it. For example, you can get e-mail addresses, phone numbers, contact info, you can get user feedback, just by editing it, and using your imagination. This is using PHP + MYSQL. This example is acquiring the names of people. Here’s the script in full:

First make a sql query.

CREATE TABLE name (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(60))

Then create a file, something.php and implement this code.

[code] <?php
//Start Script

// Connects to your Database
mysql_connect("localhost", "username", "pass")
or die(mysql_error());
mysql_select_db("database name")
or die(mysql_error()); 
//Connects to your Database end


//Echoing some stuff
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<center>";
echo "What's Up?<br>";
echo "Hello<br>";
echo "Hi";
echo "<br>";
echo "<br>";
echo "<br>";
//Echoing some stuff end
?>

<!--HTML FORM-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Name:</td><td>
<input type="text" name="name" maxlength="60">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Submit"></th></tr> </table>
</form>
<!--HTML FORM End-->


<?php

//If you already submitted the form
if (isset($_POST[‘submit’])) {
if (!$_POST[‘name’]) {
die(‘You did not complete all of the required fields’);
}
//If you already submitted the form end

//Inserts data in DataBase

$insert = “INSERT INTO name (name)
VALUES (’”.$_POST[‘name’]."’)";
$add_member = mysql_query($insert);
//Inserts data in DataBase end

//End Script

}
?>[/code]

Explantion:

1. Connect to Database
This part is self-explanatory. In your cpanel account or what you make you should have an option to create a database, and a user + pass for the database. Most hosts use a localhost, if it gives an error the problem is usually it’s not localhost, your server will usually display the info if it’s not localhost.

2. Echoing
This is just general stuff. You could use the “<? echo "text"; ?>” command, or regular HTML if you prefer, or the “<? print "text"; ?>” does the same as echo, some say it’s slightly faster to use echo though.

3. HTML Form
This is basic HTML, the name part is the most important part of the form. Without that the $_POST variable wont be able to recognize what info to grab.

4. Already submitted form
The isset variable in PHP tells whether a variable has been set or not. In this case “<? (isset($_POST['submit'])) { } ?>” command checks if you already pressed submit or not. simple. The next part is the “<? if (!$_POST['name']) ?>” command. It’s easy when you break it down if is the start to the if statement, it checks conditions. the exclamation point in php pretty much means not so all together it says IF nothing is submitted for name{ die('You did not complete all of the required fields'); } it will echo the line above.

5. Inserting into the databse
This is where it’s stored. This line "<? $insert = "INSERT INTO name (name) … " is self explanatory, the $insert variable means to insert into name (name) i.e. insert the data into name (table) and name (feild)

You see the MySQL database is set up like

TABLE: name
inside the table name there’s a feild called name

Now this VALUES (’".$_POST[‘name’]."’)"; means insert the values from the input box named name.

$add_member = mysql_query($insert); … This means to finally insert the data in the databse.

:)

-PhpCoder

Sponsor our Newsletter | Privacy Policy | Terms of Service