PHP Script is not inserting values into Database [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
For whatever reason, whenever I submit the form, it doesn't add the inputted data to the database. It was working before... Which I think is what's frustrating me the most. I came back to it a couple of hours later, and surprise! Not inserting new rows into the database. Perhaps there's a slight error I may have done subconsciously by accident? Nothing stands out to me, though. :(
refer.html: http://pastebin.com/d1xQUJLR
generator.php: http://pastebin.com/CE2UX8zs
main.js: http://pastebin.com/CSQh9DKs

Just fixed some bugs in your code ;)
<?php
include_once "access.php";
$ref_email = $_POST["tf_ref_email"];
$ref_username = $_POST["tf_ref_username"];
$ref_ign = $_POST["tf_ref_ign"];
$access = new Access();
$crux = $access->getCrux();
$anchor = $access->getAnchor();
$user = $access->getUser();
try {
$pdo = new PDO($anchor, $user, $crux);
$stq = "INSERT INTO referred_users
(ref_id, ref_email, ref_username, ref_ign, ref_awarded, new_email, new_awarded)
VALUES (:ref_id, :ref_email, :ref_username, :ref_ign, :ref_awarded, :new_email, :new_awarded)";
for($i = 0; $i < min(count($_POST["emails"]), 10); $i++) {
$ref_id = $_POST["ref_ids"][$i];
$new_email = $_POST["emails"][$i];
$new_ref_id = checkId($ref_id, $pdo);
$query = $pdo->prepare($stq);
$results = $query->execute(
array(
':ref_id' => $new_ref_id,
':ref_email' => $ref_email,
':ref_username' => $ref_username,
':ref_ign' => $ref_ign,
':ref_awarded' => '0',
':new_email' => $new_email,
':new_awarded' => '0'
)
);
$to = $new_email;
$subject = "Check out this AMAZING Minecraft RP Server!";
$message = "You have been invited to join " . $ref_ign . " in Arithia! Enter your Referral ID in the provided link to get a head start on your first character!" . "\n";
$message .= "Referral ID: " . $new_ref_id . "\n";
$message .= "Redeem Rewards: " . "http://www.graphicgoldfish.com/referral/referral.html" . "\n";
$message .= "By redeeming your referral, you will get:" . "\n";
$message .= "- 300 Credit Points" . "\n";
$message .= "- 50 Ducats" . "\n";
$message .= "- 25 Cooked Beef" . "\n";
$message .= "- Identification Tome" . "\n";
$message .= "We look forward to seeing you in Arithia! For more information, visit: http://www.arithia.com" . "\n";
$headers = "From: $ref_email";
mail($to, $subject, $message, $headers);
}
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
function checkId($id, $con) {
$stmt = $con->prepare("SELECT ref_id FROM referred_users WHERE ref_id = :ref_id");
$stmt->bindParam(':ref_id', $id);
$stmt->execute();
if($stmt->rowCount() > 0) {
$new_id = (string)rand(1000000, 9999999);
return checkId($new_id, $con);
}
return $id;
}

Related

Sending data by email formatted into a HTML table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 days ago.
Improve this question
I am trying to put the contents of an array into a table that can be used with PHP mail.
My code returns data from MySQL database and then gets called by another script to display the results.
I then need to insert the same data into a html table and send the tables and data in an email. The issue is when the email is sent
the only content in the email are the table headers, no data. Can anyone see why the data is not included in the email table?
$sql = "SELECT deviceType,deviceTypeCode,deviceDesc,deviceID, DeviceID19, DeviceLocation FROM FIDS_Hardware WHERE hotelid = '".$_SESSION['_amember_user']['hotelid']."' AND messageCount = 2";
$res = mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn));
$TotalRows_res = mysqli_num_rows($res);
$data = array();
while($row = mysqli_fetch_array($res)){
$DeviceDescription = $row['deviceDesc'];
$DeviceType = $row['deviceType'];
$DeviceTypeCode = $row['deviceTypeCode'];
$DeviceRefNo = $row['deviceID'];
$DeviceID19 = $row['DeviceID19'];
$DeviceLocation = $row['DeviceLocation'];
if($DeviceTypeCode == 5){
$DeviceRefNo = $DeviceRefNo;
} else {
$DeviceRefNo = $DeviceID19;
}
$data[] = array(
"DeviceDescription" => $DeviceDescription,
"DeviceType" => $DeviceType,
"DeviceRefNo" => $DeviceRefNo,
"DeviceLocation" => $DeviceLocation,
"TotalRows" => $TotalRows_res);
}
echo json_encode($data); // DATA DISPLAYED ON IN ANOTHER SCRIP[T
$subject = "Advisory:";
$to = $_SESSION['AdminEmail'];
$newOffline = array();
foreach($data as $data1){
$newOffline[] = implode(', ', $data1);
}
$newOffline = implode(" \n<br>", $newOffline);
$offline = $newOffline;
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers = "From: <support#mydomain.com>" . "\r\n" .
mail($to, $subject,$offline, $headers);
/* ALL OF THE ABOVE WORKS AND SEND DATA WHICH LOOKS LIKE THIS
EXBHX-Baskerville, Conference, 3, , 9
EXBHX-Bolton, Conference, 1, Conference centre, 9
EXBHX-Watt, Conference, 2, , 9
EXBHX-Brindley, Conference, 4, , 9
EXBHX-Cadbury, Conference, 5, , 9
EXBHX-Aston, Conference, 6, , 9
EXBHX-Main reception No2, Conference, WF2, Main reception, 9
EXBHX-Main reception No3, Conference, WF3, Conference centre, 9
EXBHX-Depart, Flightboard, WF6, Main reception, 9
*/
/* THIS IS THE TABLE I AM TRYING TO WORK WITH
$offline = '
echo "<tr><th>Description</th><th>Type</th><th>Device ID</th><th>Location</th></tr>";
foreach($data as $data1){
$newOffline[] = implode(', ', $data1);
$description = $newOffline[0];
$type = $newOffline[1];
$deviceid = $newOffline[2];
$location = $newOffline[3];
echo "<tr>";
echo " <td>"; echo $description; echo "</td>";
echo " <td>"; echo $type; echo "</td>";
echo " <td>"; echo $deviceid; echo "</td>";
echo " <td>"; echo $location; echo "</td>";
echo "</tr>";
}
'
;
*/
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers = "From: <support#mydomain.com>" . "\r\n" .
mail($to, $subject,$offline, $headers);

Sending email with mixed data from both form inputs and database data

I am trying to send email of which one of the data is from database, i tried to look on some post here in stack-overflow but they were different;
I tried to send without the data from the database and it works fine,but after adding database fetch its not working
<?php
include 'include/connect.php';
if (isset($_POST['book2'])) {
$id = mysqli_real_escape_string($conn, $_GET['id']);
$sql = "SELECT room_price FROM room_details WHERE id='$id';";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$room_price = $row['room_price'];
$email = $_POST['email'];
$room_type = $_POST['room_type'];
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$adults = $_POST['adults'];
$children = $_POST['children'];
$mailTo = "booking#johndoexxx.com";
$headers = "Guest sent e-mail from: " . $email;
$txt = "New booking received, room for " . $adults . " adults and " . $children . " child / children, reservation starts on " . $checkin . " up to " . $checkout . " whereby " . $room_type . " room is selected, Please respond to the sender!";
$heading = "New Booking!";
mail($mailTo, $heading, $headers, $txt);
header("Location: index.php?bookingsent");
}
}
} else {
echo "Booking failed to process!, observe your inputs carefully!";
}
?>
was expecting it will fetch data from form and will send the data into the targeted email.

Account activation link in email after registration cakephp

I am using cakephp 2.6. i need to send account activation link in email after successful registration and
if the user clicks on activation link then the status field of my database to be updated with 1.
My email is going to be correctly but i don know how to send link inside email by getting lastinsertid as a unique identifier in email and update the account after click.
please help me to out of this.
function add() {
$this->layout = "layout_login";
$this->pageTitle = "Add Admin";
$this->set("pageTitle", "Add Admin");
if (!empty($this->request->data)) {
$this->User->set($this->data['User']);
$isValidated = $this->User->validates();
if ($isValidated) {
// Generating UUID
$this->data['User']['activation_key'] = $activation_key = String::uuid();
$this->User->save($this->data, array('validate' => false));
$this->Session->setFlash(__('Please check your email for account verification.'));
$subject = "Account Activation link By Kaya Dispatch";
$to = trim($this->request->data['User']['email']);
$this->Email->sendAs = 'html';
$this->Email->from = 'luckybajpai87#gmail.com';
$this->Email->to = $to;
$this->Email->subject = $subject;
$activationUrl = Router::url(['controller' => 'users', 'action' => 'activate/' . $activation_key ]);
$message = "Dear <span style='color:#666666'>" . $name . "</span>,<br/><br/>";
$message .= "Your account has been created successfully by Administrator.<br/>";
$message .= "Please find the below details of your account: <br/><br/>";
$message .= "<b>Activate your account by clicking on the below url:</b> <br/>";
$message .= "<a href='$activationUrl'>$activationUrl</a><br/><br/>";
$message .= "<br/>Thanks, <br/>Support Team";
$this->Email->send($message);
}
}
}
below is my account activation code
function activate($activation_key) {
$userData = $this->User->find('first', array(
'conditions' => array(
'User.activation_key' => $activation_key,
'User.status' => 0
)
));
if( !empty($userData)){
if ($userData['User']['status'] == 0)
{
$activeStatus = 1;
}
$status = $this->User->updateAll(array('User.status' => $activeStatus), array('User.id' => $id));
if ($status)
{ $this->Session->setFlash(__('Status updated successfully'));
} else
{ $this->Session->setFlash(__('Something went wrong, please try again.'));
}
$this->redirect(array('controller' => 'Users', 'action' => 'index'));
}
}
Well, this is something logical, I mean this is totally depends how you wanna do that. I am writing my way :)
Make new column in your users table i.e. activation_key, and set it before inserting it into the Table. Like this:
if ($isValidated) {
$password = $this->data['User']['password'];
$this->data['User']['password'] = md5($this->data['User']['password']);
// Generating UUID
$this->data['User']['activation_key'] = $activation_key = String::uuid();
$this->User->save($this->data, array('validate' => false));
$this->Session->setFlash("<div class='success-message flash notice'>Admin has been created successfully.</div>");
$subject = "Account Activation link send on your email";
$name = $this->request->data['User']['fname'] . " " . $this->request->data['User']['lname'];
$to = trim($this->request->data['User']['email']);
$this->Email->sendAs = 'html';
$this->Email->from = 'luckybajpai87#gmail.com';
$this->Email->to = $to;
$this->Email->subject = $subject;
$activationUrl = Router::url(['controller' => 'users', 'action' => 'activate/' . $activation_key ]);
// Always try to write clean code, so that you can read it :) :
$message = "Dear <span style='color:#666666'>" . $name . "</span>,<br/><br/>";
$message .= "Your account has been created successfully by Administrator.<br/>";
$message .= "Please find the below details of your account: <br/><br/>";
$message .= "<b>First Name:</b> " . $this->data['User']['fname'] . "<br/>";
$message .= "<b>Last Name:</b> " . $this->data['User']['lname'] . ((!empty($this->data['User']['phone'])) ? "<br/>";
$message .= "<b>Phone:</b> " . $this->data['User']['phone'] : "") . "<br/>";
$message .= "<b>Address:</b> " . $this->data['User']['address1'] . " " . $this->data['User']['address2'] . "<br/>";
$message .= "<b>Email:</b> " . $this->data['User']['email'] . "<br/>";
$message .= "<b>Username:</b> " . $this->data['User']['username'] . "<br/>";
$message .= "<b>Password:</b> " . $password . "<br/>";
$message .= "<b>Activate your account by clicking on the below url:</b> <br/>";
$message .= "<a href='$activationUrl'>$activationUrl</a><br/><br/>";
$message .= "<br/>Thanks, <br/>Support Team";
$this->Email->send($message);
}
And then in your function activate($activation_key='') receive that activation key as a parameter, and find from users like this:
// Finding user data from users table on behalf of activation key
// and Status should be 0 (deactivate). So that, a URL can be use only ONCE.
$userData = $this->User->find('first', array(
'conditions' => array(
'User.activation_key' => $activation_key,
'User.status' => 0
)
));
if( !empty($userData) ){
// $userData as you have User's data update the status as 1 and set activation_key as empty "";
}else{
// So, you don't find any user, it is an invalid request.
}
Sometime, it happens that Cakephp do not allow you to update $this->data as in this example we are trying to change $this->data['User']['password'] and $this->data['User']['activation_key'] values so you can simply store $postData = $this->data and then use $postData for insertion and further operations.
Update: Please confirm/debug if String::uuid() is working correctly for you, if it dosn't work, try to use the below line for this purpose:
// Generating activation_key
$this->data['User']['activation_key'] = $activation_key = time();
Thank you!

PHP POST form validating error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got a PHP form that executes on the contact form's page. I've got it working well - but it currently only checks to make sure name and email are entered. I'm trying to get it to check for a message too, but my attempts just cos the page not to load after the php. Here's what I have:
<?php
$to = "me#gmail.com" ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$startmonth = $_REQUEST['StartMonth'];
$startyear = $_REQUEST['StartYear'];
$endmonth = $_REQUEST['EndMonth'];
$endyear = $_REQUEST['EndYear'];
$message = $_REQUEST['Message'];
$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$selectedProjects = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
$selectedProjects = implode(', ', $_POST['projects']);
}
$selectedSkills = 'None';
if(isset($_POST['skills']) && is_array($_POST['skills']) && count($_POST['skills']) > 0){
$selectedSkills = implode(', ', $_POST['skills']);
}
$selectedNoRush = 'None';
if(isset($_POST['norush']) && is_array($_POST['norush']) && count($_POST['norush']) > 0){
$NoRush= implode(', ', $_POST['norush']);
}
$selectedWhenReady = 'None';
if(isset($_POST['whenready']) && is_array($_POST['whenready']) && count($_POST['whenready']) > 0){
$WhenReady= implode(', ', $_POST['whenready']);
}
$selectedBudget = 'None';
if(isset($_POST['budget']) && is_array($_POST['budget']) && count($_POST['budget']) > 0){
$selectedBudget= implode(', ', $_POST['budget']);
}
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);}
$body .= "\n" . 'Selected Projects: ' . $selectedProjects . "\n";
$body .= 'Selected Skills: ' . $selectedSkills . "\n\n";
$body .= 'Start Date: ' . $startmonth . " " . $startyear . " " . $NoRush . "\n";
$body .= 'End Date: ' . $endmonth . " " . $endyear . " " . $WhenReady . "\n";
$body .= 'Budget: ' . $selectedBudget . "\n\n";
$body .= 'Message:' . $message . "\n";
$headers2 = "From: me#gmail.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{print "Thank you. Your request has been successfully submitted.";}
else
{print "We encountered an error sending your mail, please check your details are correct or email us at hello#lyonempire.co.uk"; }
}
}
?>
So the above all works fine, but when I add the following code after the name/email checks, it breaks:
if($message == '') {print "You have not entered a message, please go back and try again";}
else {
What am I doing wrong?
Thanks! MC
if($name == '')
{
print "You have not entered a name, please go back and try again";
}
else if($message == '') {
// do what ever you want
}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
.... Rest goes here
i cant tell from the code you've shown, but have you closed the curly bracket on your else statement?
if($message == '') {print "You have not entered a message, please go back and try again";}
else {} <----

Running Query inside WHILE/Loop

Need help here, got a query that counts number of rows for each member and emails it to them.
So right now they get a simple email telling them how many posts they have, I have searched for a way to run a second query inside the main query (which counts the posts from the post table, generates the email based on their account info in another table) What we want is to have the email show each post as well, I am already joining tables in my first query and I can't seem to find a way to run a second inside the while statement.
Here is the code we currently have. Any help would be greatly appreciated.
<?php
//Connect to mysql server
$link = mysql_connect(localhost, xxxxxxx, xxxxxxxx);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(systm_test);
if(!$db) {
die("Unable to select database");
}
$count = 0;
$date = date("Y-m-d", $_SERVER['REQUEST_TIME']);
$day = date("l F jS, Y", $_SERVER['REQUEST_TIME']);
$qry= sprintf("SELECT p.tech, p.date, e.email, e.fullname, "
. "COUNT( * ) AS record_count "
. "FROM users_test AS e "
. "JOIN sc AS p ON e.tech = p.tech "
. "WHERE DATE = '%s' "
. "GROUP BY p.tech, e.fullname, e.email, p.date",
$date
);
$result = mysql_query($qry);
if (!$result) {
# something went wrong...
error_log(sprintf('%d: %s', mysql_errno(), mysql_error()));
echo 'Unable to retrieve callpost report';
} else {
while ($node = mysql_fetch_object($result)) {
$headers = "From: xxxxxxxxxx <noreply#xxxxx.xxx>\r\n";
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$headers .= "X-Priority: 1 (Higuest)\r\n";
$headers .= "Importance: High\r\n";
$subject = sprintf('%s | Post Report', $node->fullname);
$date = date("Y-m-d", $_SERVER['REQUEST_TIME']);
$body = sprintf(
'Dear %s, <br><br>
Your daily post report for %s.
<br>
<br>
System Info<br>
------------------------------------<br>
Member Id: <b>%d</b><br>
Posts: <b>%d</b>
<br>
System Name: TEST<br>',
$node->fullname,
$day,
$node->tech,
$node->record_count
);
if (!mail($node->email, $subject, $body, $headers))
{
error_log(
sprintf('failed sending email report to %s (%d)',
$node->fullname,
$node->tech)
);
}
} //end while statement
mail($node->email, $subject, $body, $headers);
}
}
?>
A sub-select is what your looking for. Something like.
"SELECT p.tech, p.date, e.email, e.fullname, "
. "COUNT( * ) AS record_count, "
. "(SELECT COUNT(*) FROM Posts z WHERE z.author = e.tech) as post_count "
. "FROM users_test AS e "
. "JOIN sc AS p ON e.tech = p.tech "
. "WHERE DATE = '%s' "
. "GROUP BY p.tech, e.fullname, e.email, p.date"

Categories