Sunday, November 14, 2010

PHP and MySql Basics: Insert Query

//open conn to db
mysql_connect('dbhostserver','dbusername','password') or die("Failure to connect to db");



mysql_select_db('dbname');

$as_value1 = addslashes($_POST['value1']);
$tr_value1 = trim($as_value1);

$as_value2 = addslashes($_POST['value2']);
$tr_value2 = trim($as_value2);

$query = "INSERT INTO table(value1, value2) VALUES ('$tr_value1', '$tr_value2')";
$result = mysql_query($query);
if(mysql_affected_rows()==1){
   $message = "Successfully added to db.";
}else{
   $message = "Could not add to db.";
}

No comments: