Jump to content

How to make a SIMPLE PHP script with MySql


gamercross

Recommended Posts

I wanted to create a field that when someone enters a word such as hello, its goes to a page called: http://www.myurl.com/hello

Although it seems complicated it is not, because i would of previously created these values in the database.

For example: i add a new table to my database. Add an ID, and keyword which will be hello and then add a url which will be http://www.myurl.com/hello

If someone enters helloeveryone, it will say please search or enter again, this is because that has not been entered yet in my database.

 

Once i have entered all the keywords and urls i want in my database, i will need a php form and a connection script, which i believe is a CMS? Content Management System?

 

I am a total newbie to php, i know some stuff like i have mentioned above but not with creating scripts so would be extremely grateful if anyone could help me and would be happy to add their url on my site.  :)

 

Thanks,

 

Gamercross

 

Gamercross.co.uk

 

Link to comment
Share on other sites

I'd highly suggest you start reading some books on php basics, get familiar with how it works. Do some tutorials and run some practices and then get involved in databases. You really should understand the basics and then you will be able to make a script like that no problem.

Link to comment
Share on other sites

I would try something like this:

 

<?
if(isset($_POST['url'])){

$url = $_POST['url'];

$query = "SELECT `id`,`url` FROM `tbl` WHERE `keyword` = '$url'";
$run = mysql_query($query);
$num = mysql_num_rows($run);

if($num > 0){
$arr = mysql_fetch_assoc($run);
extract($arr);
header("Location: $url");

}else{
echo "<script type=\"text/javascript\">alert('Unable to find url!');</script>";
}

}
?>

<form name="redirectForm" id="redirectForm" action="index.php" method="post" enctype="multipart/form-data">
URL: <input type="text" size="20" id="url" name="url" />
<br /><br />
<input type="submit" value="Submit" />
</form>

 

This is obviously a very simple example, but it should demonstrate what you have to do.

Link to comment
Share on other sites

Create Table in Databse

 

CREATE TABLE `keywords` (

  `id` int(225) NOT NULL auto_increment,

  `keywords` text NOT NULL,

  `url` text NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

 

It has Keywords table in which you will be able to save URL + Keywords

 

Now Create Page add_url.php

 

and add

 

<?php
$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'root';
$dbpass = '';

$connect = mysql_connect($dbhost,$dbuser,$dbpass);
if (!mysql_select_db($dbname)) die(mysql_error());

if(isset($_POST['button']){
$keywords = $_POST['keyoword'];
$url = $_POST['url'];
mysql_query('INSERT INTO keywords(keywords,url)VALUES("'.$keywords.'","'.$url.'")');
echo 'Url has been Added';
}

?>
<form id="form1" name="form1" method="post" action="">
  <label>
  Url:
  <input type="text" name="url" id="url" />
  </label>
  <br />
  Keywords :
  <label>
  <input type="text" name="keywords" id="keywords" />
  </label>
  <br>
  <label>
  <input type="submit" name="button" id="button" value="Submit">
  </label>
  <br />
  <label></label>
</form>

 

Now Create another Page

 

show_urls.php

 

and add following code

 

<?php

$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'root';
$dbpass = '';

$connect = mysql_connect($dbhost,$dbuser,$dbpass);
if (!mysql_select_db($dbname)) die(mysql_error());

$query = mysql_query('SELECT * FROM keywords');
while($row = mysql_fetch_array($query)){
echo '<a href='.$row['url'].'>'.$row['url'].'</a>';
}

?>

 

This will show all that links you have added now we gonna add a form through which only relavant urls shown, Npw modify the code as given below

 

<?php

$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'root';
$dbpass = '';

$connect = mysql_connect($dbhost,$dbuser,$dbpass);
if (!mysql_select_db($dbname)) die(mysql_error());

if(isset($_POST['button']){
$keyword = $_POST['keyoword'];	
$query = mysql_query('SELECT * FROM keywords WHERE keywords like "$keyword"');

while($row = mysql_fetch_array($query)){
echo '<a href='.$row['url'].'>'.$row['url'].'</a>';
}
}

?>
<form name="form1" method="post" action="">
  <label>Enter Keyword:<br>
  <input type="text" name="keyword" id="keyword">
  <br>
  <input type="submit" name="button" id="button" value="Submit">
  <br>
  </label>
</form>

 

now thats it, hope this helps you alot, reply if you found any problem , i havent tested it yet :P

Link to comment
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.