Solved

PHP import help

Posted on 2012-04-03
2
208 Views
Last Modified: 2012-04-03
Hi,

I am very new to PHP and MySQL however I do not think this request is too difficult.

I am trying to build a php script that will allow my market managers to enter their company profit factors for the year. The form asks what country they work in and what their profit factors are. It then imports this data into a MYSQL database.

Some sample data looks like this

Market Profit Factor
UK              50
BE               200
DE              80

This works fine, however I would like to know how I can tell my script to just overwrite existing records. As If I enter a profit factor twice then I get two rows of data i.e -

Market Profit Factor
UK              50
UK              50

Is there anyway I can just overwrite the data?

I have attached my php

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="bonuscalc"; // Database name 
$tbl_name="market_values"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$market=$_POST['market'];
$profit_factor=$_POST['profit_factor'];


// Insert data into mysql 
$sql="INSERT INTO $tbl_name(market, profit_factor)VALUES('$market', '$profit_factor')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection 
mysql_close();
?>

Open in new window

0
Comment
Question by:Dan560
2 Comments
 
LVL 9

Accepted Solution

by:
wasiftoor earned 500 total points
ID: 37801664
I have noticed that you are using an INSERT statement. This will always insert a new row into the Database. Consider using an UPDATE statement instead. I am attaching sample code. Please make sure that all of your markets already exist in the database. (UK,BE, DE). If you have not data yet for a particular market, just put in an entry with a zero value to start off with.

// Update data in mysql. Replace the existing insert statement.
$sql="UPDATE $tbl_name SET profit_factor = '$profit_factor' WHERE market = '$market'";
$result=mysql_query($sql);

Hopefully this will work. Good Luck!
0
 
LVL 2

Author Comment

by:Dan560
ID: 37801687
Perfect, it works well. Thank you!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now