Solved

PHP import help

Posted on 2012-04-03
2
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
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…
The viewer will learn how to dynamically set the form action using jQuery.

759 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