Solved

PHP import help

Posted on 2012-04-03
2
212 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 count occurrences of each item in an array.

786 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