作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我真的不知道我在这里做错了什么。由于某种原因,数据不会插入到数据库中。我认为问题在于我将数据保存到局部变量中并为了安全起见将它们转义。所以,如果我删除那组局部变量,那么显然我在数据库中得到一个空行,如果我把它们留在那里,那么它什么也不会做,但我需要有那组局部变量来保护数据。我还使用 PHP 正则表达式验证数据,我知道这不是问题所在。
如果有人能找到问题,请告诉我。
PHP
我知道这看起来不太好,但这是我能做的最好的了。
if(isset($_POST['submit'])){
$errors = array();
// Check name is valid
if(empty($_POST['full_name'])):
$errors['full_name'] = "";//"Please enter your name."
echo "<script type='text/javascript'>$(document).ready(function(){ $('#full_name').addClass('input-error')});</script>";
elseif(!preg_match('/\b([A-Z]{1}[a-z]{1,30}[- ]{0,1}|[A-Z]{1}[- \']{1}[A-Z]{0,1}[a-z]{1,30}[- ]{0,1}|[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}){2,5}/', $_POST['full_name'])):
$errors['full_name'] = "";//"Please enter a valid name."
echo "<script type='text/javascript'>$(document).ready(function(){ $('#full_name').addClass('input-error')});</script>";
else: endif;
// Check email is valid
if(empty($_POST['email'])):
$errors['email'] = ""; //"Please enter your email.
echo "<script type='text/javascript'>$(document).ready(function(){ $('#email').addClass('input-error')});</script>";
elseif (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)): // validate the email *** REQUIRES PHP 5.2 ***
$errors['email'] = "";//'Please enter a valid email.
echo "<script type='text/javascript'>$(document).ready(function(){ $('#email').addClass('input-error')});</script>";
else: endif;
if(!empty($_POST['phone'])){
//Check phone is valid Matches - 14165551212, 4165551212, (416)5551212, 416 555 1212, 416-555-1212, (416)-555-1212, (416) 555 1212, 1-900-888-1212
if(!preg_match('/^(1?)(-| ?)(\()?([0-9]{3})(\)|-| |\)-|\) )?([0-9]{3})(-| )?([0-9]{4}|[0-9]{4})$/', $_POST['phone'])):
$errors['phone'] = "";//"Please enter a valid phone number.
echo "<script type='text/javascript'>$(document).ready(function(){ $('#phone').addClass('input-error')});</script>";
else: endif;
}
// Check subject is valid
if(empty($_POST['subject'])):
$errors['subject'] = "";//"Please enter your subject.
echo "<script type='text/javascript'>$(document).ready(function(){ $('#subject').addClass('input-error')});</script>";
elseif(!preg_match("/^(([a-zA-Z])+\s)?[a-zA-Z]+$/", $_POST['subject'])):
$errors['subject'] = "";//"Please enter a subject.
echo "<script type='text/javascript'>$(document).ready(function(){ $('#subject').addClass('input-error')});</script>";
else: endif;
// Check URL is valid Matches - http://regexlib.com | http://www.google.com | ftp://teach.me.regex/checkpattern/o | http://www.google.com/search?hl=en&source=hp&q=asp.net | https://secure.mailserver.com | http://localhost/mypage.html | http://localhost:89783/mypage.aspx | http://go.com | http://forum.whoisyourdaddy.org/index.html?RegID=7449046&Daddy=dontknow&son=me
if(!empty($_POST['url'])){
if(!preg_match("/^(ht|f)tp(s?)\:\/\/(([a-zA-Z0-9\-\._]+(\.[a-zA-Z0-9\-\._]+)+)|localhost)(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?([\d\w\.\/\%\+\-\=\&\?\:\\\"\'\,\|\~\;]*)$/", $_POST['url'])):
$errors['url'] = ""; //"Please enter a valid URL address.
echo "<script type='text/javascript'>$(document).ready(function(){ $('#url').addClass('input-error')});</script>";
else: endif;
}
// Check message is valid
if(empty($_POST['message'])):
$errors['message'] = "";//"Please enter your message.
echo "<script type='text/javascript'>$(document).ready(function(){ $('#message').addClass('input-error')});</script>";
elseif(!preg_match("/^(([a-zA-Z])+\s)?[a-zA-Z]+$/", $_POST['message'])):
//$errors['message'] = "Please enter a minimum or more than 50 characters.";
//echo "<script type='text/javascript'>$(document).ready(function(){ $('#message').addClass('input-error')});";
else: endif;
if ( (strlen( $message ) >= 50) && (strlen( $message ) <= 1500)) {
} else {
//$errors['message'] = "Please enter from 50 to 1500 characters.";
echo "<script type='text/javascript'>$(document).ready(function(){ $('#message').addClass('input-error')});</script>";
}
// If no validation errors
if(0!==count($errors)) {
echo "<script type='text/javascript'>$(document).ready(function(){ $('.error-container').show()});</script></strong>";
}
elseif(0===count($errors)){
include_once('admin/includes/database.php');
$conn = db_connect();
// Save data into local variables and escape them for security
$name = mysqli_real_escape_string($conn,$_POST['full_name']);
$email = mysqli_real_escape_string($conn,$_POST['email']);
$phone = mysqli_real_escape_string($conn,$_POST['phone']);
$subject = mysqli_real_escape_string($conn,$_POST['subject']);
$url = mysqli_real_escape_string($conn,$_POST['url']);
$message = mysqli_real_escape_string($conn,$_POST['message']);
// An insertion query. $result will be `true` if successful
$insertSQL = "insert into users (fullname,email,phone,subject,url,message) VALUES ('$name','$email','$phone','$subject','$url','$message')";
$run_insertion = mysqli_query($conn, $insertSQL);
if ($run_insertion === false) {
$error = db_error();
} else {
echo "<script>alert('Your submisision was successfully sent!')</script>";
}
}
}
HTML
<form action="contact.php" method="post" id="form1" name="form1">
<div class="error-container">
<div class="error-content">
<div class="error-header">
<div class="error-icon"></div>
<div class="error-title">Oops, it looks like something wasn't right.</div>
</div> <!-- END OF error-header -->
<div class="error-message-content">Mistakes are marked below. <br>Correct the errors and resubmit the form.</div> <!-- END OF error-message-content -->
</div> <!-- END OF error-content -->
</div> <!-- END OF error-container -->
<div class="form-element"><label for="name"><b>Name <div>*</div></b></label><br>
<div class="input-wrapper <?php echo form_row_class("full_name") ?>"><input type="text" class="input" name="full_name" placeholder="Full name" id="full_name" value="<?php echo h($_POST['full_name']); ?>" /><font color="red"><?php echo error_for('full_name') ?></font></div>
</div>
<div class="form-element">
<label for="email"><b>Email <div>*</div></b></label><br>
<div class="input-wrapper <?php echo form_row_class("email") ?>"><input type="text" id="email" class="input" name="email" placeholder="jhon@example.com" value="<?php echo h($_POST['email']); ?>" /><font color="red"><?php echo error_for('email') ?></font></div>
</div>
<div class="form-element">
<label for="phone">Phone</label><br>
<div class="input-wrapper <?php echo form_row_class("phone") ?>"><input type="text" id="phone" class="input" name="phone" placeholder="1 800 000 0000" value="<?php echo h($_POST['phone']); ?>"/><font color="red"><?php echo error_for('phone') ?></font></div>
</div>
<div class="form-element">
<label for="subject"><b>Subject <div>*</div></b></label><br>
<div class="input-wrapper <?php echo form_row_class("subject") ?>"><input type="text" id="subject" class="input" name="subject" placeholder="Subject" value="<?php echo h($_POST['subject']); ?>" /><font color="red"><?php echo error_for('subject') ?></font></div>
</div>
<div class="form-element">
<label for="company">URL</label><br>
<div class="input-wrapper <?php echo form_row_class("url") ?>"><input type="text" id="url" class="input" name="url" placeholder="URL" value="<?php echo h($_POST['url']); ?>" /><font color="red"><?php echo error_for('url') ?></font></div>
</div>
<div class="form-textarea">
<label for="message"><b>Your Message <div>*</div></b></label><br>
<div class="textarea-wrapper <?php echo form_row_class("message") ?>"><textarea class="textarea" id="message" name="message" placeholder="Your message" ><?php echo h($_POST['message']); ?></textarea><font color="red"><?php echo error_for('message') ?></font></div>
</div>
<div class="submit-element">
<input type="submit" class="submit" name="submit" value="Submit" />
</div>
</form>
最佳答案
正如 Nikos 所说,您应该使用 $link = mysqli_connect 进行连接。
检查你的错误,
if ($run_insertion) {
echo "<script>alert('Your submisision was successfully sent!') /script>";
}else{
echo ("Could not insert data : " . mysqli_error($link) . " " . mysqli_errno($link));
}
关于php - 如何在php中使用mysqli向数据库中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918959/
我是一名优秀的程序员,十分优秀!