0

I am new to this website and also new in php..

I just want to ask on how to check a specific data or value in mysql database using the SELECT AND WHERE clause..

I have a table called websites.. And it has 3 fields namely id,websitename and ipaddress.

Sample content in the table

id   websitename           ipaddress
 1   www.google.com        12.1.1.0
 2   www.ebay.com          1.1.1.1

In my form I have a textbox where the user can input the website name and it has a button named get ip address..

To get the ip address of the website entered by the user I used the function in php called gethostbyname();

what i want to do is before gettip the ip address of the website entered by the user it should first check the database if the website name is already there. If the website name already exists, it should prompt a message cannot add but if the website name is not yet stored in the database it should get the ip address of the website and then it should be inserted into the table.

Please help I am so sorry I am a newbie.. Thank you very much in advance..

1
  • 2
    This is a very basic question. I think you should read some book before trying to do something more complicated. Commented Jan 10, 2012 at 9:26

4 Answers 4

1

Basically for checking in database you need to do something like this:


$ipaddress = mysql_real_escape_string($_POST['ipaddress']); // get post data
//then query
$sql = mysql_query("SELECT id FROM websites WHERE ipaddress='".$ipaddress."'");

if(mysql_num_rows($sql) > 0) {
  //already exists
}
else {
 //does not exist
}


Yes of course you need to read some beginner tutorials for it.

Sign up to request clarification or add additional context in comments.

Comments

0

you must make a request on the name You could use the "COUNT" SQL clause to count if there is already an entry.

If count != 0, you should add in the mysql.

If you are a newbie, you could use phpmyadmin to show concretly SQL requests.

hopes its help you

Comments

0

Try something like this:

<?php
// Make a MySQL Connection: change with your data
mysql_connect("localhost", "admin", "admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Get all the data from the table
$result = mysql_query("SELECT * FROM websites") or die(mysql_error()); 

if(mysql_num_rows($result) > 0) {
    echo "Can't add";
} else {
    $ip = gethostbyname();
    $website = $_POST['wesitename'];

    // Add into db
    mysql_query("INSERT INTO websites VALUES ($website, $ip)");

    // Print all values
    echo "<table>";
    echo "<tr><th>Site</th> <th>IP</th></tr>";

    // Print all the rows
    while($row = mysql_fetch_array( $result )) 
    {
    echo "<tr>"; 
    echo "<td> . $row['websitename'] . "</td>";
    echo "<td> . $row['ipaddress'] . "</td>";
    echo "</tr>"; 
    } 

    echo "</table>";
}

mysql_close();
?>

Comments

0
  1. Please visit http://www.w3schools.com/sql/default.asp. You can learn about select , update , delete

  2. To save to your database with mysql_query( "code sql ");

  3. Check data from database using mysql_num_rows();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.