如何在php中验证输入数据 [英] how to validate input data in php
问题描述
您好。
我是新人。我不知道如何验证电子邮件,网站和数值等特定数据。这是我的代码,工作正常。以下是我的代码。
请帮帮我
add.php
<?php
include_once ' ../ inc / header.php';
?>
< 脚本 语言 = javascript >
function change_action()
{
var frm_obj = document .getElementById(< span class =code-string> frm);
frm_obj.action = data.php;
}
< / 脚本 >
< form action = method = POST id = frm>
< table class = align = center cellpadding = 4 cellspacing = 1跨度>>
< tr>
< td>< label class = lbl> ID < / label > < / td >
< td>< label class = < span class =code-string> lbl>:< / label > < / td >
< td>< input type = text name = id id = id readonly = class = txt> < / td >
< / tr >
< tr>
< td>< label class = lbl>名称< / label > < / td >
< td>< label class = < span class =code-string> lbl>:< / label > < / td >
< td>< input type = text name = name id = name class = txt> < / td >
< / tr >
< tr>
< td>< label class = lbl>地址< / label > < / td >
< td>< label class = < span class =code-string> lbl>:< / label > < / td >
< td>< textarea name = 地址 id = 地址 class = txt rows = 5 cols = 40> < / textarea > < / td >
< / tr >
< tr>
< td>< label class = lbl>电话< / label > < / td >
< td>< label class = < span class =code-string> lbl>:< / label > < / td >
< td>< input type = text name = tel id = tel class = txt> < / td >
< / tr >
< tr>
< td>< label class = lbl>传真< / label > < / td >
< td>< label class = < span class =code-string> lbl>:< / label > < / td >
< td>< input type = text name = fax id = fax class = txt> < / td >
< / tr >
< tr>
< td>< label class = lbl>电子邮件< / label > < / td >
< td>< label class = < span class =code-string> lbl>:< / label > < / td >
< td>< input type = text name = email id = email class = txt> < / td >
< / tr >
< tr>
< td>< label class = lbl>网站< / label > < / td >
< td>< label 类 = lbl>:< / label > < / td < span class =code-keyword>>
< td>< input type = text name = website id = website class = txt> < / td >
< / tr >
< tr>
< td>< label class = lbl>输入< / label > < / td >
< td>< label class = < span class =code-string> lbl>:< / label > < / td >
<! - < td>< input type = text name = type id = type class = txt> < / td > - >
< td>
< SELECT NAME = type id = type>
< OPTION VALUE = 0>选择
<?php
$ sql = SELECT * FROM bk_parameter where type ='typcs'order by id;
$ result = mysqli_query($ con,$ sql);
while ($ row = mysqli_fetch_array($ result))
{
$ typename = $ row [ name];
echo < OPTION VALUE = \$ typename \> 。$ typename。' < / option>';
}
?>
< / SELECT >
< / td >
< / tr >
< tr>
< td> < / td >
< td> < / td >
< td>< input type = submit value = submit name = submit class = btn onclick = change_action()跨度>>
< input type = submit value = back name = 返回 class = btn onclick = change_action()> < / td >
< / tr >
< / 表格 >
< / 表格 >
<?php
include_once ' ../ inc / footer.php';
?>
data.php
< pre lang =PHP> <?php
include_once ' ../ INC / header.php文件'跨度>;
// 插入表格
< span class =code-keyword> if (isset($ _ POST [' submit'])&& $ _POST [' submit']!= )
{
$ name =($ _POST [ name]);
$ address =($ _POST [ 地址跨度>]);
$ tel =($ _POST [ 电话跨度>]);
$ fax =($ _POST [ 传真跨度>]);
$ email =($ _POST [ 电子邮件跨度>]);
$ website =($ _POST [ 网站跨度>]);
$ type =($ _POST [ 类型跨度>]);
尝试
{
$ sql = INSERT INTO bk_customer(姓名,地址,电话,传真,电子邮件,网站,类型)
VALUES('$ name','$ address','$ tel','$ fax', '$电子邮件', '$网站', '$类型')跨度>;
$ result = mysqli_query($ con,$ sql);
header( Location:index.php);
exit();
}
catch (例外$ ex)
{
echo $ e-> getMessage()。 \ n;
file_put_contents(' PDOErrors.txt',$ e-> getMessage(),FILE_APPEND );
exit();
}
}
// mysqli_close($ con);
// 更新表
if (isset($ _ POST [' update'])& & $ _POST [' update']!= )
{
$ id =($ _POST [ id]);
$ name =($ _POST [ 名称跨度>]);
$ address =($ _POST [ 地址跨度>]);
$ tel =($ _POST [ 电话跨度>]);
$ fax =($ _POST [ 传真跨度>]);
$ email =($ _POST [ 电子邮件跨度>]);
$ website =($ _POST [ 网站跨度>]);
$ type =($ _POST [ 类型跨度>]);
尝试
{
$ sql = UPDATE bk_customer SET name ='$ name',address ='$ address',tel ='$ tel',fax ='$ fax',
。 email ='$ email',website ='$ website',type ='$ type'WHERE id ='$ ID跨度>;
$ result = mysqli_query($ con,$ sql);
header( Location:index.php);
}
catch (例外$ ex)
{
echo $ e-> getMessage()。 \ n;
file_put_contents(' PDOErrors.txt',$ e-> getMessage(),FILE_APPEND );
exit();
}
}
// 更新记录
if (isset($ _ POST [' 删除'])&& $ _POST [' delete']!= < span class =code-string> )
{
$ id = $ _ POST [ id];
尝试
{
$ sql = DELETE FROM bk_customer WHERE id ='$ id';
$ result = mysqli_query($ con,$ sql);
header( Location:index.php);
}
catch (例外$ ex)
{
echo $ e-> getMessage()。 \ n;
file_put_contents(' PDOErrors.txt',$ e-> getMessage(),FILE_APPEND );
exit();
}
}
// 返回index.php
if (isset($ _ POST ['' back'])&& $ _POST [' 返回']!= )
{
header( Location:index.php);
} sql = SELECT * FROM bk_parameter where type ='typcs'order by id;
result = mysqli_query(
CON,
Hi.
I am new. I don't know how to validate certain data like emai, web site and numeric value. Here is my code which is work fine. Below is my code.
Pls Help me
add.php
<?php
include_once '../inc/header.php';
?>
<Script Language="javascript">
function change_action()
{
var frm_obj=document.getElementById("frm");
frm_obj.action="data.php";
}
</Script>
<form action="" method="POST" id="frm" >
<table class="" align="center" cellpadding="4" cellspacing="1">
<tr>
<td><label class="lbl">ID </label></td>
<td><label class="lbl">: </label></td>
<td><input type="text" name="id" id="id" readonly="" class="txt"></td>
</tr>
<tr>
<td><label class="lbl">Name </label></td>
<td><label class="lbl">: </label></td>
<td><input type="text" name="name" id="name" class="txt"></td>
</tr>
<tr>
<td><label class="lbl">Address </label></td>
<td><label class="lbl">: </label></td>
<td><textarea name="address" id="address" class="txt" rows="5" cols="40"></textarea></td>
</tr>
<tr>
<td><label class="lbl">Tel</label></td>
<td><label class="lbl">: </label></td>
<td><input type="text" name="tel" id="tel" class="txt"></td>
</tr>
<tr>
<td><label class="lbl">Fax</label></td>
<td><label class="lbl">: </label></td>
<td><input type="text" name="fax" id="fax" class="txt"></td>
</tr>
<tr>
<td><label class="lbl">Email</label></td>
<td><label class="lbl">: </label></td>
<td><input type="text" name="email" id="email" class="txt"></td>
</tr>
<tr>
<td><label class="lbl">Web site</label></td>
<td><label class="lbl">: </label></td>
<td><input type="text" name="website" id="website" class="txt"></td>
</tr>
<tr>
<td><label class="lbl">Type</label></td>
<td><label class="lbl">: </label></td>
<!--<td><input type="text" name="type" id="type" class="txt"></td>-->
<td>
<SELECT NAME=type id="type">
<OPTION VALUE=0>Choose
<?php
$sql="SELECT * FROM bk_parameter where type='typcs' order by id";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
$typename=$row["name"];
echo "<OPTION VALUE=\"$typename\">".$typename.'</option>';
}
?>
</SELECT>
</td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" value="submit" name="submit" class="btn" onclick="change_action()">
<input type="submit" value="back" name="back" class="btn" onclick="change_action()"></td>
</tr>
</table>
</form>
<?php
include_once '../inc/footer.php';
?>
data.php
<?php
include_once '../inc/header.php';
//insert into table
if (isset($_POST['submit']) && $_POST['submit'] != "" )
{
$name = ($_POST["name"]);
$address = ($_POST["address"]);
$tel = ($_POST["tel"]);
$fax = ($_POST["fax"]);
$email = ($_POST["email"]);
$website = ($_POST["website"]);
$type = ($_POST["type"]);
try
{
$sql="INSERT INTO bk_customer (name,address,tel,fax,email,website,type)
VALUES ('$name','$address','$tel','$fax','$email','$website','$type')";
$result = mysqli_query($con,$sql) ;
header("Location:index.php");
exit();
}
catch (Exception $ex)
{
echo $e->getMessage() . "\n";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
exit();
}
}
//mysqli_close($con);
// update table
if (isset($_POST['update']) && $_POST['update'] != "" )
{
$id= ($_POST["id"]);
$name = ($_POST["name"]);
$address = ($_POST["address"]);
$tel = ($_POST["tel"]);
$fax = ($_POST["fax"]);
$email = ($_POST["email"]);
$website = ($_POST["website"]);
$type = ($_POST["type"]);
try
{
$sql="UPDATE bk_customer SET name='$name',address='$address', tel='$tel',fax='$fax',"
. "email='$email', website='$website',type='$type' WHERE id='$id'";
$result = mysqli_query($con,$sql) ;
header("Location:index.php");
}
catch (Exception $ex)
{
echo $e->getMessage() . "\n";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
exit();
}
}
// update record
if (isset($_POST['delete']) && $_POST['delete'] != "" )
{
$id=$_POST["id"];
try
{
$sql="DELETE FROM bk_customer WHERE id='$id'";
$result = mysqli_query($con,$sql) ;
header("Location:index.php");
}
catch (Exception $ex)
{
echo $e->getMessage() . "\n";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
exit();
}
}
// back to index.php
if (isset($_POST['back']) && $_POST['back'] != "" )
{
header("Location:index.php");
}
sql="SELECT * FROM bk_parameter where type='typcs' order by id";
result = mysqli_query(
con,
这篇关于如何在php中验证输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!