【问题标题】:on form submission values not inserting into database in php关于表单提交值未在 php 中插入数据库
【发布时间】:2020-05-15 21:37:16
【问题描述】:

我有一个 html 表单,当用户提交数据时,数据会进入数据库,这一直很好,直到我添加了另外一件事,我添加了邮件功能以在提交数据后发送邮件。我的代码如下:

<?php
error_reporting(0);

session_start();
require('db_config.php');

if (isset($_POST['submit'])) {
	
	$name = $_FILES['Photo']['name'];
	list($txt, $ext) = explode(".", $name);
	$image_name = time() . "." . $ext;
	$tmp = $_FILES['Photo']['tmp_name'];
	
	$shame = $_FILES['paymentphoto']['name'];
	list($txts, $exts) = explode(".", $shame);
	$receipt_name = time() . "." . $ext;
	$tmps = $_FILES['paymentphoto']['tmp_name'];
	
	if (move_uploaded_file($tmp, 'uploads/' . $image_name) && move_uploaded_file($tmps, 'receipt/' . $receipt_name)) {
		$sql = "INSERT INTO members (firstname, lastname, image, company, designation, addressone, addresstwo, aadhar, city, state, pin, pan, rnameone, rnametwo, mobile, alternate, email, experience, businessdate, companyregistration, gstin, servicesoffered, fee, mode, receipt) VALUES ('" . $_POST['first_name'] . "','" . $_POST['last_name'] . "' , '" . $image_name . "','" . $_POST['company'] . "', '" . $_POST['designation'] . "','" . $_POST['address'] . "', '" . $_POST['address2'] . "', '" . $_POST['aadhaar'] . "', '" . $_POST['city'] . "', '" . $_POST['state'] . "', '" . $_POST['pin'] . "', '" . $_POST['pan'] . "', '" . $_POST['recommended'] . "', '" . $_POST['recommended2'] . "','" . $_POST['mobile'] . "', '" . $_POST['alternate'] . "', '" . $_POST['email'] . "', '" . $_POST['experience'] . "', '" . $_POST['date'] . "', '" . $_POST['registration'] . "', '" . $_POST['gst'] . "', '" . $_POST['services'] . "', '" . $_POST['fee'] . "', '" . $_POST['payment'] . "', '" . $receipt_name . "' )";
		$mysqli->query($sql);
		
		$to = "teiamembers@gmail.com"; // this is your Email address
		$from = $_POST['email']; // this is the sender's Email address
		$first_name = $_POST['first_name'];
		$last_name = $_POST['last_name'];
		
		$headers = "From:" . $from . "\nMIME-Version: 1.0\nContent-Type: text/html; charset=utf-8\n";
		$headers2 = "From:" . $to;
		$subject = "TEIA Membership Registration Request";
		$subject2 = "TEIA Membership Request";
		$message = $first_name . " has requested for TEIA Registration.  Full Name:" . " " . $first_name . " " . $last_name . "<br>" . "Email:" . $from . "<br>" . "Mobile:" . " " . $_POST['mobile'] . "<br>" . "Company Name:" . " " . $_POST['company'] . "<br>" . "Designation" . " " . $_POST['designation'] . "<br>" . "Residence Address:" . " " . $_POST['address'] . "<br>" . "Office Address:" . " " . $_POST['address2'] . "<br>" . "Aadhaar:" . " " . $_POST['aadhaar'] . "<br>" . "City:" . " " . $_POST['city'] . "<br>" . "State:" . " " . $_POST['state'] . "<br>" . "Pin:" . " " . $_POST['pin'] . "<br>" . "Pan:" . " " . $_POST['pan'] . "<br>" . "Reference:" . " " . $_POST['recommended'] . "<br>" . "Alternate Number:" . " " . $_POST['alternate'] . "<br>" . "Experience:" . " " . $_POST['experience'] . "<br>" . "Aadhaar:" . " " . $_POST['aadhaar'] . "<br>" . "Date of Business Setup:" . " " . $_POST['date'] . "<br>" . "Company Registration Number:" . " " . $_POST['registration'] . "<br>" . "GSTIN:" . " " . $_POST['gst'] . "<br>" . "Services Offered:" . " " . $_POST['services'] . "<br>" . "Fee Paid:" . " " . $_POST['fee'] . "<br>" . "Payment Mode:" . " " . $_POST['payment'] . "<br>";
		$message2 = "Your request for TEIA Membership Received. We will contact you Shortly. ";
		
		mail($to, $subject, $message, $headers);
		mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
		
		if ($mysqli) {
			$msg = "Your Request For Membership Registration Sent Successfully";
		}
	}
}
?>

现在的问题是当用户提交表单时,邮件工作正常,但值没有进入数据库,谁能告诉我这里可能出了什么问题,提前谢谢

【问题讨论】:

  • 这两个条件move_uploaded_file($tmp, 'uploads/'.$image_name) &amp;&amp; move_uploaded_file($tmps, 'receipt/'.$receipt_name) 评估为真吗?
  • 您需要添加错误报告启用。有一些警告或通知查询失败并且邮件功能有效。或检查您的日志 apache\logs\error.log 并将它们粘贴到此处
  • @YomnaHesham 是的
  • 您的脚本容易受到 SQL 注入攻击。

标签: php mysql sql forms phpmailer


【解决方案1】:

首先注意 SQL 注入。

始终需要进行一定程度的检查。至少……这个。

if($mysqli->query($sql)) { fine } else { error }

并且在上面的“错误”部分,如果您包含错误号和实际消息,您自己可能已经找到了答案。

我的主要疑问是...您有一些独特的列,并且插入没有发生,因为它之前确实出现在数据库中。

此外,最好将 id int unique auto_increment 用于多种目的。

其次,您应该已经创建了 $mailstatus 并检查了该邮件,而不是 mysqli。

if ($mailstatus) {
    $msg = "Your Request For Membership Registration Sent Successfully";
}

【讨论】:

    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 2014-03-25
    • 2016-03-24
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 2016-04-03
    • 2011-03-21
    相关资源
    最近更新 更多