lady
lady

Reputation: 51

MYSQL INSERT CLAUSE USING PHP

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

Upvotes: 0

Views: 188

Answers (4)

Yan Susanto
Yan Susanto

Reputation: 353

  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();

Upvotes: 0

Marco Pace
Marco Pace

Reputation: 3870

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();
?>

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

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.

Upvotes: 1

clement
clement

Reputation: 4266

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

Upvotes: 0

Related Questions