FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


how to simplify these codes?





bartdou
I use the following codes to insert data into the database, the database name is "mydata",the table name is "customers", the table has 4 fields which are CustID,CustName,Email and Address. I think these codes are not laconic enough,how to simplify them while keeping their function.

Code:
<?php   
$cn=mysql_connect('localhost','root','333333')
    or die("Fail to connect!");

$strsql1="insert into customers (CustID,CustName,Email,Address) values ('C00001','Bart','liuxl@souhu.com','myhome')";
$strsql2="insert into customers (CustID,CustName,Email,Address) values ('C00002','Marry','wangdh@263.net','xxxxxxxxx')";
$strsql3="insert into customers (CustID,CustName,Email,Address) values ('C00003','Goedo','zhangww@pa18.com','xxxxxx')";
$strsql4="insert into customers (CustID,CustName,Email,Address) values ('C00004','Lizzie','linwd@sina.com','xxxxxxx')";
$strsql5="insert into customers (CustID,CustName,Email,Address) values ('C00005','Miranda','xiexl@sohu.com','xxxxxx')";

mysql_db_query("mydata",$strsql1,$cn);   
mysql_db_query("mydata",$strsql2,$cn);
mysql_db_query("mydata",$strsql3,$cn);
mysql_db_query("mydata",$strsql4,$cn);
mysql_db_query("mydata",$strsql5,$cn);

mysql_close($cn);    
?>
izcool
This might work, I didn't test this to see if it would :

Code:
<?php
$cn = mysql_connect("localhost","root","333333") or exit("MySQL Error :  " . mysql_error());

mysql_query("INSERT INTO customers (CustID,CustName,Email,Address) VALUES('C00005','Miranda','xiexl@sohu.com','xxxxxx'"),$cn) or exit("MySQL Error :  " . mysql_error());
mysql_query("INSERT INTO customers (CustID,CustName,Email,Address) VALUES('C00001','Bart','liuxl@souhu.com','myhome')"),$cn) or exit("MySQL Error :  " . mysql_error());
mysql_query("INSERT INTO customers (CustID,CustName,Email,Address) VALUES('C00003','Goedo','zhangww@pa18.com','xxxxxx')"),$cn) or exit("MySQL Error :  " . mysql_error());
mysql_query("INSERT INTO customers (CustID,CustName,Email,Address) VALUES('C00004','Lizzie','linwd@sina.com','xxxxxxx')"),$cn) or exit("MySQL Error :  " . mysql_error());
mysql_query("INSERT INTO customers (CustID,CustName,Email,Address) VALUES('C00005','Miranda','xiexl@sohu.com','xxxxxx')"),$cn) or exit("MySQL Error :  " . mysql_error());

mysql_close($cn) or exit("MySQL Error :  " . mysql_error());     
?>


Good luck.

- Mike.
Rhysige
For starters why are you specifying ID's? just make it an int field auto increment and primary key... but using your code as is this is probably the simplest form.

Code:

<?php   
$cn=mysql_connect('localhost','root','333333')
    or die("Fail to connect!");

$query="INSERT INTO `customers` (`CustID`,`CustName`,`Email,Address`) VALUES ('C00001','Bart','liuxl@souhu.com','myhome'), ('C00002','Marry','wangdh@263.net','xxxxxxxxx'),('C00003','Goedo','zhangww@pa18.com','xxxxxx'),('C00004','Lizzie','linwd@sina.com','xxxxxxx'),('C00005','Miranda','xiexl@sohu.com','xxxxxx')";

mysql_db_query("mydata",$query,$cn);   

mysql_close($cn);   
?>
hexkid
bartdou wrote:
I think these codes are not laconic enough,how to simplify them while keeping their function.

What's so good about laconic code? Smile

Rhysige already gave you a good answer.
You can try another version
Code:
<?php
$cn = mysql_connect('localhost', 'root', '333333') or die("Fail to connect!");
mysql_select_db('mydata') or die('Fail to select database!');

$clients = array(
  array('C00001', 'Bart', 'liuxl@souhu.com', 'myhome'),
  array('C00002', 'Marry', 'wangdh@263.net', 'xxxxxxxxx'),
  array('C00003', 'Goedo', 'zhangww@pa18.com', 'xxxxxx'),
  array('C00004', 'Lizzie', 'linwd@sina.com', 'xxxxxxx'),
  array('C00005', 'Miranda', 'xiexl@sohu.com', 'xxxxxx'),
);

foreach ($clients as $client) {
  $sql = 'insert into customers (CustID, CustName, Email, Address) values (\'';
  $sql .= implode('\', \'', $client);
  $sql .= '\')';
  if (!mysql_query($sql)) {
    // There was an error. For simplicity sake we just print the error and exit
    exit('Error in insert query: ' . mysql_error() . "<br>\nThe query was: $sql");
  }
}

mysql_close($cn);   
?>
Rhysige
Havnt any of you guys ever learnt to capitalise the commands.. like INSERT INTO and WHERE and FROM and ORDER BY.. it make querys so much easier to read Smile
jcvincent
I'm not really a great PHP developer, but I'll try to help.

bartdou wrote:
I use the following codes to insert data into the database, the database name is "mydata",the table name is "customers", the table has 4 fields which are CustID,CustName,Email and Address. I think these codes are not laconic enough,how to simplify them while keeping their function.

Code:
<?php   
$cn=mysql_connect('localhost','root','333333')
    or die("Fail to connect!");

$strsql1="insert into customers (CustID,CustName,Email,Address) values ('C00001','Bart','liuxl@souhu.com','myhome')";
$strsql2="insert into customers (CustID,CustName,Email,Address) values ('C00002','Marry','wangdh@263.net','xxxxxxxxx')";
$strsql3="insert into customers (CustID,CustName,Email,Address) values ('C00003','Goedo','zhangww@pa18.com','xxxxxx')";
$strsql4="insert into customers (CustID,CustName,Email,Address) values ('C00004','Lizzie','linwd@sina.com','xxxxxxx')";
$strsql5="insert into customers (CustID,CustName,Email,Address) values ('C00005','Miranda','xiexl@sohu.com','xxxxxx')";

mysql_db_query("mydata",$strsql1,$cn);   
mysql_db_query("mydata",$strsql2,$cn);
mysql_db_query("mydata",$strsql3,$cn);
mysql_db_query("mydata",$strsql4,$cn);
mysql_db_query("mydata",$strsql5,$cn);

mysql_close($cn);    
?>


1. are you going to fill out all the fields in your table? if you wil, then try this:

$strsql1="insert into customers values ('C00001','Bart','liuxl@souhu.com','myhome')";

just to simplify the code.

2. Are this the only records that you are going to insert in your MySQL table? If you are going to insert more records, I suggest that you should create a form just to avoid recoding the PHP script everytime you insert a record.
bartdou
THANK YOU ALL!!!
hexkid wrote:
bartdou wrote:
I think these codes are not laconic enough,how to simplify them while keeping their function.

What's so good about laconic code? Smile

By the way, perhaps the word 'laconic' isn't used fitly here, which word can substitute it?
Related topics
Creating a new Operating System
Free Opera registration - surf without ads - only today!
Evil Genius Cheats
Guissmo's Javascript (1)
(Music) Video Codes Sites & Using Them Illegal ?
Shell script to automate rlogin
Form Hit Counter
100F$ for Javascript Menu code(done. LOCK please)
Hello. I'm a generic Frihost Bot.
how did you learn about html, php or other languages???
mobile programmers codes
How can I create a User account and password for my website?
BBCode Unleashed
11 MyCokeRewards.com codes [Only for U.S. Residents]
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.