简体   繁体   English

SQL查询中的数组值?

[英]Array values in SQL query?

$sql = mysql_query("INSERT INTO `users` VALUES(
        '',
        '$form['name']',
        '$form['saddress']',
        '$form['apt']',
        '$form['zip']',
        '$form['homephone']',
        '$form['cellphone']',
        '$form['email']',
        '$form['    ']',
        '$form['city']',
        '$form['state']',
        '$form['country']',
        '$salt','$hash',
        '$form['username']'
    )");

How would I make that work? 我将如何进行这项工作? It's giving me Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING 它给我Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Try using curly brackets around the variables, like this: 尝试在变量周围使用大括号,如下所示:

..
'{$form['name']}',
'{$form['saddress']}',
..

Either by removing the single quotes from the $form['x'] or by doing something like: 通过从$form['x']删除单引号或执行以下操作:

mysql_query("INSERT INTO x VALUES(
  '" . mysql_real_escape_string($form['x']) . "',
  '" . mysql_real_escape_string($form['x']) . "'
");

Notice that there are double quotes inside the single quotes. 请注意,单引号内有双引号。

This is a classic problem that stems from PHP's ability to parse strings for variables, which can get confusing. 这是一个经典的问题,源于PHP解析字符串以获取变量的能力,这可能会造成混淆。 I prefer to keep my variables outside of the strings, and concatenate when needed. 我更喜欢将变量保留在字符串之外,并在需要时进行连接。

Here's what I would do: 这就是我要做的事情:

$sql = mysql_query("INSERT INTO `users` VALUES('" .
    implode("','", array(
        "",
        $form['name'],
        $form['saddress'],
        $form['apt'],
        $form['zip'],
        $form['homephone'],
        $form['cellphone'],
        $form['email'],
        $form['    '],
        $form['city'],
        $form['state'],
        $form['country'],
        $salt,
        $hash,
        $form['username']
    )) . "')";
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM