我想在表中插入一个值,但首先输入的数据我检查使用正则表达式输入的数据的模式
这是我的代码..但它有用吗能有人告诉我我的代码有问题吗?
<?php
extract($_POST);
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rootpass"; // Mysql password
$db_name="myshop"; // Database name
$name=trim($Gname); //trimming the name
$pattern="/^[a-zA-Z]$/";
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
if ($name=="")
echo ("Missing Info");
else
{
if (isset($name))
{
if (preg_match($pattern,$name))
$sql="INSERT INTO ayesha VALUES('$name')";
else
echo("wrong syntax");
}
else
{
echo("missing info");
}
?>
答案 0 :(得分:2)
尝试此$pattern="/[a-zA-Z]+/";
而不是$pattern="/^[a-zA-Z]$/";
$pattern="/[a-zA-Z]+/";
//your code
if (!empty($name)) {
if (preg_match($pattern,$name)) {
$sql="INSERT INTO ayesha VALUES('$name')";
} else {
echo("wrong syntax");
}
} else {
echo("missing info");
}
?>