I am trying to build a personal messaging system into my project. I have been using MYSQLi so far in the project, I just found this code for PMS. Now I don't know how to convert this mysql code into mysqli..
<?php
session_start();
$message = $_POST['forward2'];
if (isset($_POST['submit']))
{
// if the form has been submitted, this inserts it into the Database
$to_user = $_POST['to_user'];
$from_user = $_POST['from_user'];
$message = $_POST['message'];
mysql_query("INSERT INTO messages (to_user, message, from_user) VALUES ('$to_user', '$message', '$from_user')")or die(mysql_error());
echo "PM succesfully sent!";
}
else
{
// if the form has not been submitted, this will show the form
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h3>Send PM:</h3></td></tr>
<tr><td></td><td>
<input type="hidden" name="from_user" maxlength="32" value = <?php echo $_SESSION['username']; ?>>
</td></tr>
<tr><td>To User: </td><td>
<input type="text" name="to_user" maxlength="32" value = "">
</td></tr>
<tr><td>Message: </td><td>
<TEXTAREA NAME="message" COLS=50 ROWS=10 WRAP=SOFT></TEXTAREA>
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Send Message">
</td></tr>
</table>
</form>
<?php
}
?>
Get yourself a tutorial on mysqli.
Follow it through.
Create some simple mysqli-based application.
Make yourself familiar with mysqli
Then start with transition.
Related
I'm getting this error in my variables on lines 26 and 27. I have been searching for the problem itself, some people say that the variables are not initialized. Althought I think they are. Also I saw people saying to use isset() / !empty() but I don't understand that, and what it does.
<?php
$nome = $_POST['nome']; //26
$preco = $_POST['preco']; //27
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "crc", "root");
mysql_select_db ("crc");
$imgData =addslashes(file_get_contents($_FILES['userImage'['tmp_name']));
$sql = "INSERT INTO fios (nome,preco,imagem)VALUES('$nome','$preco','{$imgData}')";
$current_id = mysql_query($sql) or die("<b>Erro:</b> Problema na imagem inserida!<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: veradmin.php");
}}}
?>
<!DOCTYPE html>
<html>
<title>Inserir</title>
</head>
<body>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<div align="center">
</p><tr>
<td width="321"><strong>Nome/Descricao:</strong></td>
<td width="102" align="left">
<input type="text" name="nome" value="" size="40" />
</td>
</tr><p>
</p><tr>
<td width="321"><strong>Preco:</strong></td>
<td width="102" align="left">
<input type="text" maxlength="9" name="preco" value="" size="20" />
</td><p>
</p></tr>
<input name="userImage" type="file" class="inputFile" /><p>
</p><input type="submit" value="Inserir Registo" class="btnSubmit" />
</form>
</div>
</body>
</html>
This issue happened because in the first load $_POST['nome'] and $_POST['preco'] is empty and these indexes does not exists.
In this cases you should check with !empty to run this lines:
if(!empty($_POST['nome']) && !empty($_POST['preco']))
{
if(count($_FILES) > 0) {
...
}
}
At this time if you post your form or not these codes will run which is root cause of showing these notices
On a membership site that I am developing, once the user has logged in, on his profile page there are two simple forms.
The first is diplayed only if the user has his "district" field NULL in the database. If the "district" field contains any information, the first form is replaced by a table displaying data from that district. The purpose of the form is for the user to select his district from a list, and update his account so he can read local information from his district. After he hits submit, the "district" field in the databsase is updated and then he never sees this form again.
The second one is a basic contact form, thru which the user can easily send an email message to the support team if he needs any help. Once the message has been sent, instead of the form, a success message is displayed until the refresh of the page, when the text field comes back to normal and the user can send another message.
I have tested them and they both work fine separately, but the error I am encountering is that when a user with NULL in his "district" field logs in, or whenever his page is refreshed, the second form automatically sends a blank email to the support team, and is allways showing the success message instead of the text input for the message to be written in. I have setup an error when the second form is submitted empty, but despite this, while the first form is showing, with every refresh, a new blank email is being sent...
After the user has submitted the first form an updated his "district", the second form returns to normal and works just fine... I know that there is a conflict, or I forgot to setup some conditions in the code below. Please be kind, take a look and tell me where do you think the error might be. Any help is welcomed.
<? if($row_user['district'] == NULL ):
if(!isset($_POST['submit2']))
{
foreach($row_user as $field => $value)
{
$_POST[$field] = $value;
}
}
$error2 = 0;
if(isset($_POST['submit2'])){
if(isset($_POST['distr']) && ($_POST['distr']==""))
{
$error2 = 1;
$msg_distr="<br /><span class='error'>Select your district</span>";
}
if($error2 == 0)
{
$update = 'UPDATE users SET ';
if(isset($_POST['distr'])) $update .= 'district = '.GetSQLValueString($_POST['distr'], 'text').', ';
$update = substr_replace($update,"",-2);
$update .= 'WHERE id_user = '.$id_user;
mysql_query($update, $conn) or die(mysql_error());
}
}
?>
The HTML code for the first form is
<form action="" method="post" enctype="multipart/form-data" >
<table width="465px" border="0" cellpadding="5" cellspacing="5">
<tr>
<td width="160" valign="middle" align="left">
<span class="style20"><b>Choose District</b> *</span>
</td>
<td valign="top" align="left">
<select name="distr" onChange="sel_distr(this.value)" class="select" >
<option value="">Choose</option>
<option value="District1" <? if($_POST['distr']=="District1") echo "'selected'"?>>District1</option>
<option value="District2" <? if($_POST['distr']=="District2") echo "'selected'"?>>District2</option>
<option value="District3" <? if($_POST['distr']=="District3") echo "'selected'"?>>District3</option>
<option value="District4" <? if($_POST['distr']=="District4") echo "'selected'"?>>District4</option>
<option value="District5" <? if($_POST['distr']=="District5") echo "'selected'"?>>District5</option>
</select>
<?=$msg_distr?>
</td>
</tr>
<tr>
<td >
<input type="submit" name="submit2" class="submit" value="Insert" />
</td>
</tr>
</table>
</form>
<?endif; ?>
The other form is for sending email messages from this page, just by inserting the message and sending it. the user will not need to insert his name or email.
<?php
$error_message=0;
if ($_POST["email"]<>'') {
if(isset($_POST['comment']) && ($_POST['comment']=="")){
$error_message=1;
$er_msg="<br /><span class='error'>You can't send a blank message</span>";
}
if($error_message == 0){
$ToEmail = 'mail#domain';
$EmailSubject = 'New message from '.$row_user['name'].'';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "<b>Name:</b> ".$_POST["name"]."<br/>";
$MESSAGE_BODY .= "<b>Email:</b> ".$_POST["email"]."<br/><br/>";
$MESSAGE_BODY .= " ".nl2br($_POST["comment"])."<br/>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<div class="success" style="height:60px;">
<b><?=$row_user['name']?>, Your message was sent!</b> <br/>
A member of our team will contact you soon.
</div>
<?php
}
if($error_message == 1){ ?>
<form action="me.html" method="post">
<input name="name" type="hidden" value="<?=$row_user['name']?>" id="name" size="32">
<input name="email" type="hidden" value="<?=$row_user['email']?>" id="email" size="32"> <br/>
<textarea name="comment" cols="45" rows="6" id="comment" class="input" style="width:90%"></textarea><br/>
<input class="submit" type="submit" name="Submit" value="Send">
</form>
<?=$er_msg?>
<?
}
} else {
?>
<form action="me.html" method="post">
<input name="name" type="hidden" value="<?=$row_user['name']?>" id="name" size="32">
<input name="email" type="hidden" value="<?=$row_user['email']?>" id="email" size="32"> <br/>
<textarea name="comment" cols="45" rows="6" id="comment" class="input" style="width:90%"></textarea><br/>
<input class="submit" type="submit" name="Submit" value="Send">
</form>
<?php
};
?>
Alright, I think this is a minor mistake and you are getting confused because of the too many error counter flags.
Try changing this:
if($error_message == 0){
to:
if($error_message == 0 && $row_user['district'] != NULL){
I'm very new to PHP coding.
I've done tons of research to try and help me. As you can imagine I've gotten tons of material for help. The problem is when I'm trying to put it all together.
Specifically here is my problem. I've come across:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "e-Mail is Valid";
} else {
echo "Invalid e-Mail";
}
But I have no idea how to implement it. As it stands now the validator checks the fields before the user has time to imput them..... I'm desperate
I'm sure the solution is really simple, but I've spent hours on this and am really desperate for this problem to be solved already.
Here's a link to the page
Here is the code for the page:
<!DOCTYPE html>
<head>
<meta charset='utf-8'>
<title>AWalsh Photography - Contact Me</title>
<link href="style/main_page.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="email_container">
<h1 class="email_head"> Contact Andrew walsh Photography</h1>
<form id="email_form" name="email_form" method="post">
<table>
<tr>
<td><label for="fname">First Name:</label>
</td>
<td><input type="text" name="fname_input" id="fname_input" /><br>
</td>
</tr>
<tr>
<td><label for="lname">Last Name:</label>
</td>
<td><input type="text" name="lname_input" id="lname_input" /><br>
</td>
</tr>
<tr>
<td><label for="email_input">Your Email:</label>
</td>
<td><input type="text" name="email_input" id="email_input" /><br>
</td>
</tr><tr>
<td><label for="email_conf">Re-enter Email:</label>
</td>
<td><input type="text" name="email_conf" id="email_conf" /><br>
</td>
</tr><tr>
<td>
<label for="message_input">Message </label>
</td><td>
<textarea rows="8" cols="45" id="message_input" name="message_input"></textarea>
</td></tr><tr><td></td>
<td>
<input id="submit"type="submit" value="submit" name="submit"/>
</td></tr>
</table>
</form>
<?php
if($_POST['email_imput'] == $_POST['email_conf']){
//stuff to do on success
echo '<h1>Success!!</h1>';
} else {
//stuff to do on failure
echo '<h1>Sorry, The emails you entered do not match</h1>';
}
$email_imput = $_POST['email_imput'];
if (filter_var($email_imput, FILTER_VALIDATE_EMAIL)) {
echo $email_imput . ' is a valid email address.';
} else {
echo $email_imput . ' is not a valid email address.';
}
$message_imput = $_POST['message_imput'];
$msg = "Email address: $email_imput \n" . "Message: $message_imput";
$to = 'myemail#gmail.com ';
$subject = 'AWP_email';
if (filter_var($email_imput)){
mail($to, $subject, $msg, $email);
}
if (mail($to, $subject, $msg, $email)) {
echo("<p>Message successfully sent! Thanks for submitting your message. We will reply to you as soon as possible</p>");
} else {
echo("<h1>Sorry, There was an error in your imput. Please try again.</h1>");
}
?>
<span class="error"><?=$error;?></span>
<form method="post" action="">
<h1> There was an error with your post</h1>
</form>
</div>
</div>
</body>
</html>
Any input would be amazing. Thank you.
You could add a hidden field into the form and check it's value when it's time to send the email.
if (isset($_POST["some_hidden_field"])) {
// put form validation and sending email here
}
else {
// print the form
}
You should first check whether the page has been submitted or not. You might want to try if ($_SERVER['METHOD'] == 'POST') before making any validations
I'm working on a very simple, very easy contact form and when i did it on a separate page it worked perfectly, but when i added it to the current website it can't get the $_POST i don't know why. here are the codes
$to ="enter email here";
$name = $_POST["name"];
$email = $_POST["email"];
$header = "From " . $name;
$message = $_POST["message"];
$content = "From: ". $name ."<br /> Email: " . $email ."<br /> Message: " . $message;
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "illegal email";
}
else
{
if (!empty($name) && !empty($message)){
mail($to, $header, $content);
echo"sent <br />";
echo $content;
}else
{
if(empty($email))
{
echo "your email is empty";
}
elseif(empty($name))
{
echo "please enter your name";
}
elseif(empty($message)){
echo "can't send empty messages";
}
}
}
html
<form method="post" action="mail.php">
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="text" name="email"/>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<input type="text" name="subject"/>
</td>
</tr>
<tr>
<td>
Message: <br /><br/><br/>
</td>
<td>
<textarea style="resize:vertical;" name="message"></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit"/>
</td>
</tr>
</table>
</form>
thanks in advance and sorry if its a repeat
Check that the PHP is actually executing by adding something like this to the top:
echo "Testing PHP...";
If you do not see that output after submitting a form, check that you are posting the form to the right file. For example, you might need to use:
<form method="post" action="/mail.php">
or
<form method="post" action="/php/mail.php">
...code depending on your website structure.
It's certainly not $_POST that's broken, so it must be something either server related or an error in your code.
Do you have any other PHP on the website your importing the form to? If so you need to make sure that it isn't affecting it in any way.
One more thing to check, it has been reported that a PHP update accidently changed the upload limit size from "8M" to "10MB". Have a scan through your php.ini file and make sure that their isn't any unwanted "MB" instead of "M" in your upload limit.
One final suggestion I can give if you still haven't found the cause after this, is try using:
<?php var_dump($_POST); ?>
which should reveal what's really there.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send email with attachment using PHP?
hello iam new to php please let me know is any script or link for ( php script to send mail with file multiple attachments) and the html form also to how to connect this form or any inbuilt script so that i can upload to my server.
i already try in many ways by coping the codes and pasting them and changing there path but still it get many errors so please let me know if there inbuilt script easily upload to my server
<?php
if($_GET['name']== '' || $_GET['email']=='' || $_GET['email']=='' || $_GET['Message']=='' )
{
?>
<form action="check3.php" method="get" name="frmPhone">
<fieldset>
<legend style="color:#000">Contact </legend>
<table width="100%">
<tr>
<td width="29%">
<label for="name" <?php if(isset($_GET['Submit']) && $_GET['name']=='') echo "style='color:red'"; ?>>Name* </label> </td> <td width="71%">
<input id="name" name="name" type="text" style="width:50%" value=" <?php echo $_GET['name']; ?>"/> </td> </tr> <tr> <td>
<label for=" email" <?php if(isset($_GET['Submit']) && $_GET['email']=='') echo "style='color:red'"; ?>>Email* </label> </td> <td>
<input id="email" name="email" type="text" style="width:50%" value=" <?php echo $_GET['email']; ?>"/> </td> </tr>
</table>
</fieldset>
<fieldset>
<legend style="color:#000">Inquiry </legend>
<table width="100%">
<tr> <td width="41%" valign="top">
<label for="Message" <?php if(isset($_GET['Submit']) && $_GET['Message']=='') echo "style='color:red'"; ?>>Message*</label> </td> <td width="59%"> <textarea name="Message" rows="5" style="width:90%" id="Message"> <?php echo $_GET['Message']; ?> </textarea> </td><div align="center">Photo
<input name="photo" type="file" size="35" />
</div></td>
</tr>
<tr>
<input name="Submit" type="submit" value="Submit" />
</form> </td> </td>
</form>
</tr>
<?php
}
else
{
$to = 'abhi326#gmail.com';
$subject = 'Customer Information';
$message = '
Name: '.$_GET['name'].'
Email Address: '.$_GET['email'].'
Message: '.$_GET['Message'];
$headers = 'From:'.$_GET['email'];
mail($to, $subject, $message, $headers);
$connection=mysql_connect("db2173.perfora.net", "dbo311409166", "malani2002") or die(mysql_error());
$query = mysql_query("INSERT INTO `feedback` ( `name` , `email` , `Message` , `photo` ) VALUES ('".$_GET['name']."', '".$_GET['email']."', '".$_GET['Message']."')");
define ("MAX_SIZE","75");
if(isset($_FILES['photo']['name']) && $_FILES['photo']['name']<>"") {
$typ = $_FILES['photo']['type'];
if($typ == "image/g if" || $typ == "image/png" || $typ == "image/jpeg" || $typ == "image/pg if" || $typ == "image/ppng" || $typ =="image/JPEG")
{
$uploaddir = "contacts/";
$uploadimages = $uploaddir.basename($_FILES['photo']['name']);
if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadimages)) {
echo "File successfully copied";
$sql="INSERT INTO contacts (Photo, Beschrijving)
VALUES ('$uploadimages',
'$_POST[images]')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
mysql_close($con);
}
}
else {echo "Copy unsuccessful"; }
}
else {
echo "Incorrect file type";
}
}
else {
echo "No file selected";
}
echo "Thank you! ";
}
?>
thanks and regards
abhi
The script you're lookingfor is phpMailer.
This script can be downloaded and is easily added to your PHP programs. It makes sending emails from PHP extremely easy, including adding attachments.
Hope that helps.