简体   繁体   English

如何将简单数组插入数据库(php / MySQL)?

[英]How do I insert a simple array into a database (php/MySQL)?

I'm working on a form in which the user makes a few selections, and then each selected item's name needs to go into a table in a db. 我正在使用一种形式,用户在其中进行一些选择,然后每个选定项目的名称都需要进入db中的表中。 The user's name can go with each item as an identifier for the user's selections. 用户名可以与每个项目一起作为用户选择的标识符。 The number of selections, and the names of the selections will change but the two fields in the database remain constant. 选择的数量和选择的名称将更改,但是数据库中的两个字段保持不变。 My problem is that on insert, I get an error that there is no column with the name specified in the VALUE I'm sending. 我的问题是在插入时,我收到一个错误,即没有列与要发送的VALUE中指定的名称相同。 Its tripped me all up....can't figure out why. 它让我震惊了...。不知道为什么。 Thanks a lot for any help or advice. 非常感谢您的帮助或建议。

Full code: 完整代码:

<?php 

$db = mysql_connect('localhost', 'root', 'root') or die(mysql_error());
mysql_select_db('reflex') or die(mysql_error());

$name_array = array('harp1','harp2','harp3');

if (isset($_POST['interests'])) {

    $interests_str = implode(" ", $_POST['interests']);// converts $_POST interests into a string
    $interests_array = explode(" ", $interests_str);// converts the string to an array which you can easily manipulate  




            foreach ($name_array as $row=>$name)

            {

            $username = mysql_real_escape_string($name);

            $photo = mysql_real_escape_string($_POST['interests'][$row]);

            mysql_query('INSERT INTO photos (username, photo) VALUES ("' . $username . '", ' . $photo . ')') or die(mysql_error());

            }




}



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>array test</title>
</head>
<body>
<form action="array.php" method="post">



    <input type="checkbox" name="interests[]" value="Politics" />Politics<br />
    <input type="checkbox" name="interests[]" value="Entertainment" /><br />
    <input type="checkbox" name="interests[]" value="Tech" /><br />
    <input type="checkbox" name="interests[]" value="Health" /><br />
    <input type="checkbox" name="interests[]" value="Living" /><br />
    <input type="checkbox" name="interests[]" value="Travel" /><br />
    <input type="checkbox" name="interests[]" value="World" /><br />


    <input type="submit" value="Submit">
</form>
</body>
</html>

$photo is not surrounded by quotes in your SQL statement. $ photo不在SQL语句中用引号引起来。 Also, the loop goes like this: 同样,循环如下所示:

Loop 1: $username="harp1", $photo="[value of first selected checkbox]" 循环1:$ username =“ harp1”,$ photo =“ [第一个选定复选框的值]”

Loop 2: $username="harp2", $photo="[second value]" 循环2:$ username =“ harp2”,$ photo =“ [第二个值]”

Loop 3: $username="harp3", $photo="[third value]" 循环3:$ username =“ harp3”,$ photo =“ [第三值]”

it doesn't make sense... 这没有道理...

Use double quoted strings to create database queries: 使用双引号字符串创建数据库查询:

mysql_query("INSERT INTO photos (username, photo) VALUES ('$username', '$photo')")
    or die(mysql_error());

You forgot to put quotes around $photo . 您忘记在$photo周围加上引号。 Double quoted strings reduce this kind of errors by making them easier to read. 双引号字符串使它们更易于阅读,从而减少了此类错误。

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

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