PHP How to shoot an email with an attched table? - php

I use i-page as a hosting company. I have a mySQL database. How can i shoot an email with an attached table? Let's say i have connected to my database:
$link = mysql_connect('www.....com', 'login', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('rates', $link);
// Now how do i SELECT table1??
// Do i create a variable $table = SELECT...
// Then how to attach it to an email and send the email?

Related

Unable to connect to MySQL database but able to connect to database server

I am trying to build a dynamic website for connecting pages with database I used the code as follows. connection with server is Ok but unable to select database. data base name, user id, password, ip of host all gave but not working. please help....
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('DB_HOST', '');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
else {
echo 'connected to server..................'; }
$db_selected = mysql_select_db($DB_NAME, $link);
if ($db_selected) {
print "Database Found";
}
else {
print "Database NOT Found";
}
Mysql has been deprecated and will eventually be removed. Consider using PDO or MySqli.
Here is a link to the PHP documentation page for making connections to a database using MySqli.
http://php.net/manual/en/mysqli.quickstart.connections.php
If you follow the instructions carefully you 'should' be able to connect. If not, perhaps you can post the error message you receive.
Try to give proper credential to connect to mysql server. mysqli_connect("localhost","root","password","db_name");

Can't access the Database (MAMP)

When I press my submit button, I get an error saying :
ErrorTable 'form2.demo' doesn't exist
form2 is my database name which is created in phpmyadmin.
I am a new MAC user.
Below is my php code.
define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8888');
My ports are :
Apache : 8888
Msql : 8889
Full code is as below:
<?php
define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8889');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('Could not connect :' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('Can\'t use' . DB_NAME . ':' . mysql_error());
}
$value = $_POST['input1'];
$sql = "INSERT INTO demo (input1) VALUES ('$value')";
if(!mysql_query($sql)){
die('Error' . mysql_error());
}
mysql_close();
?>
Your problem have nothing to do with Mac. You have to show us more code. How the code in your form looks like? How you are sending stuff to database?
Let's assume that everything in code is set up properly. The main thing you have to check is if you actually have demo table in your database. Go to phpmyadmin and check it, if there is no table create it. You can use phpmyadmin to do that or do it via SQL query like that one
CREATE TABLE IF NOT EXISTS `form2`.`demo` ( `id` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY (`id`));
Of course code above will create table with only ID column so you have to adjust it to your needs
DEFINE('DB_USERNAME', 'root');
DEFINE('DB_PASSWORD', '');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_DATABASE', 'customer');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
}
echo 'Connected successfully.';
$mysqli->close();
Try the msqli library instead

sending an email in PHP with information taken from database

I want to pull an email from the database as the recipient. If I replace the line "SELECT student_user_email FROM students";' with an actual email address I get the desired outcome. However it would be handier to pull a particular email address from the 'students' table. Below is the current code. Wondering if anyone can help?
mail('"SELECT student_user_email FROM students";','Sample Form',$msg, 'From: johnsmith#gmail.com');
First, use mysqli or PDO because the mysql_ functions are depricated. The documentation has very useful information to get started with it.
mysqli: http://php.net/manual/en/book.mysqli.php
pdo: http://us3.php.net/pdo
mail: http://us3.php.net/manual/en/function.mail.php
This is what you need to do to connect to your database (from the php documentation: http://www.php.net/manual/en/mysqli.quickstart.connections.php)
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
Once connected you can query the database:
$result = $mysqli->query("SELECT student_user_email FROM students");
while ($row = $result->fetch_object()) {
if (isset($row->student_user_email)) {
mail($row->student_user_email,'Sample Form',$msg, 'johnsmith#gmail.com');
}
}
You can't send email to a multiple recipients in this way. Because query return array list so you must be looping and send email one by one recipients by this following way,
Try this will work:
<?php
$con = mysqli_connect("localhost", "uname", "password");
if (!$con){
die('Could not connect: ' . mysqli_error());
}
$db_selected = mysqli_select_db("database",$con);
$sql = "SELECT student_user_email FROM students";
$result = mysqli_query($sql,$con);
while ($row_data = mysqli_fetch_array($result)) {
// Then you will set your variables for the e-mail using the data
// from the array.
$from = 'you#yoursite.com';
$to = $row_data['email']; // The column where your e-mail was stored.
$subject = 'Sample Form';
$msg = 'Hello world!';
mail($to, $subject, $msg, $from);
}
?>

check if email exists in MySQL database [duplicate]

This question already has answers here:
Quickest way to check for pre-existing record before insert [mysql_errno()]
(2 answers)
Closed 9 years ago.
I am using the following PHP code to store data in my MySQL database, everything works perfect but I would like to build in a check; if the email already exists in the database then redirect to another page. (I'm a rookie when it comes to PHP)
<?php
if(empty($_POST['name']))
{} else {
define('DB_NAME', 'dbname');
define('DB_USER', 'dbuser');
define('DB_PASSWORD', 'dbpass');
define('DB_HOST', 'host');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) { die('could not connect: ' . mysql_error()); }
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) { die('can not use ' . DB_NAME . ': ' . mysql_error()); }
$sql="INSERT INTO game (name, lastname, company, email, time) VALUES
('".$_POST['name']."','".$_POST['lastname']."','".$_POST['company']."','".$_POST['email']."','".$_POST['time']."')";
if (!mysql_query($sql)) { die('Error: ' . mysql_error()); }
mysql_close();
}
?>
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
exit('Invalid email address'); // Use your own error handling ;)
}
$select = mysqli_query($connectionID, "SELECT `email` FROM `game` WHERE `email` = '".$_POST['email']."'") or exit(mysqli_error($connectionID));
if(mysqli_num_rows($select)) {
exit('This email is already being used');
}
Stick that in above the insert query
First, to your question. The most simple is to use the header() function to redirect to a page.
if ( !$email) {
header('Location: http://example.com/new_page.html' );
} else {
// Continue with code
}
Secondly, there is a whole lot of issues going on in that code. For example:
Use PDO
parameterize your query
Your control statement should probably look like if(!empty($_POST['name'])) {

Connecting to a mysql Database via PHP?

Ok, Ive been playing around with PHP and databases. Note, Im doing all my work on a webserver hosted by bluehost.
So I went into the cpanel and set up a database called gagalugc_stocks.
I also setup a username (test) and a password (password) and added them to be able to control the databse. Below is my script in an attempt to connect to it, not it is also on the server.
<?php
$connect = mysql_connect('localhost','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
-Thanks
NOTE: The problem is that it can never reach the database.
<?php
$connection = mysql_connect ('localhost', 'test', 'password')
or die ('<b>Could not connect: </b>' . mysql_error());
$result = mysql_select_db("gagalugc_stocks")
or die ('<b>Could not connect: </b>' . mysql_error());
?>
Didn't check for port when adding database make sure to specify host and port
(localhost:2083)
How about
<?php
$connect = mysql_connect('localhost:2083','test','password');
if(!$connect){die('Could not reach database!');}
mysql_select_db("gagalugc_stocks", $connect)
?>
For local system:-
$con = mysql_connect("localhost","root","");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
For website or remote server:-
$con = mysql_connect("localhost","username","password");
if (!$con){
die('Unable to connect to the server ' . mysql_error());
}
mysql_select_db("databasename", $con) or die(mysql_error());
for more please visit : Algosoftwares

Categories