<!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'/>";
?>
第二个是savePhone2.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',
答案 0 :(得分:1)
从列表构建字符串时控制尾随字符的最简单方法是连接数组的预处理元素。
更改
$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
不为空时才会有效。
答案 1 :(得分:0)
不确定我是否理解你的问题。
您是说从数据库中检索到电话号码时是否有小数点(句点,点),并且您希望将其删除?
如果是这样的话:
<?php
$n = "123.456.7890";
$o = str_replace(".", "", $n);
echo $o;
$x = "123,456,7890";
$y = str_replace(",", "", $x);
echo $y;
答案 2 :(得分:0)
您可以继续使用您的代码
foreach($good_data as $field =>$value)
{
$check_exist.=$field."="; $check_exist.="'$value'".",";
}
并在最后清理$ check_exists
$check_exists = rtrim($check_exists, ",");