Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP import help

Posted on 2012-04-03
2
Medium Priority
?
221 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

783 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