doulun1915
2012-02-01 15:32
浏览 102
已采纳

PHP / MySql插入语句不起作用

I have never done this before, and this is the first time I have encountered this situation

Here is my code that I created:

$query = mysql_query("INSERT INTO customerinfo (CustomerID, FirstName, LastName, PhoneNumber, PhoneNumber, PhoneNumber, Email, Symptoms) VALUES ('', '{$first}', '{$last}', {$codearea}, {$threedigit}, {$fourdigit}, '{$email}')", $connection);

As you can see, I have table that has 6 columns, which are:

  1. CustomerInfo
  2. FirstName
  3. LastName
  4. PhoneNumber
  5. Email
  6. Symptoms

Now, when a client fills out the form, it should insert it into the database. However, if you look at my insert statement, I listed PhoneNumber column 3 times because the form has 3 fields(code area, prefix, and line number) on the HTML page, and I want all three fields insert into the PhoneNumber column. How can I do this?

Thank you!

图片转代码服务由CSDN问答提供 功能建议

我以前从未这样做过,这是我第一次遇到这种情况

这是我创建的代码:

  $ query = mysql_query(“INSERT INTO customerinfo(CustomerID,FirstName,LastName,PhoneNumber,PhoneNumber,PhoneNumber,Email)  ,症状)VALUES('','{$ first}','{$ last}',{$ codearea},{$ threedigit},{$ fourdigit},'{$ email}')“,$ connection);  
   
 
 

如您所见,我的表有6列,分别为:

  1. CustomerInfo
  2. 名字
  3. ******中国
  4. 电子邮件
  5. 症状

    现在,当客户端填写表单时,它应该将其插入到数据库中。 但是,如果查看我的insert语句,我列出了PhoneNumber列 3次,因为表单在HTML页面上有3个字段(代码区,前缀和行号),我希望所有三个字段都插入到PhoneNumber列中。 我该怎么做?

    谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • dongyao4003 2012-02-01 15:38
    已采纳

    Concatenate the values into a single string before executing the query.

    $phone = $codearea . $threedigit . $fourdigit;
    $query = mysql_query("INSERT INTO customerinfo (CustomerID, FirstName, LastName, PhoneNumber, Email, Symptoms) VALUES ('', '{$first}', '{$last}', '{$phone}', '{$email}')", $connection);
    
    点赞 打赏 评论
  • duanchuopi2298 2012-02-01 15:34

    PHP mysql_ is deprecated, use http://php.net/pdo instead.

    Properly escape your column names and use PDO prepared statements to set bindings.

    INSERT INTO `customerinfo` (`CustomerInfo`, `FirstName`, `LastName`) VALUES (:customer_info, :first_name, :last_name);
    

    Once you have this, you can execute the query like this.

    $sth    = $db->prepare([the query from the above]);
    $sth->execute(array('customer_info' => implode('; ', array([data that you want to concatenate])), 'first_name' => 'name', 'last_name' => 'name'));
    
    点赞 打赏 评论
  • dongtuo2373 2012-02-01 15:36

    There is no way to store three values in one field.

    You must

    • either use three columns for PhoneNumber (code area, prefix, and line number)
    • or somehow make one value out of the three (e.g. by making a csv string out of it).
    点赞 打赏 评论
  • dongwo1914 2012-02-01 15:36

    You just need to concatenate those with a . in PHP and insert as a single field. You could do something similar in MySQL but it is a bit more messy.

    点赞 打赏 评论
  • dtntjwkl83750 2012-02-01 15:36

    If you're prompting for separate areacode/prefix/line data in the form, you should keep them separate in the database as well - three separate fields instead of a single "phone number" fields.

    However, if that's not possible, then concatenate the values in PHP:

    INSERT INTO ... (..., PhoneNumber, ...) VALUES (..., '{$codearea}-{$threedigit}-{$fourdigit}', ...)
    
    点赞 打赏 评论
  • douxiangbiao1899 2012-02-01 15:38

    You will want to use PHP to merge the 3 HTML form fields together before you insert:

    $vPhoneNumber = $part1 . $part2 . $part3

    You can then insert $vPhonenumber in your SQL.

    点赞 打赏 评论
  • drh78568 2012-02-01 15:38

    Use a variable to store all related phone values, like this:

    $phoneNumber = $_POST['codearea'] + " " + $_POST['threedigit'] + " " + $_POST['fourdigit'];
    

    Then, change your query to:

    INSERT INTO customerinfo 
    (CustomerInfo, FirstName, LastName, PhoneNumber, Email, Symptoms) 
    VALUES 
    ('', '{$first}', '{$last}', '{$phoneNumber}', '{$email}')", $connection);
    

    This aproach you're using will difficult your work if you have to present the data from the database to html, in 3 fields. Probably you'll need "explode" the phone string in 3. Would be better if you were using only one html field or 3 collumns in your database table.

    点赞 打赏 评论
  • dora1989 2012-02-01 15:41
    $query = mysql_query(
        "INSERT INTO customerinfo (
            CustomerInfo, 
            FirstName, 
            LastName, 
            PhoneNumber, 
            Email, 
            Symptoms
        ) VALUES (
            '', 
            '".$first."', 
            '".$last."', 
            '".$codearea.$threedigit.$fourdigit."', 
            '".$email."',
            '".$symptoms."'
        )", $connection);
    
    点赞 打赏 评论

相关推荐 更多相似问题