Jump to content

[Guide]PHP & MySQL


WizZy™

Recommended Posts

Well its a small guide ,but you can use this for a shit of things.Its a simple form that inserts data in SQL and then show's it to the admin (example).You can do ALOT of things like i did with it ,its so fucking simple and its used alot.An example is this : click.I used it as a L2 Ticketing system for reporting bugs blabla ,but if you have a good imagination you can use it for news (example).Insert the record to MySQL and then show it to the readers :) So lets see how this thingie works

 

First make a connection to the database.I recommend for all to make a other file called "config.php" or something similiar and there will be all values for connecting etc.So here's how config file should look :

<?php
$user = "root"; // Database user
$pass = "112233"; // Password for the database
$db = "dot"; // Database name
$host = "localhost"; // IP/Host to connect to the database
$connection = mysql_connect($host, $user, $pass) or die("Cant connect to database1"); // Lets try to connect ,if not die();
$db = mysql_select_db($db,$connection)or die("Cant connect to database2"); // If first connection is successful ,lets conncet to the database name ,if error die();
$chars="set character set latin1"; //Charset .. You can use this if you have some problems with the encoding of your character
mysql_query($chars); // To send the encoding of the character type (latin1 type)
?> 

 

That was a simple connection method ,and the most used though.

 

Second step.How to insert to the database?

Well now to establish the connection you can use this code to include the configuration file for sql

<?php include "FILENAME.php"; ?> 

Okay now we have a database connection ,lets proceed to the importing into it.

The import way is easy too ,but pls dont copy/paste it ,so you can learn it faster :)

Here's how the submit form should look .. With explames in the file :)

  <form method="POST">
</p>
<p>Value1: <br />
  <input name="value" type="text" /> 
  *
</p>
  <input type="submit" name="button" value="Submit it!">
  <?php
include('FILENAME.php'); // This is the config file name.Replace it with your config file name in order to connect into database 
if($_POST['button']){ // Here is the button "Submit".It has a name "button" you can change it ,but it its the same.Leave it as it is
$value=$_POST['value']; // This is the first value that we will insert.. If you wish to add more ,look above.
//$ot=$_POST['value1'];
//$izp=$_POST['value2];
//$pesen=$_POST['value3'];
//$screen=$_POST['value4'];
//$comment=$_POST['value5'];
$query = mysql_query("INSERT INTO `table` (value) VALUES('$value')") or die(mysql_error()); // We insert the value we want.For an example i inserted the "VALUE" name ,so its named as $value.
echo "Added successfuly.<br><a href=\"index.php\">Go to the index page..</a>";}?> // Successfuly added ,return to index ^^

 

Okay lets give explanations now.

  <input name="value" type="text" />

This is the value submitted.You can configurate this ,but you also gotta change the sql insert :

INSERT INTO `table` (value) VALUES('$value')")

which is configurated by

$value=$_POST['value'];

If you need more than 1 value ,you can remove the // before the $value1 value and it will be enabled.But you gotta insert it in the INSERT INTO syntax too and has to look something like this :

$query = mysql_query("INSERT INTO `table` (value,value1) VALUES('$value', '$value1')") or die(mysql_error());

 

Okay i think you got it.

Now how to view our already posted records in database?

Well if its successful ,you can make a file called view.php (example ,you can name it w/e u want)

Into this file lets export the records in this way .. Example of the file :

<?php include "conf.php"; // Change it to your config file name 
?>
<?php $q = mysql_query("SELECT * from TABLE order by id DESC"); // Getting the query
while($d = mysql_fetch_assoc($q)) {
		 echo 'Value is : '.$d['value'].''; } ?>

Lets see what we need to change

First the config file name (where are the database info's)

Then change the table which the file will connect

mysql_query("SELECT * from TABLE order by id DESC");

P.S. If you got a ORDER BY id error ,remove the "order by id" text :)

Okay ,everything we got ready from php side ,lets view the sql code now :

CREATE TABLE `TABLE NAME` (
`value` varchar(999) collate latin1_swedish_ci NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=376 ; 

On the TABLE NAME write your table name that this code will create.

Execute it with navicat or w/e you are using ,and you're done :)

 

Extra scripts :

 

How to write the users IP when they submit?

Simple :) If you read that guide (all of it) you already know how to insert a new value.So make 1 ,and another colum in SQL by this code :

ALTER TABLE tablename
ADD columname varchar(250);

and execute in sql after you configurate it by your needs.

So make a new value in the php (with inserting in database) and @ put this in the start of the site :

<?php $ip=$_SERVER['REMOTE_ADDR']; ?>

and then insert the value as $ip in the sql insert syntax.

 

How to record all visitors IP's in a text file :

<?php
$file = fopen('ipadmin.txt', 'a'); // opens ,create a new file with storage
fwrite($file,$_SERVER['REMOTE_ADDR']."\n"); // Ip storing
fclose($file); // Close the file
echo "<br><b>Your ip was stored successfuly in ipadmin.txt file.</b>"; // if successful
?>

If you dont want your visitors not to understand that they'r ip is recorded ,remove this

echo "<br><b>Your ip was stored successfuly in ipadmin.txt file.</b>"; // if successful

 

Thanks for all who saw it ^^

 

The guide is MINE and its wroten specialy for the programmers in worldb0x ,but i shared it here too :)

Thanks for reading!

Link to comment
Share on other sites

Nice one for the newcomers.

If someone wants to gain more knowledge about this or other useful things, visit www.w3schools.com [server Scripting].

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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