我有两个PHP文件。第一个是显示电话.php
<!DOCTYPE html>
<html>
<?php
$labels=array("first_name"=>"First Name",
"last_name"=>"Last Name",
"phone"=>"Phone");
?>
<body>
<h3>please enter your phone number below</h3>
<form action='savePhone2.php' method='POST'>
<?php
//loop that displays the form field
foreach($labels as $field =>$value)
{
echo "$field <input type='text' name='$field' size='65' maxlenghth='65'/><br/>";
}
echo "<input type='submit' value='submit phone number'/>";
?>
第二个是保存电话2.php
<?php
$labels=array("first_name"=>"First Name",
"last_name"=>"Last Name",
"phone"=>"Phone");
?>
<body>
<?php
foreach($_POST as $field =>$value)
{
if(empty($value))
{
$blank_array[]=$field;
}
elseif(preg_match("/name/i",$field))
{
if(!preg_match("/^[A-Za-z' -]{1,50}$/",$value))
{
$bad_format[]=$field;
}
}
elseif($field=="phone")
{
if(!preg_match("/^('('d+')|'d+'-)?'d{10,20}$/",$value))
{
$bad_format[]=$field;
}
}
}
if(@sizeof($blank_array)>0 or @sizeof($bad_format)>0)
{
if(@sizeof($blank_array)>0)
{
echo "<p>input";
foreach($blank_array as $value)
{
echo " $labels[$value]";
}
echo "</p>";
}
if(@sizeof($bad_format)>0)
{
echo "<p>invalid format";
foreach($bad_format as $value)
{
echo $labels[$value];
}
echo "</p>";
}
//redisplay form
echo "<hr/>";
echo "enter phone number";
echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
foreach($labels as $field =>$label)
{
$good_data[$field]=strip_tags(trim($_POST[$field]));
echo "$label <input type='text' name='$field' size='65' maxlength='65' value='$good_data[$field]'/><br/>";
}
echo "<input type='submit' value='submit phone number'/>";
exit();
}
else
{
$user='root';
$host='localhost';
$password='root';
$dbname='pet';
$cxn=mysqli_connect($host,$user,$password,$dbname) or die("can't connect to server");
foreach($labels as $field =>$value)
{
$good_data[$field]=strip_tags(trim($_POST[$field]));
$good_data[$field]=mysqli_real_escape_string($cxn,$good_data[$field]);
}
$check_exist="SELECT ";
foreach($labels as $field =>$value)
{
$check_exist.=$field.",";
}
$check_exist=preg_replace("/,{3}/","",$check_exist);
$check_exist.=" FROM data WHERE ";
foreach($good_data as $field =>$value)
{
$check_exist.=$field."="; $check_exist.="'$value'".",";
}
echo $check_exist;
我有一个问题,我不知道如何删除phone,
和phone='0123456789087'
的小数,关于这个查询:
SELECT first_name,last_name,phone, FROM data WHERE
first_name='sloth',last_name='dig',phone='0123456789087',
从列表构建字符串时,控制尾随字符的最简单方法是连接数组的准备好元素。
改变
$check_exist="SELECT ";
foreach($labels as $field =>$value)
{
$check_exist.=$field.",";
}
自
$check_exist="SELECT ";
$fieldArray = array();
foreach($labels as $field =>$value)
{
$fieldArray[] = $field;
}
$check_exist .= join(', ', $fieldArray);
以同样的方式,您可以更改
foreach($good_data as $field =>$value)
{
$check_exist.=$field."="; $check_exist.="'$value'".",";
}
自
$whereArray = array();
foreach($good_data as $field =>$value)
{
$whereArray[] = $field . "=" . "'$value'";
}
$check_exist .= join(' AND ', $whereArray);
这应该会产生SELECT first_name, last_name, phone FROM data WHERE
first_name='sloth' AND last_name='dig' AND phone='0123456789087'
- 请注意,在where
条件之后,由 AND
而不是逗号,
连接。当然,这只有在$good_data
和$labels
不为空时才有效。
不确定我是否理解你的问题。
您的意思是从数据库中检索时电话号码中有小数点(句点,点)并且您希望删除它们吗?
php小提琴在这里
㞖:
<?php
$n = "123.456.7890";
$o = str_replace(".", "", $n);
echo $o;
$x = "123,456,7890";
$y = str_replace(",", "", $x);
echo $y;
您可以继续使用您的代码
foreach($good_data as $field =>$value)
{
$check_exist.=$field."="; $check_exist.="'$value'".",";
}
和干净的$check_存在于最后
$check_exists = rtrim($check_exists, ",");