0

我有两个 php 文件。第一个是 displayPhone.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'/>";
?>

第二个是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;

在此查询中,我遇到一个问题,我不知道如何删除 atphone,和 at的小数:phone='0123456789087'

SELECT first_name,last_name,phone, FROM data WHERE
    first_name='sloth',last_name='dig',phone='0123456789087',
4

3 回答 3

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'- 注意 afterwhere条件是AND用逗号而不是逗号连接的,。当然,这只有在$good_data并且$labels不为空时才有效。

于 2013-09-27T16:12:31.217 回答
0

Not sure if I understand your question.

Do you mean that there are decimal points (periods, dots) in the phone number when retrieved from the database and you wish to remove them?

phpFiddle here

If so:

<?php
$n = "123.456.7890";
$o = str_replace(".", "", $n);
echo $o;

$x = "123,456,7890";
$y = str_replace(",", "", $x);
echo $y;
于 2013-09-27T16:19:23.100 回答
0

You may continue to use your code

foreach($good_data as $field =>$value)
{
    $check_exist.=$field."="; $check_exist.="'$value'".",";
}

and clean $check_exists at the end

$check_exists = rtrim($check_exists, ",");
于 2013-09-27T16:19:30.213 回答