php - 将输入表单更改为文本区域而不影响数据库

标签 php html mysql twitter-bootstrap-3

我制作了一个完整的网站,有4个表单输入,输入是文本输入,并且连接到MySQL数据库。现在我想将这些文本输入之一更改为文本框,当我这样做时,它不再将特定“消息”字段的数据发送到数据库。

//php code to insert to db
 <?php

$errors         = array();  	// array to hold validation errors
$data 			= array(); 		// array to pass back data
$email = $_POST['email'];
$phone = $_POST['phone_number'];
$name = $_POST['name'];
$message=$_POST['message'];

// validate the variables ======================================================
	// if any of these variables don't exist, add an error to $errors array 

	if (empty($_POST['name']))
	{
		$errors['name'] = 'Name is required.';
	}

	if (empty($_POST['email']))
	{
		$errors['email'] = 'Email is required.';
	} else {
		$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
		
		if(!preg_match($email_exp,$email))
		{
			$errors ['email']= '<b>The email you entered does not appear to be valid</b></br>';
		}
	}

	if (empty($_POST['phone_number'])){
		$errors['phone_number'] = 'Phone number is required.';
	} else{
		$phone_number_exp ='/^[0-9]{5,12}$/';
		if(!preg_match($phone_number_exp, $phone))
		{
		   $errors ['phone_number']='<b>Phone number entered is invalid</b></br>';
		}																		
	}
	if (empty($_POST['message'])){
		$errors['message'] = 'Message is required.';
	}

// return a response ===========================================================

	// if there are any errors in our errors array, return a success boolean of false
	if ( ! empty($errors)) {

		// if there are items in our errors array, return those errors
		$data['success'] = false ;
		$data['errors']  = $errors ;	
	} else 
	{

		// success processing


//connecting to the db

$dbhost = 'localhost';		
$dbuser = 'root';
$dbpass = '';
$db = '1';

$dbconn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db);

$query = ("INSERT INTO user (name, email, phone_number, message) VALUES ('$name','$email','$phone','$message')");
mysql_query($query);	
		
	       {
				$data['1'] = false;
	 		}
// email sent from gmail client id = --- 
$email_to="---";
$email_subject="Lead Form"; 
$msg = "Form details from interested customer:\n\n";
	
	 function clean($string)
	 {
	 	$sickstring = array("content-type","bcc:","to:","cc:","href");
	 	return str_replace($sickstring,"-",$string);
	 	//replaces any characters found in the array with a "-".
	 } 
		$msg .= "Email: ".clean($email)."\n";
	    $msg .= "Phone Number: ".clean($phone)."\n";
		$msg .= "Name: ".clean($name)."\n";
		$msg .= "Message: " . ($message)."\n";

	  /* if using the name variable
	 $message .= "name: ".clean($name)."\n"; */
	 
	 //header for email.. variable not working , check variable on thursday ..  
	 
	 $headers = "From: " . ($email) . "\r\n".
	 "Reply_To: " . ($email) . "\r\n".
	 "xmailer: php/" . phpversion();
	 
	 @mail($email_to, $email_subject, $msg, $headers);

	 $data['success'] = true;
	 $data['message'] = 'Success!'; 
	
	}// return all our data to an AJAX call
	echo json_encode($data);
 <form action="process.php" method="POST" id="ezform">
    
        
        <!-- NAME -->
        <div id="name-group" class="form-group">
            <label for="name">Name</label>
            <input type="text" class="form-control" name="name" placeholder="Name">
            
        </div>

        <!-- EMAIL -->
        <div id="email-group" class="form-group">
            <label for="email">Email</label>
            <input type="text" class="form-control" name="email" placeholder="example@etc.com">
            
        </div>

        <!-- NUMBER -->
        <div id="phone_number-group" class="form-group">
            <label for="phone_number">Phone Number</label>
            <input type="text" class="form-control" name="phone_number" placeholder="Number">
            
        </div>
        <!--MESSAGE-->
        <div id="message-group" class="form-group">
            <label for="message">Message</label>
            <input type="text" class="form-control" name="message" placeholder="Your Message">
            
        </div>
         <div id="Demo-BS" style="padding:30px;">
   <button type="submit" class="btn btn-default">Submit</button>
  </div>

    </form>

最佳答案

将此代码添加到您编写的查询上方。

$message = htmlentities($message);

虽然您使用的是文本区域,所以您应该使用 htmlentities 来转义 HTML 字符

关于php - 将输入表单更改为文本区域而不影响数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601866/

相关文章:

mysql - MySQL中有这样的列类型吗?

php - 如何创建 JSON 对象

PHP 文件输出,提要中的随机空格

php - 如何从表中打印数据?

javascript - PHP round($num,2) 和 javascript toFixed(2) 没有给出该值 53.955 的正确输出

javascript - 使用 HTML 和 Java 脚本在表格中动态添加按钮

php - 通过模拟数据或使用空白替换 "NA"和空白

html - 删除 HTML 标签但仍允许特殊字符

javascript - 将复选框放在 JavaScript 的组合框中

mysql - 当您必须在许多查询中使用派生列时,创建表是否更好?