Insert Problem

Dear Sir,

I have following codes to save record in MySql.

[PHP]<?php
include(“connect.php”);
$insert_query = “INSERT INTO ghee (sno,pack,weight) VALUES (18,‘kk’,80)”;
$insertion_result = mysql_query($insert_query);
//check whether the data insertion was successful
if(!$insertion_result)
{
die(“Sorry! Something went wrong.

”);
}
else
{
echo “

Record saved successfully.

”;
}
mysql_close();
?>[/PHP]

These codes work fine, no error. But I need some modification as

In above code these values are going to save

VALUES (18,'kk',80)";

If I press submit button then values are written in dbf.
But if I press submit button MORE THAN ONE TIME then everytime it says:

[PHP]echo “

Record saved successfully.

”; [/PHP]

Before sending data to table it must check whether current SNO=18 already exist or not. If already exist then it must say:

[PHP]die “

Current SNO already exists .

”; [/PHP]

Please help

So basically what you’re saying is that you don’t have any unique keys on you table to prevent duplicate data from inserting? If you add an unique key to your table then you can do something like below.

Else you will have to select from the table, before you insert into it. A more elegant way will be to add the unique key and then do an insert ignore query. They every query would be successful.

[php] <?php
include(“connect.php”);
$insert_query = “INSERT INTO ghee (sno,pack,weight) VALUES (18,‘kk’,80)”;
mysql_query($q) OR die(“Sorry! Something went wrong.

”);
echo “

Record saved successfully.

”;
mysql_close();
?>[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service